123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.Db = void 0;
- const admin_1 = require("./admin");
- const bson_1 = require("./bson");
- const change_stream_1 = require("./change_stream");
- const collection_1 = require("./collection");
- const CONSTANTS = require("./constants");
- const aggregation_cursor_1 = require("./cursor/aggregation_cursor");
- const error_1 = require("./error");
- const logger_1 = require("./logger");
- const add_user_1 = require("./operations/add_user");
- const collections_1 = require("./operations/collections");
- const create_collection_1 = require("./operations/create_collection");
- const drop_1 = require("./operations/drop");
- const execute_operation_1 = require("./operations/execute_operation");
- const indexes_1 = require("./operations/indexes");
- const list_collections_1 = require("./operations/list_collections");
- const profiling_level_1 = require("./operations/profiling_level");
- const remove_user_1 = require("./operations/remove_user");
- const rename_1 = require("./operations/rename");
- const run_command_1 = require("./operations/run_command");
- const set_profiling_level_1 = require("./operations/set_profiling_level");
- const stats_1 = require("./operations/stats");
- const read_concern_1 = require("./read_concern");
- const read_preference_1 = require("./read_preference");
- const utils_1 = require("./utils");
- const write_concern_1 = require("./write_concern");
- const DB_OPTIONS_ALLOW_LIST = [
- 'writeConcern',
- 'readPreference',
- 'readPreferenceTags',
- 'native_parser',
- 'forceServerObjectId',
- 'pkFactory',
- 'serializeFunctions',
- 'raw',
- 'authSource',
- 'ignoreUndefined',
- 'readConcern',
- 'retryMiliSeconds',
- 'numberOfRetries',
- 'loggerLevel',
- 'logger',
- 'promoteBuffers',
- 'promoteLongs',
- 'bsonRegExp',
- 'enableUtf8Validation',
- 'promoteValues',
- 'compression',
- 'retryWrites'
- ];
- class Db {
-
- constructor(client, databaseName, options) {
- var _a;
- options = options !== null && options !== void 0 ? options : {};
-
- options = (0, utils_1.filterOptions)(options, DB_OPTIONS_ALLOW_LIST);
-
- validateDatabaseName(databaseName);
-
- this.s = {
-
- client,
-
- options,
-
- logger: new logger_1.Logger('Db', options),
-
- readPreference: read_preference_1.ReadPreference.fromOptions(options),
-
- bsonOptions: (0, bson_1.resolveBSONOptions)(options, client),
-
- pkFactory: (_a = options === null || options === void 0 ? void 0 : options.pkFactory) !== null && _a !== void 0 ? _a : utils_1.DEFAULT_PK_FACTORY,
-
- readConcern: read_concern_1.ReadConcern.fromOptions(options),
- writeConcern: write_concern_1.WriteConcern.fromOptions(options),
-
- namespace: new utils_1.MongoDBNamespace(databaseName)
- };
- }
- get databaseName() {
- return this.s.namespace.db;
- }
-
- get options() {
- return this.s.options;
- }
-
- get slaveOk() {
- return this.secondaryOk;
- }
-
- get secondaryOk() {
- var _a;
- return ((_a = this.s.readPreference) === null || _a === void 0 ? void 0 : _a.preference) !== 'primary' || false;
- }
- get readConcern() {
- return this.s.readConcern;
- }
-
- get readPreference() {
- if (this.s.readPreference == null) {
- return this.s.client.readPreference;
- }
- return this.s.readPreference;
- }
- get bsonOptions() {
- return this.s.bsonOptions;
- }
-
- get writeConcern() {
- return this.s.writeConcern;
- }
- get namespace() {
- return this.s.namespace.toString();
- }
- createCollection(name, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new create_collection_1.CreateCollectionOperation(this, name, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- command(command, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
-
- return (0, execute_operation_1.executeOperation)(this.s.client, new run_command_1.RunCommandOperation(this, command, options !== null && options !== void 0 ? options : {}), callback);
- }
-
- aggregate(pipeline = [], options) {
- if (arguments.length > 2) {
- throw new error_1.MongoInvalidArgumentError('Method "db.aggregate()" accepts at most two arguments');
- }
- if (typeof pipeline === 'function') {
- throw new error_1.MongoInvalidArgumentError('Argument "pipeline" must not be function');
- }
- if (typeof options === 'function') {
- throw new error_1.MongoInvalidArgumentError('Argument "options" must not be function');
- }
- return new aggregation_cursor_1.AggregationCursor(this.s.client, this.s.namespace, pipeline, (0, utils_1.resolveOptions)(this, options));
- }
-
- admin() {
- return new admin_1.Admin(this);
- }
-
- collection(name, options = {}) {
- if (typeof options === 'function') {
- throw new error_1.MongoInvalidArgumentError('The callback form of this helper has been removed.');
- }
- const finalOptions = (0, utils_1.resolveOptions)(this, options);
- return new collection_1.Collection(this, name, finalOptions);
- }
- stats(options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new stats_1.DbStatsOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- listCollections(filter = {}, options = {}) {
- return new list_collections_1.ListCollectionsCursor(this, filter, (0, utils_1.resolveOptions)(this, options));
- }
- renameCollection(fromCollection, toCollection, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
-
- options = { ...options, readPreference: read_preference_1.ReadPreference.PRIMARY };
-
- options.new_collection = true;
- return (0, execute_operation_1.executeOperation)(this.s.client, new rename_1.RenameOperation(this.collection(fromCollection), toCollection, options), callback);
- }
- dropCollection(name, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new drop_1.DropCollectionOperation(this, name, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- dropDatabase(options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new drop_1.DropDatabaseOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- collections(options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new collections_1.CollectionsOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- createIndex(name, indexSpec, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new indexes_1.CreateIndexOperation(this, name, indexSpec, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- addUser(username, password, options, callback) {
- if (typeof password === 'function') {
- (callback = password), (password = undefined), (options = {});
- }
- else if (typeof password !== 'string') {
- if (typeof options === 'function') {
- (callback = options), (options = password), (password = undefined);
- }
- else {
- (options = password), (callback = undefined), (password = undefined);
- }
- }
- else {
- if (typeof options === 'function')
- (callback = options), (options = {});
- }
- return (0, execute_operation_1.executeOperation)(this.s.client, new add_user_1.AddUserOperation(this, username, password, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- removeUser(username, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new remove_user_1.RemoveUserOperation(this, username, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- setProfilingLevel(level, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new set_profiling_level_1.SetProfilingLevelOperation(this, level, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- profilingLevel(options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new profiling_level_1.ProfilingLevelOperation(this, (0, utils_1.resolveOptions)(this, options)), callback);
- }
- indexInformation(name, options, callback) {
- if (typeof options === 'function')
- (callback = options), (options = {});
- return (0, execute_operation_1.executeOperation)(this.s.client, new indexes_1.IndexInformationOperation(this, name, (0, utils_1.resolveOptions)(this, options)), callback);
- }
-
- unref() {
- (0, utils_1.getTopology)(this).unref();
- }
-
- watch(pipeline = [], options = {}) {
-
- if (!Array.isArray(pipeline)) {
- options = pipeline;
- pipeline = [];
- }
- return new change_stream_1.ChangeStream(this, pipeline, (0, utils_1.resolveOptions)(this, options));
- }
-
- getLogger() {
- return this.s.logger;
- }
- get logger() {
- return this.s.logger;
- }
- }
- exports.Db = Db;
- Db.SYSTEM_NAMESPACE_COLLECTION = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION;
- Db.SYSTEM_INDEX_COLLECTION = CONSTANTS.SYSTEM_INDEX_COLLECTION;
- Db.SYSTEM_PROFILE_COLLECTION = CONSTANTS.SYSTEM_PROFILE_COLLECTION;
- Db.SYSTEM_USER_COLLECTION = CONSTANTS.SYSTEM_USER_COLLECTION;
- Db.SYSTEM_COMMAND_COLLECTION = CONSTANTS.SYSTEM_COMMAND_COLLECTION;
- Db.SYSTEM_JS_COLLECTION = CONSTANTS.SYSTEM_JS_COLLECTION;
- function validateDatabaseName(databaseName) {
- if (typeof databaseName !== 'string')
- throw new error_1.MongoInvalidArgumentError('Database name must be a string');
- if (databaseName.length === 0)
- throw new error_1.MongoInvalidArgumentError('Database name cannot be the empty string');
- if (databaseName === '$external')
- return;
- const invalidChars = [' ', '.', '$', '/', '\\'];
- for (let i = 0; i < invalidChars.length; i++) {
- if (databaseName.indexOf(invalidChars[i]) !== -1)
- throw new error_1.MongoAPIError(`database names cannot contain the character '${invalidChars[i]}'`);
- }
- }
|