1
1
import { deprecate } from 'util' ;
2
- import { emitDeprecatedOptionWarning , Callback } from './utils' ;
2
+ import {
3
+ emitDeprecatedOptionWarning ,
4
+ Callback ,
5
+ resolveOptions ,
6
+ filterOptions ,
7
+ deprecateOptions ,
8
+ MongoDBNamespace ,
9
+ getTopology
10
+ } from './utils' ;
3
11
import { loadAdmin } from './dynamic_loaders' ;
4
12
import { AggregationCursor , CommandCursor } from './cursor' ;
5
13
import { ObjectId , Code , Document , BSONSerializeOptions , resolveBSONOptions } from './bson' ;
@@ -11,13 +19,6 @@ import * as CONSTANTS from './constants';
11
19
import { WriteConcern , WriteConcernOptions } from './write_concern' ;
12
20
import { ReadConcern } from './read_concern' ;
13
21
import { Logger , LoggerOptions } from './logger' ;
14
- import {
15
- filterOptions ,
16
- mergeOptionsAndWriteConcern ,
17
- deprecateOptions ,
18
- MongoDBNamespace ,
19
- getTopology
20
- } from './utils' ;
21
22
import { AggregateOperation , AggregateOptions } from './operations/aggregate' ;
22
23
import { AddUserOperation , AddUserOptions } from './operations/add_user' ;
23
24
import { CollectionsOperation } from './operations/collections' ;
@@ -254,19 +255,20 @@ export class Db implements OperationParent {
254
255
callback ?: Callback < Collection >
255
256
) : Promise < Collection > | void {
256
257
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
257
- options = options || { } ;
258
- options . readConcern = ReadConcern . fromOptions ( options ) ?? this . readConcern ;
259
258
260
259
return executeOperation (
261
260
getTopology ( this ) ,
262
- new CreateCollectionOperation ( this , name , options ) ,
261
+ new CreateCollectionOperation ( this , name , resolveOptions ( this , options ) ) ,
263
262
callback
264
263
) ;
265
264
}
266
265
267
266
/**
268
267
* Execute a command
269
268
*
269
+ * @remarks
270
+ * This command does not inherit options from the MongoClient.
271
+ *
270
272
* @param command - The command to run
271
273
* @param options - Optional settings for the command
272
274
* @param callback - An optional callback, a Promise will be returned if none is provided
@@ -281,11 +283,11 @@ export class Db implements OperationParent {
281
283
callback ?: Callback < Document >
282
284
) : Promise < Document > | void {
283
285
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
284
- options = options || { } ;
285
286
287
+ // Intentionally, we do not inherit options from parent for this operation.
286
288
return executeOperation (
287
289
getTopology ( this ) ,
288
- new RunCommandOperation ( this , command , options ) ,
290
+ new RunCommandOperation ( this , command , options ?? { } ) ,
289
291
callback
290
292
) ;
291
293
}
@@ -307,8 +309,7 @@ export class Db implements OperationParent {
307
309
throw new TypeError ( '`options` parameter must not be function' ) ;
308
310
}
309
311
310
- options = options || { } ;
311
-
312
+ options = resolveOptions ( this , options ) ;
312
313
const cursor = new AggregationCursor (
313
314
getTopology ( this ) ,
314
315
new AggregateOperation ( this , pipeline , options ) ,
@@ -341,21 +342,10 @@ export class Db implements OperationParent {
341
342
callback ?: Callback < Collection >
342
343
) : Collection | void {
343
344
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
344
- options = Object . assign ( { } , options ) ;
345
-
346
- // If we have not set a collection level readConcern set the db level one
347
- options . readConcern = ReadConcern . fromOptions ( options ) ?? this . readConcern ;
348
-
349
- // Merge in all needed options and ensure correct writeConcern merging from db level
350
- const finalOptions = mergeOptionsAndWriteConcern (
351
- options ,
352
- this . s . options ?? { } ,
353
- collectionKeys ,
354
- true
355
- ) as CollectionOptions ;
345
+ const finalOptions = resolveOptions ( this , options ) ;
356
346
357
347
// Execute
358
- if ( finalOptions == null || ! finalOptions . strict ) {
348
+ if ( ! finalOptions . strict ) {
359
349
try {
360
350
const collection = new Collection ( this , name , finalOptions ) ;
361
351
if ( callback ) callback ( undefined , collection ) ;
@@ -414,9 +404,11 @@ export class Db implements OperationParent {
414
404
callback ?: Callback < Document >
415
405
) : Promise < Document > | void {
416
406
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
417
- options = options || { } ;
418
-
419
- return executeOperation ( getTopology ( this ) , new DbStatsOperation ( this , options ) , callback ) ;
407
+ return executeOperation (
408
+ getTopology ( this ) ,
409
+ new DbStatsOperation ( this , resolveOptions ( this , options ) ) ,
410
+ callback
411
+ ) ;
420
412
}
421
413
422
414
/**
@@ -427,7 +419,7 @@ export class Db implements OperationParent {
427
419
*/
428
420
listCollections ( filter ?: Document , options ?: ListCollectionsOptions ) : CommandCursor {
429
421
filter = filter || { } ;
430
- options = options || { } ;
422
+ options = resolveOptions ( this , options ) ;
431
423
432
424
return new CommandCursor (
433
425
getTopology ( this ) ,
@@ -439,6 +431,9 @@ export class Db implements OperationParent {
439
431
/**
440
432
* Rename a collection.
441
433
*
434
+ * @remarks
435
+ * This operation does not inherit options from the MongoClient.
436
+ *
442
437
* @param fromCollection - Name of current collection to rename
443
438
* @param toCollection - New name of of the collection
444
439
* @param options - Optional settings for the command
@@ -468,7 +463,9 @@ export class Db implements OperationParent {
468
463
callback ?: Callback < Collection >
469
464
) : Promise < Collection > | void {
470
465
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
471
- options = Object . assign ( { } , options , { readPreference : ReadPreference . PRIMARY } ) ;
466
+
467
+ // Intentionally, we do not inherit options from parent for this operation.
468
+ options = { ...options , readPreference : ReadPreference . PRIMARY } ;
472
469
473
470
// Add return new collection
474
471
options . new_collection = true ;
@@ -497,11 +494,10 @@ export class Db implements OperationParent {
497
494
callback ?: Callback < boolean >
498
495
) : Promise < boolean > | void {
499
496
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
500
- options = options || { } ;
501
497
502
498
return executeOperation (
503
499
getTopology ( this ) ,
504
- new DropCollectionOperation ( this , name , options ) ,
500
+ new DropCollectionOperation ( this , name , resolveOptions ( this , options ) ) ,
505
501
callback
506
502
) ;
507
503
}
@@ -521,9 +517,12 @@ export class Db implements OperationParent {
521
517
callback ?: Callback < boolean >
522
518
) : Promise < boolean > | void {
523
519
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
524
- options = options || { } ;
525
520
526
- return executeOperation ( getTopology ( this ) , new DropDatabaseOperation ( this , options ) , callback ) ;
521
+ return executeOperation (
522
+ getTopology ( this ) ,
523
+ new DropDatabaseOperation ( this , resolveOptions ( this , options ) ) ,
524
+ callback
525
+ ) ;
527
526
}
528
527
529
528
/**
@@ -541,14 +540,20 @@ export class Db implements OperationParent {
541
540
callback ?: Callback < Collection [ ] >
542
541
) : Promise < Collection [ ] > | void {
543
542
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
544
- options = options || { } ;
545
543
546
- return executeOperation ( getTopology ( this ) , new CollectionsOperation ( this , options ) , callback ) ;
544
+ return executeOperation (
545
+ getTopology ( this ) ,
546
+ new CollectionsOperation ( this , resolveOptions ( this , options ) ) ,
547
+ callback
548
+ ) ;
547
549
}
548
550
549
551
/**
550
552
* Runs a command on the database as admin.
551
553
*
554
+ * @remarks
555
+ * This command does not inherit options from the MongoClient.
556
+ *
552
557
* @param command - The command to run
553
558
* @param options - Optional settings for the command
554
559
* @param callback - An optional callback, a Promise will be returned if none is provided
@@ -567,11 +572,11 @@ export class Db implements OperationParent {
567
572
callback ?: Callback < void >
568
573
) : Promise < void > | void {
569
574
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
570
- options = options || { } ;
571
575
576
+ // Intentionally, we do not inherit options from parent for this operation.
572
577
return executeOperation (
573
578
getTopology ( this ) ,
574
- new RunAdminCommandOperation ( this , command , options ) ,
579
+ new RunAdminCommandOperation ( this , command , options ?? { } ) ,
575
580
callback
576
581
) ;
577
582
}
@@ -604,11 +609,10 @@ export class Db implements OperationParent {
604
609
callback ?: Callback < Document >
605
610
) : Promise < Document > | void {
606
611
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
607
- options = options ? Object . assign ( { } , options ) : { } ;
608
612
609
613
return executeOperation (
610
614
getTopology ( this ) ,
611
- new CreateIndexOperation ( this , name , indexSpec , options ) ,
615
+ new CreateIndexOperation ( this , name , indexSpec , resolveOptions ( this , options ) ) ,
612
616
callback
613
617
) ;
614
618
}
@@ -652,10 +656,9 @@ export class Db implements OperationParent {
652
656
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
653
657
}
654
658
655
- options = options || { } ;
656
659
return executeOperation (
657
660
getTopology ( this ) ,
658
- new AddUserOperation ( this , username , password , options ) ,
661
+ new AddUserOperation ( this , username , password , resolveOptions ( this , options ) ) ,
659
662
callback
660
663
) ;
661
664
}
@@ -677,11 +680,10 @@ export class Db implements OperationParent {
677
680
callback ?: Callback < boolean >
678
681
) : Promise < boolean > | void {
679
682
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
680
- options = options || { } ;
681
683
682
684
return executeOperation (
683
685
getTopology ( this ) ,
684
- new RemoveUserOperation ( this , username , options ) ,
686
+ new RemoveUserOperation ( this , username , resolveOptions ( this , options ) ) ,
685
687
callback
686
688
) ;
687
689
}
@@ -710,11 +712,10 @@ export class Db implements OperationParent {
710
712
callback ?: Callback < ProfilingLevel >
711
713
) : Promise < ProfilingLevel > | void {
712
714
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
713
- options = options || { } ;
714
715
715
716
return executeOperation (
716
717
getTopology ( this ) ,
717
- new SetProfilingLevelOperation ( this , level , options ) ,
718
+ new SetProfilingLevelOperation ( this , level , resolveOptions ( this , options ) ) ,
718
719
callback
719
720
) ;
720
721
}
@@ -734,11 +735,10 @@ export class Db implements OperationParent {
734
735
callback ?: Callback < string >
735
736
) : Promise < string > | void {
736
737
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
737
- options = options || { } ;
738
738
739
739
return executeOperation (
740
740
getTopology ( this ) ,
741
- new ProfilingLevelOperation ( this , options ) ,
741
+ new ProfilingLevelOperation ( this , resolveOptions ( this , options ) ) ,
742
742
callback
743
743
) ;
744
744
}
@@ -764,11 +764,10 @@ export class Db implements OperationParent {
764
764
callback ?: Callback < Document >
765
765
) : Promise < Document > | void {
766
766
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
767
- options = options || { } ;
768
767
769
768
return executeOperation (
770
769
getTopology ( this ) ,
771
- new IndexInformationOperation ( this , name , options ) ,
770
+ new IndexInformationOperation ( this , name , resolveOptions ( this , options ) ) ,
772
771
callback
773
772
) ;
774
773
}
@@ -835,11 +834,10 @@ export class Db implements OperationParent {
835
834
callback ?: Callback < Document >
836
835
) : Promise < Document > | void {
837
836
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
838
- options = options || { } ;
839
837
840
838
return executeOperation (
841
839
getTopology ( this ) ,
842
- new EvalOperation ( this , code , parameters , options ) ,
840
+ new EvalOperation ( this , code , parameters , resolveOptions ( this , options ) ) ,
843
841
callback
844
842
) ;
845
843
}
@@ -873,11 +871,10 @@ export class Db implements OperationParent {
873
871
callback ?: Callback < Document >
874
872
) : Promise < Document > | void {
875
873
if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
876
- options = options || { } ;
877
874
878
875
return executeOperation (
879
876
getTopology ( this ) ,
880
- new EnsureIndexOperation ( this , name , fieldOrSpec , options ) ,
877
+ new EnsureIndexOperation ( this , name , fieldOrSpec , resolveOptions ( this , options ) ) ,
881
878
callback
882
879
) ;
883
880
}
@@ -905,18 +902,6 @@ export class Db implements OperationParent {
905
902
}
906
903
}
907
904
908
- const collectionKeys = [
909
- 'pkFactory' ,
910
- 'readPreference' ,
911
- 'serializeFunctions' ,
912
- 'strict' ,
913
- 'readConcern' ,
914
- 'ignoreUndefined' ,
915
- 'promoteValues' ,
916
- 'promoteBuffers' ,
917
- 'promoteLongs'
918
- ] ;
919
-
920
905
Db . prototype . createCollection = deprecateOptions (
921
906
{
922
907
name : 'Db.createCollection' ,
0 commit comments