Skip to content

Commit

Permalink
refactor(NODE-2752): deprecate strict option for Db.collection (#2819)
Browse files Browse the repository at this point in the history
  • Loading branch information
emadum committed May 28, 2021
1 parent 394832a commit f42ac4c
Showing 1 changed file with 77 additions and 70 deletions.
147 changes: 77 additions & 70 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,91 +409,98 @@ const COLLECTION_OPTION_KEYS = [
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {boolean} [options.strict=false] Returns an error if the collection does not exist
* @param {boolean} [options.strict=false] **Deprecated** Returns an error if the collection does not exist
* @param {object} [options.readConcern] Specify a read concern for the collection. (only MongoDB 3.2 or higher supported)
* @param {ReadConcernLevel} [options.readConcern.level='local'] Specify a read concern level for the collection operations (only MongoDB 3.2 or higher supported)
* @param {Db~collectionResultCallback} [callback] The collection result callback
* @return {Collection} return the new Collection instance if not in strict mode
*/
Db.prototype.collection = function(name, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = Object.assign({}, options);

// Set the promise library
options.promiseLibrary = this.s.promiseLibrary;
Db.prototype.collection = deprecateOptions(
{
name: 'Db.collection',
deprecatedOptions: ['strict'],
optionsIndex: 1
},
function(name, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options = Object.assign({}, options);

// If we have not set a collection level readConcern set the db level one
options.readConcern = options.readConcern
? new ReadConcern(options.readConcern.level)
: this.readConcern;
// Set the promise library
options.promiseLibrary = this.s.promiseLibrary;

// Do we have ignoreUndefined set
if (this.s.options.ignoreUndefined) {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
// If we have not set a collection level readConcern set the db level one
options.readConcern = options.readConcern
? new ReadConcern(options.readConcern.level)
: this.readConcern;

for (const collectionOptionKey of COLLECTION_OPTION_KEYS) {
if (!(collectionOptionKey in options) && this.s.options[collectionOptionKey] !== undefined) {
options[collectionOptionKey] = this.s.options[collectionOptionKey];
// Do we have ignoreUndefined set
if (this.s.options.ignoreUndefined) {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
}

// Merge in all needed options and ensure correct writeConcern merging from db level
options = conditionallyMergeWriteConcern(options, this.s.options);

// Execute
if (options == null || !options.strict) {
try {
const collection = new Collection(
this,
this.s.topology,
this.databaseName,
name,
this.s.pkFactory,
options
);
if (callback) callback(null, collection);
return collection;
} catch (err) {
if (err instanceof MongoError && callback) return callback(err);
throw err;
for (const collectionOptionKey of COLLECTION_OPTION_KEYS) {
if (!(collectionOptionKey in options) && this.s.options[collectionOptionKey] !== undefined) {
options[collectionOptionKey] = this.s.options[collectionOptionKey];
}
}
}

// Strict mode
if (typeof callback !== 'function') {
throw toError(`A callback is required in strict mode. While getting collection ${name}`);
}
// Merge in all needed options and ensure correct writeConcern merging from db level
options = conditionallyMergeWriteConcern(options, this.s.options);

// Execute
if (options == null || !options.strict) {
try {
const collection = new Collection(
this,
this.s.topology,
this.databaseName,
name,
this.s.pkFactory,
options
);
if (callback) callback(null, collection);
return collection;
} catch (err) {
if (err instanceof MongoError && callback) return callback(err);
throw err;
}
}

// Did the user destroy the topology
if (this.serverConfig && this.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
// Strict mode
if (typeof callback !== 'function') {
throw toError(`A callback is required in strict mode. While getting collection ${name}`);
}

const listCollectionOptions = Object.assign({}, options, { nameOnly: true });

// Strict mode
this.listCollections({ name: name }, listCollectionOptions).toArray((err, collections) => {
if (err != null) return handleCallback(callback, err, null);
if (collections.length === 0)
return handleCallback(
callback,
toError(`Collection ${name} does not exist. Currently in strict mode.`),
null
);

try {
return handleCallback(
callback,
null,
new Collection(this, this.s.topology, this.databaseName, name, this.s.pkFactory, options)
);
} catch (err) {
return handleCallback(callback, err, null);
// Did the user destroy the topology
if (this.serverConfig && this.serverConfig.isDestroyed()) {
return callback(new MongoError('topology was destroyed'));
}
});
};

const listCollectionOptions = Object.assign({}, options, { nameOnly: true });

// Strict mode
this.listCollections({ name: name }, listCollectionOptions).toArray((err, collections) => {
if (err != null) return handleCallback(callback, err, null);
if (collections.length === 0)
return handleCallback(
callback,
toError(`Collection ${name} does not exist. Currently in strict mode.`),
null
);

try {
return handleCallback(
callback,
null,
new Collection(this, this.s.topology, this.databaseName, name, this.s.pkFactory, options)
);
} catch (err) {
return handleCallback(callback, err, null);
}
});
}
);

/**
* Create a new collection on a server with the specified options. Use this to create capped collections.
Expand Down

0 comments on commit f42ac4c

Please sign in to comment.