This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

246
node_modules/mongodb/lib/db.js generated vendored
View File

@@ -12,9 +12,8 @@ const MongoError = require('./core').MongoError;
const ObjectID = require('./core').ObjectID;
const Logger = require('./core').Logger;
const Collection = require('./collection');
const mergeOptionsAndWriteConcern = require('./utils').mergeOptionsAndWriteConcern;
const conditionallyMergeWriteConcern = require('./utils').conditionallyMergeWriteConcern;
const executeLegacyOperation = require('./utils').executeLegacyOperation;
const resolveReadPreference = require('./utils').resolveReadPreference;
const ChangeStream = require('./change_stream');
const deprecate = require('util').deprecate;
const deprecateOptions = require('./utils').deprecateOptions;
@@ -35,8 +34,9 @@ const AggregateOperation = require('./operations/aggregate');
const AddUserOperation = require('./operations/add_user');
const CollectionsOperation = require('./operations/collections');
const CommandOperation = require('./operations/command');
const RunCommandOperation = require('./operations/run_command');
const CreateCollectionOperation = require('./operations/create_collection');
const CreateIndexOperation = require('./operations/create_index');
const CreateIndexesOperation = require('./operations/create_indexes');
const DropCollectionOperation = require('./operations/drop').DropCollectionOperation;
const DropDatabaseOperation = require('./operations/drop').DropDatabaseOperation;
const ExecuteDbAdminCommandOperation = require('./operations/execute_db_admin_command');
@@ -72,6 +72,7 @@ const legalOptionNames = [
'wtimeout',
'fsync',
'j',
'writeConcern',
'readPreference',
'readPreferenceTags',
'native_parser',
@@ -82,7 +83,6 @@ const legalOptionNames = [
'bufferMaxEntries',
'authSource',
'ignoreUndefined',
'promoteLongs',
'promiseLibrary',
'readConcern',
'retryMiliSeconds',
@@ -94,6 +94,7 @@ const legalOptionNames = [
'promoteBuffers',
'promoteLongs',
'promoteValues',
'bsonRegExp',
'compression',
'retryWrites'
];
@@ -105,9 +106,10 @@ const legalOptionNames = [
* @param {(Server|ReplSet|Mongos)} topology The server topology for the database.
* @param {object} [options] Optional settings.
* @param {string} [options.authSource] If the database authentication is dependent on another databaseName.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {boolean} [options.forceServerObjectId=false] Force server to assign _id values instead of driver.
* @param {boolean} [options.serializeFunctions=false] Serialize functions on any object.
* @param {Boolean} [options.ignoreUndefined=false] Specify if the BSON serializer should ignore undefined fields.
@@ -115,6 +117,7 @@ const legalOptionNames = [
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {number} [options.bufferMaxEntries=-1] Sets a cap on how many operations the driver will buffer up before giving up on getting a working connection, default is -1 which is unlimited.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {object} [options.pkFactory] A primary key factory object for generation of custom _id keys.
@@ -211,6 +214,14 @@ function Db(databaseName, topology, options) {
inherits(Db, EventEmitter);
Db.prototype.on = deprecate(function() {
return Db.super_.prototype.on.apply(this, arguments);
}, 'Listening to events on the Db class has been deprecated and will be removed in the next major version.');
Db.prototype.once = deprecate(function() {
return Db.super_.prototype.once.apply(this, arguments);
}, 'Listening to events on the Db class has been deprecated and will be removed in the next major version.');
// Topology
Object.defineProperty(Db.prototype, 'topology', {
enumerable: true,
@@ -290,7 +301,7 @@ Db.prototype.command = function(command, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({}, options);
const commandOperation = new CommandOperation(this, options, null, command);
const commandOperation = new RunCommandOperation(this, command, options);
return executeOperation(this.s.topology, commandOperation, callback);
};
@@ -304,7 +315,7 @@ Db.prototype.command = function(command, options, callback) {
* @param {number} [options.batchSize=1000] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/aggregate|aggregation documentation}.
* @param {object} [options.cursor] Return the query as cursor, on 2.6 > it returns as a real cursor on pre 2.6 it returns as an emulated cursor.
* @param {number} [options.cursor.batchSize=1000] Deprecated. Use `options.batchSize`
* @param {boolean} [options.explain=false] Explain returns the aggregation execution plan (requires mongodb 2.6 >).
* @param {'queryPlanner'|'queryPlannerExtended'|'executionStats'|'allPlansExecution'|boolean} [options.explain] The verbosity mode for the explain output.
* @param {boolean} [options.allowDiskUse=false] allowDiskUse lets the server know if it can use disk to store temporary results for the aggregation (requires mongodb 2.6 >).
* @param {number} [options.maxTimeMS] maxTimeMS specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point.
* @param {number} [options.maxAwaitTimeMS] The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query.
@@ -313,6 +324,7 @@ Db.prototype.command = function(command, options, callback) {
* @param {boolean} [options.promoteLongs=true] Promotes Long values to number if they fit inside the 53 bits resolution.
* @param {boolean} [options.promoteValues=true] Promotes BSON values to native types where possible, set to false to only receive wrapper types.
* @param {boolean} [options.promoteBuffers=false] Promotes Binary BSON values to native Node Buffers.
* @param {boolean} [options.bsonRegExp=false] By default, regex returned from MDB will be native to the language. Setting to true will ensure that a BSON.BSONRegExp object is returned.
* @param {object} [options.collation] Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields).
* @param {string} [options.comment] Add a comment to an aggregation command
* @param {string|object} [options.hint] Add an index selection hint to an aggregation command
@@ -372,7 +384,7 @@ Db.prototype.admin = function() {
* @param {AggregationCursor} cursor The cursor if the aggregation command was executed successfully.
*/
const collectionKeys = [
const COLLECTION_OPTION_KEYS = [
'pkFactory',
'readPreference',
'serializeFunctions',
@@ -381,7 +393,8 @@ const collectionKeys = [
'ignoreUndefined',
'promoteValues',
'promoteBuffers',
'promoteLongs'
'promoteLongs',
'bsonRegExp'
];
/**
@@ -391,92 +404,106 @@ const collectionKeys = [
* @method
* @param {string} name the collection name we wish to access.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
* @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);
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);
// Set the promise library
options.promiseLibrary = this.s.promiseLibrary;
// Set the promise library
options.promiseLibrary = this.s.promiseLibrary;
// 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;
// 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;
// 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 = mergeOptionsAndWriteConcern(options, this.s.options, collectionKeys, true);
// 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;
// Do we have ignoreUndefined set
if (this.s.options.ignoreUndefined) {
options.ignoreUndefined = this.s.options.ignoreUndefined;
}
}
// Strict mode
if (typeof callback !== 'function') {
throw toError(`A callback is required in strict mode. While getting collection ${name}`);
}
// 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);
for (const collectionOptionKey of COLLECTION_OPTION_KEYS) {
if (!(collectionOptionKey in options) && this.s.options[collectionOptionKey] !== undefined) {
options[collectionOptionKey] = this.s.options[collectionOptionKey];
}
}
});
};
// 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;
}
}
// Strict mode
if (typeof callback !== 'function') {
throw toError(`A callback is required in strict mode. While getting collection ${name}`);
}
// 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.
@@ -485,14 +512,15 @@ Db.prototype.collection = function(name, options, callback) {
* @method
* @param {string} name the collection name we wish to access.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {boolean} [options.raw=false] Return document results as raw BSON buffers.
* @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 {boolean} [options.capped=false] Create a capped collection.
* @param {boolean} [options.autoIndexId=true] DEPRECATED: Create an index on the _id field of the document, True by default on MongoDB 2.6 - 3.0
* @param {number} [options.size] The size of the capped collection in bytes.
@@ -513,7 +541,7 @@ Db.prototype.collection = function(name, options, callback) {
Db.prototype.createCollection = deprecateOptions(
{
name: 'Db.createCollection',
deprecatedOptions: ['autoIndexId'],
deprecatedOptions: ['autoIndexId', 'strict', 'w', 'wtimeout', 'j'],
optionsIndex: 1
},
function(name, options, callback) {
@@ -643,10 +671,10 @@ Db.prototype.renameCollection = function(fromCollection, toCollection, options,
* @method
* @param {string} name Name of collection to drop
* @param {Object} [options] Optional settings
* @param {WriteConcern} [options.writeConcern] A full WriteConcern object
* @param {(number|string)} [options.w] The write concern
* @param {number} [options.wtimeout] The write concern timeout
* @param {boolean} [options.j] The journal write concern
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Db~resultCallback} [callback] The results callback
* @return {Promise} returns Promise if no callback passed
@@ -709,7 +737,7 @@ Db.prototype.collections = function(options, callback) {
Db.prototype.executeDbAdminCommand = function(selector, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
options.readPreference = resolveReadPreference(this, options);
options.readPreference = ReadPreference.resolve(this, options);
const executeDbAdminCommandOperation = new ExecuteDbAdminCommandOperation(
this,
@@ -726,9 +754,10 @@ Db.prototype.executeDbAdminCommand = function(selector, options, callback) {
* @param {string} name Name of the collection to create the index on.
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {boolean} [options.unique=false] Creates an unique index.
* @param {boolean} [options.sparse=false] Creates a sparse index.
* @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible.
@@ -740,6 +769,7 @@ Db.prototype.executeDbAdminCommand = function(selector, options, callback) {
* @param {string} [options.name] Override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
* @param {object} [options.partialFilterExpression] Creates a partial index based on the given filter object (MongoDB 3.2 or higher)
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {(number|string)} [options.commitQuorum] (MongoDB 4.4. or higher) Specifies how many data-bearing members of a replica set, including the primary, must complete the index builds successfully before the primary marks the indexes as ready. This option accepts the same values for the "w" field in a write concern plus "votingMembers", which indicates all voting data-bearing nodes.
* @param {Db~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
@@ -747,9 +777,9 @@ Db.prototype.createIndex = function(name, fieldOrSpec, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options ? Object.assign({}, options) : {};
const createIndexOperation = new CreateIndexOperation(this, name, fieldOrSpec, options);
const createIndexesOperation = new CreateIndexesOperation(this, name, fieldOrSpec, options);
return executeOperation(this.s.topology, createIndexOperation, callback);
return executeOperation(this.s.topology, createIndexesOperation, callback);
};
/**
@@ -759,9 +789,10 @@ Db.prototype.createIndex = function(name, fieldOrSpec, options, callback) {
* @param {string} name The index name
* @param {(string|object)} fieldOrSpec Defines the index.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {boolean} [options.unique=false] Creates an unique index.
* @param {boolean} [options.sparse=false] Creates a sparse index.
* @param {boolean} [options.background=false] Creates the index in the background, yielding whenever possible.
@@ -799,9 +830,10 @@ Db.prototype.addChild = function(db) {
* @param {string} username The username.
* @param {string} password The password.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {object} [options.customData] Custom data associated with the user (only Mongodb 2.6 or higher)
* @param {object[]} [options.roles] Roles associated with the created user (only Mongodb 2.6 or higher)
* @param {ClientSession} [options.session] optional session to use for this operation
@@ -828,9 +860,10 @@ Db.prototype.addUser = function(username, password, options, callback) {
* @method
* @param {string} username The username.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] The write concern.
* @param {number} [options.wtimeout] The write concern timeout.
* @param {boolean} [options.j=false] Specify a journal write concern.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Db~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
@@ -918,6 +951,7 @@ Db.prototype.indexInformation = function(name, options, callback) {
/**
* Unref all sockets
* @method
* @deprecated This function is deprecated and will be removed in the next major version.
*/
Db.prototype.unref = function() {
this.s.topology.unref();