@@ -409,91 +409,98 @@ const COLLECTION_OPTION_KEYS = [
409
409
* @param {object } [options.pkFactory] A primary key factory object for generation of custom _id keys.
410
410
* @param {(ReadPreference|string) } [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
411
411
* @param {boolean } [options.serializeFunctions=false] Serialize functions on any object.
412
- * @param {boolean } [options.strict=false] Returns an error if the collection does not exist
412
+ * @param {boolean } [options.strict=false] **Deprecated** Returns an error if the collection does not exist
413
413
* @param {object } [options.readConcern] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
414
414
* @param {ReadConcernLevel } [options.readConcern.level='local'] Specify a read concern level for the collection operations (only MongoDB 3.2 or higher supported)
415
415
* @param {Db~collectionResultCallback } [callback] The collection result callback
416
416
* @return {Collection } return the new Collection instance if not in strict mode
417
417
*/
418
- Db . prototype . collection = function ( name , options , callback ) {
419
- if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
420
- options = options || { } ;
421
- options = Object . assign ( { } , options ) ;
422
-
423
- // Set the promise library
424
- options . promiseLibrary = this . s . promiseLibrary ;
418
+ Db . prototype . collection = deprecateOptions (
419
+ {
420
+ name : 'Db.collection' ,
421
+ deprecatedOptions : [ 'strict' ] ,
422
+ optionsIndex : 1
423
+ } ,
424
+ function ( name , options , callback ) {
425
+ if ( typeof options === 'function' ) ( callback = options ) , ( options = { } ) ;
426
+ options = options || { } ;
427
+ options = Object . assign ( { } , options ) ;
425
428
426
- // If we have not set a collection level readConcern set the db level one
427
- options . readConcern = options . readConcern
428
- ? new ReadConcern ( options . readConcern . level )
429
- : this . readConcern ;
429
+ // Set the promise library
430
+ options . promiseLibrary = this . s . promiseLibrary ;
430
431
431
- // Do we have ignoreUndefined set
432
- if ( this . s . options . ignoreUndefined ) {
433
- options . ignoreUndefined = this . s . options . ignoreUndefined ;
434
- }
432
+ // If we have not set a collection level readConcern set the db level one
433
+ options . readConcern = options . readConcern
434
+ ? new ReadConcern ( options . readConcern . level )
435
+ : this . readConcern ;
435
436
436
- for ( const collectionOptionKey of COLLECTION_OPTION_KEYS ) {
437
- if ( ! ( collectionOptionKey in options ) && this . s . options [ collectionOptionKey ] !== undefined ) {
438
- options [ collectionOptionKey ] = this . s . options [ collectionOptionKey ] ;
437
+ // Do we have ignoreUndefined set
438
+ if ( this . s . options . ignoreUndefined ) {
439
+ options . ignoreUndefined = this . s . options . ignoreUndefined ;
439
440
}
440
- }
441
441
442
- // Merge in all needed options and ensure correct writeConcern merging from db level
443
- options = conditionallyMergeWriteConcern ( options , this . s . options ) ;
444
-
445
- // Execute
446
- if ( options == null || ! options . strict ) {
447
- try {
448
- const collection = new Collection (
449
- this ,
450
- this . s . topology ,
451
- this . databaseName ,
452
- name ,
453
- this . s . pkFactory ,
454
- options
455
- ) ;
456
- if ( callback ) callback ( null , collection ) ;
457
- return collection ;
458
- } catch ( err ) {
459
- if ( err instanceof MongoError && callback ) return callback ( err ) ;
460
- throw err ;
442
+ for ( const collectionOptionKey of COLLECTION_OPTION_KEYS ) {
443
+ if ( ! ( collectionOptionKey in options ) && this . s . options [ collectionOptionKey ] !== undefined ) {
444
+ options [ collectionOptionKey ] = this . s . options [ collectionOptionKey ] ;
445
+ }
461
446
}
462
- }
463
447
464
- // Strict mode
465
- if ( typeof callback !== 'function' ) {
466
- throw toError ( `A callback is required in strict mode. While getting collection ${ name } ` ) ;
467
- }
448
+ // Merge in all needed options and ensure correct writeConcern merging from db level
449
+ options = conditionallyMergeWriteConcern ( options , this . s . options ) ;
450
+
451
+ // Execute
452
+ if ( options == null || ! options . strict ) {
453
+ try {
454
+ const collection = new Collection (
455
+ this ,
456
+ this . s . topology ,
457
+ this . databaseName ,
458
+ name ,
459
+ this . s . pkFactory ,
460
+ options
461
+ ) ;
462
+ if ( callback ) callback ( null , collection ) ;
463
+ return collection ;
464
+ } catch ( err ) {
465
+ if ( err instanceof MongoError && callback ) return callback ( err ) ;
466
+ throw err ;
467
+ }
468
+ }
468
469
469
- // Did the user destroy the topology
470
- if ( this . serverConfig && this . serverConfig . isDestroyed ( ) ) {
471
- return callback ( new MongoError ( 'topology was destroyed' ) ) ;
472
- }
470
+ // Strict mode
471
+ if ( typeof callback !== 'function' ) {
472
+ throw toError ( `A callback is required in strict mode. While getting collection ${ name } ` ) ;
473
+ }
473
474
474
- const listCollectionOptions = Object . assign ( { } , options , { nameOnly : true } ) ;
475
-
476
- // Strict mode
477
- this . listCollections ( { name : name } , listCollectionOptions ) . toArray ( ( err , collections ) => {
478
- if ( err != null ) return handleCallback ( callback , err , null ) ;
479
- if ( collections . length === 0 )
480
- return handleCallback (
481
- callback ,
482
- toError ( `Collection ${ name } does not exist. Currently in strict mode.` ) ,
483
- null
484
- ) ;
485
-
486
- try {
487
- return handleCallback (
488
- callback ,
489
- null ,
490
- new Collection ( this , this . s . topology , this . databaseName , name , this . s . pkFactory , options )
491
- ) ;
492
- } catch ( err ) {
493
- return handleCallback ( callback , err , null ) ;
475
+ // Did the user destroy the topology
476
+ if ( this . serverConfig && this . serverConfig . isDestroyed ( ) ) {
477
+ return callback ( new MongoError ( 'topology was destroyed' ) ) ;
494
478
}
495
- } ) ;
496
- } ;
479
+
480
+ const listCollectionOptions = Object . assign ( { } , options , { nameOnly : true } ) ;
481
+
482
+ // Strict mode
483
+ this . listCollections ( { name : name } , listCollectionOptions ) . toArray ( ( err , collections ) => {
484
+ if ( err != null ) return handleCallback ( callback , err , null ) ;
485
+ if ( collections . length === 0 )
486
+ return handleCallback (
487
+ callback ,
488
+ toError ( `Collection ${ name } does not exist. Currently in strict mode.` ) ,
489
+ null
490
+ ) ;
491
+
492
+ try {
493
+ return handleCallback (
494
+ callback ,
495
+ null ,
496
+ new Collection ( this , this . s . topology , this . databaseName , name , this . s . pkFactory , options )
497
+ ) ;
498
+ } catch ( err ) {
499
+ return handleCallback ( callback , err , null ) ;
500
+ }
501
+ } ) ;
502
+ }
503
+ ) ;
497
504
498
505
/**
499
506
* Create a new collection on a server with the specified options. Use this to create capped collections.
0 commit comments