db.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Db = void 0;
  4. const admin_1 = require("./admin");
  5. const bson_1 = require("./bson");
  6. const change_stream_1 = require("./change_stream");
  7. const collection_1 = require("./collection");
  8. const CONSTANTS = require("./constants");
  9. const aggregation_cursor_1 = require("./cursor/aggregation_cursor");
  10. const list_collections_cursor_1 = require("./cursor/list_collections_cursor");
  11. const run_command_cursor_1 = require("./cursor/run_command_cursor");
  12. const error_1 = require("./error");
  13. const add_user_1 = require("./operations/add_user");
  14. const collections_1 = require("./operations/collections");
  15. const create_collection_1 = require("./operations/create_collection");
  16. const drop_1 = require("./operations/drop");
  17. const execute_operation_1 = require("./operations/execute_operation");
  18. const indexes_1 = require("./operations/indexes");
  19. const profiling_level_1 = require("./operations/profiling_level");
  20. const remove_user_1 = require("./operations/remove_user");
  21. const rename_1 = require("./operations/rename");
  22. const run_command_1 = require("./operations/run_command");
  23. const set_profiling_level_1 = require("./operations/set_profiling_level");
  24. const stats_1 = require("./operations/stats");
  25. const read_concern_1 = require("./read_concern");
  26. const read_preference_1 = require("./read_preference");
  27. const utils_1 = require("./utils");
  28. const write_concern_1 = require("./write_concern");
  29. // Allowed parameters
  30. const DB_OPTIONS_ALLOW_LIST = [
  31. 'writeConcern',
  32. 'readPreference',
  33. 'readPreferenceTags',
  34. 'native_parser',
  35. 'forceServerObjectId',
  36. 'pkFactory',
  37. 'serializeFunctions',
  38. 'raw',
  39. 'authSource',
  40. 'ignoreUndefined',
  41. 'readConcern',
  42. 'retryMiliSeconds',
  43. 'numberOfRetries',
  44. 'useBigInt64',
  45. 'promoteBuffers',
  46. 'promoteLongs',
  47. 'bsonRegExp',
  48. 'enableUtf8Validation',
  49. 'promoteValues',
  50. 'compression',
  51. 'retryWrites'
  52. ];
  53. /**
  54. * The **Db** class is a class that represents a MongoDB Database.
  55. * @public
  56. *
  57. * @example
  58. * ```ts
  59. * import { MongoClient } from 'mongodb';
  60. *
  61. * interface Pet {
  62. * name: string;
  63. * kind: 'dog' | 'cat' | 'fish';
  64. * }
  65. *
  66. * const client = new MongoClient('mongodb://localhost:27017');
  67. * const db = client.db();
  68. *
  69. * // Create a collection that validates our union
  70. * await db.createCollection<Pet>('pets', {
  71. * validator: { $expr: { $in: ['$kind', ['dog', 'cat', 'fish']] } }
  72. * })
  73. * ```
  74. */
  75. class Db {
  76. /**
  77. * Creates a new Db instance
  78. *
  79. * @param client - The MongoClient for the database.
  80. * @param databaseName - The name of the database this instance represents.
  81. * @param options - Optional settings for Db construction
  82. */
  83. constructor(client, databaseName, options) {
  84. options = options ?? {};
  85. // Filter the options
  86. options = (0, utils_1.filterOptions)(options, DB_OPTIONS_ALLOW_LIST);
  87. // Ensure we have a valid db name
  88. validateDatabaseName(databaseName);
  89. // Internal state of the db object
  90. this.s = {
  91. // Options
  92. options,
  93. // Unpack read preference
  94. readPreference: read_preference_1.ReadPreference.fromOptions(options),
  95. // Merge bson options
  96. bsonOptions: (0, bson_1.resolveBSONOptions)(options, client),
  97. // Set up the primary key factory or fallback to ObjectId
  98. pkFactory: options?.pkFactory ?? utils_1.DEFAULT_PK_FACTORY,
  99. // ReadConcern
  100. readConcern: read_concern_1.ReadConcern.fromOptions(options),
  101. writeConcern: write_concern_1.WriteConcern.fromOptions(options),
  102. // Namespace
  103. namespace: new utils_1.MongoDBNamespace(databaseName)
  104. };
  105. this.client = client;
  106. }
  107. get databaseName() {
  108. return this.s.namespace.db;
  109. }
  110. // Options
  111. get options() {
  112. return this.s.options;
  113. }
  114. /**
  115. * Check if a secondary can be used (because the read preference is *not* set to primary)
  116. */
  117. get secondaryOk() {
  118. return this.s.readPreference?.preference !== 'primary' || false;
  119. }
  120. get readConcern() {
  121. return this.s.readConcern;
  122. }
  123. /**
  124. * The current readPreference of the Db. If not explicitly defined for
  125. * this Db, will be inherited from the parent MongoClient
  126. */
  127. get readPreference() {
  128. if (this.s.readPreference == null) {
  129. return this.client.readPreference;
  130. }
  131. return this.s.readPreference;
  132. }
  133. get bsonOptions() {
  134. return this.s.bsonOptions;
  135. }
  136. // get the write Concern
  137. get writeConcern() {
  138. return this.s.writeConcern;
  139. }
  140. get namespace() {
  141. return this.s.namespace.toString();
  142. }
  143. /**
  144. * Create a new collection on a server with the specified options. Use this to create capped collections.
  145. * More information about command options available at https://www.mongodb.com/docs/manual/reference/command/create/
  146. *
  147. * @param name - The name of the collection to create
  148. * @param options - Optional settings for the command
  149. */
  150. async createCollection(name, options) {
  151. return (0, execute_operation_1.executeOperation)(this.client, new create_collection_1.CreateCollectionOperation(this, name, (0, utils_1.resolveOptions)(this, options)));
  152. }
  153. /**
  154. * Execute a command
  155. *
  156. * @remarks
  157. * This command does not inherit options from the MongoClient.
  158. *
  159. * The driver will ensure the following fields are attached to the command sent to the server:
  160. * - `lsid` - sourced from an implicit session or options.session
  161. * - `$readPreference` - defaults to primary or can be configured by options.readPreference
  162. * - `$db` - sourced from the name of this database
  163. *
  164. * If the client has a serverApi setting:
  165. * - `apiVersion`
  166. * - `apiStrict`
  167. * - `apiDeprecationErrors`
  168. *
  169. * When in a transaction:
  170. * - `readConcern` - sourced from readConcern set on the TransactionOptions
  171. * - `writeConcern` - sourced from writeConcern set on the TransactionOptions
  172. *
  173. * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value.
  174. *
  175. * @param command - The command to run
  176. * @param options - Optional settings for the command
  177. */
  178. async command(command, options) {
  179. // Intentionally, we do not inherit options from parent for this operation.
  180. return (0, execute_operation_1.executeOperation)(this.client, new run_command_1.RunCommandOperation(this, command, options));
  181. }
  182. /**
  183. * Execute an aggregation framework pipeline against the database, needs MongoDB \>= 3.6
  184. *
  185. * @param pipeline - An array of aggregation stages to be executed
  186. * @param options - Optional settings for the command
  187. */
  188. aggregate(pipeline = [], options) {
  189. return new aggregation_cursor_1.AggregationCursor(this.client, this.s.namespace, pipeline, (0, utils_1.resolveOptions)(this, options));
  190. }
  191. /** Return the Admin db instance */
  192. admin() {
  193. return new admin_1.Admin(this);
  194. }
  195. /**
  196. * Returns a reference to a MongoDB Collection. If it does not exist it will be created implicitly.
  197. *
  198. * @param name - the collection name we wish to access.
  199. * @returns return the new Collection instance
  200. */
  201. collection(name, options = {}) {
  202. if (typeof options === 'function') {
  203. throw new error_1.MongoInvalidArgumentError('The callback form of this helper has been removed.');
  204. }
  205. return new collection_1.Collection(this, name, (0, utils_1.resolveOptions)(this, options));
  206. }
  207. /**
  208. * Get all the db statistics.
  209. *
  210. * @param options - Optional settings for the command
  211. */
  212. async stats(options) {
  213. return (0, execute_operation_1.executeOperation)(this.client, new stats_1.DbStatsOperation(this, (0, utils_1.resolveOptions)(this, options)));
  214. }
  215. listCollections(filter = {}, options = {}) {
  216. return new list_collections_cursor_1.ListCollectionsCursor(this, filter, (0, utils_1.resolveOptions)(this, options));
  217. }
  218. /**
  219. * Rename a collection.
  220. *
  221. * @remarks
  222. * This operation does not inherit options from the MongoClient.
  223. *
  224. * @param fromCollection - Name of current collection to rename
  225. * @param toCollection - New name of of the collection
  226. * @param options - Optional settings for the command
  227. */
  228. async renameCollection(fromCollection, toCollection, options) {
  229. // Intentionally, we do not inherit options from parent for this operation.
  230. return (0, execute_operation_1.executeOperation)(this.client, new rename_1.RenameOperation(this.collection(fromCollection), toCollection, { ...options, new_collection: true, readPreference: read_preference_1.ReadPreference.primary }));
  231. }
  232. /**
  233. * Drop a collection from the database, removing it permanently. New accesses will create a new collection.
  234. *
  235. * @param name - Name of collection to drop
  236. * @param options - Optional settings for the command
  237. */
  238. async dropCollection(name, options) {
  239. return (0, execute_operation_1.executeOperation)(this.client, new drop_1.DropCollectionOperation(this, name, (0, utils_1.resolveOptions)(this, options)));
  240. }
  241. /**
  242. * Drop a database, removing it permanently from the server.
  243. *
  244. * @param options - Optional settings for the command
  245. */
  246. async dropDatabase(options) {
  247. return (0, execute_operation_1.executeOperation)(this.client, new drop_1.DropDatabaseOperation(this, (0, utils_1.resolveOptions)(this, options)));
  248. }
  249. /**
  250. * Fetch all collections for the current db.
  251. *
  252. * @param options - Optional settings for the command
  253. */
  254. async collections(options) {
  255. return (0, execute_operation_1.executeOperation)(this.client, new collections_1.CollectionsOperation(this, (0, utils_1.resolveOptions)(this, options)));
  256. }
  257. /**
  258. * Creates an index on the db and collection.
  259. *
  260. * @param name - Name of the collection to create the index on.
  261. * @param indexSpec - Specify the field to index, or an index specification
  262. * @param options - Optional settings for the command
  263. */
  264. async createIndex(name, indexSpec, options) {
  265. return (0, execute_operation_1.executeOperation)(this.client, new indexes_1.CreateIndexOperation(this, name, indexSpec, (0, utils_1.resolveOptions)(this, options)));
  266. }
  267. /**
  268. * Add a user to the database
  269. *
  270. * @param username - The username for the new user
  271. * @param passwordOrOptions - An optional password for the new user, or the options for the command
  272. * @param options - Optional settings for the command
  273. * @deprecated Use the createUser command in `db.command()` instead.
  274. * @see https://www.mongodb.com/docs/manual/reference/command/createUser/
  275. */
  276. async addUser(username, passwordOrOptions, options) {
  277. options =
  278. options != null && typeof options === 'object'
  279. ? options
  280. : passwordOrOptions != null && typeof passwordOrOptions === 'object'
  281. ? passwordOrOptions
  282. : undefined;
  283. const password = typeof passwordOrOptions === 'string' ? passwordOrOptions : undefined;
  284. return (0, execute_operation_1.executeOperation)(this.client, new add_user_1.AddUserOperation(this, username, password, (0, utils_1.resolveOptions)(this, options)));
  285. }
  286. /**
  287. * Remove a user from a database
  288. *
  289. * @param username - The username to remove
  290. * @param options - Optional settings for the command
  291. */
  292. async removeUser(username, options) {
  293. return (0, execute_operation_1.executeOperation)(this.client, new remove_user_1.RemoveUserOperation(this, username, (0, utils_1.resolveOptions)(this, options)));
  294. }
  295. /**
  296. * Set the current profiling level of MongoDB
  297. *
  298. * @param level - The new profiling level (off, slow_only, all).
  299. * @param options - Optional settings for the command
  300. */
  301. async setProfilingLevel(level, options) {
  302. return (0, execute_operation_1.executeOperation)(this.client, new set_profiling_level_1.SetProfilingLevelOperation(this, level, (0, utils_1.resolveOptions)(this, options)));
  303. }
  304. /**
  305. * Retrieve the current profiling Level for MongoDB
  306. *
  307. * @param options - Optional settings for the command
  308. */
  309. async profilingLevel(options) {
  310. return (0, execute_operation_1.executeOperation)(this.client, new profiling_level_1.ProfilingLevelOperation(this, (0, utils_1.resolveOptions)(this, options)));
  311. }
  312. /**
  313. * Retrieves this collections index info.
  314. *
  315. * @param name - The name of the collection.
  316. * @param options - Optional settings for the command
  317. */
  318. async indexInformation(name, options) {
  319. return (0, execute_operation_1.executeOperation)(this.client, new indexes_1.IndexInformationOperation(this, name, (0, utils_1.resolveOptions)(this, options)));
  320. }
  321. /**
  322. * Create a new Change Stream, watching for new changes (insertions, updates,
  323. * replacements, deletions, and invalidations) in this database. Will ignore all
  324. * changes to system collections.
  325. *
  326. * @remarks
  327. * watch() accepts two generic arguments for distinct use cases:
  328. * - The first is to provide the schema that may be defined for all the collections within this database
  329. * - The second is to override the shape of the change stream document entirely, if it is not provided the type will default to ChangeStreamDocument of the first argument
  330. *
  331. * @param pipeline - An array of {@link https://www.mongodb.com/docs/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
  332. * @param options - Optional settings for the command
  333. * @typeParam TSchema - Type of the data being detected by the change stream
  334. * @typeParam TChange - Type of the whole change stream document emitted
  335. */
  336. watch(pipeline = [], options = {}) {
  337. // Allow optionally not specifying a pipeline
  338. if (!Array.isArray(pipeline)) {
  339. options = pipeline;
  340. pipeline = [];
  341. }
  342. return new change_stream_1.ChangeStream(this, pipeline, (0, utils_1.resolveOptions)(this, options));
  343. }
  344. /**
  345. * A low level cursor API providing basic driver functionality:
  346. * - ClientSession management
  347. * - ReadPreference for server selection
  348. * - Running getMores automatically when a local batch is exhausted
  349. *
  350. * @param command - The command that will start a cursor on the server.
  351. * @param options - Configurations for running the command, bson options will apply to getMores
  352. */
  353. runCursorCommand(command, options) {
  354. return new run_command_cursor_1.RunCommandCursor(this, command, options);
  355. }
  356. }
  357. Db.SYSTEM_NAMESPACE_COLLECTION = CONSTANTS.SYSTEM_NAMESPACE_COLLECTION;
  358. Db.SYSTEM_INDEX_COLLECTION = CONSTANTS.SYSTEM_INDEX_COLLECTION;
  359. Db.SYSTEM_PROFILE_COLLECTION = CONSTANTS.SYSTEM_PROFILE_COLLECTION;
  360. Db.SYSTEM_USER_COLLECTION = CONSTANTS.SYSTEM_USER_COLLECTION;
  361. Db.SYSTEM_COMMAND_COLLECTION = CONSTANTS.SYSTEM_COMMAND_COLLECTION;
  362. Db.SYSTEM_JS_COLLECTION = CONSTANTS.SYSTEM_JS_COLLECTION;
  363. exports.Db = Db;
  364. // TODO(NODE-3484): Refactor into MongoDBNamespace
  365. // Validate the database name
  366. function validateDatabaseName(databaseName) {
  367. if (typeof databaseName !== 'string')
  368. throw new error_1.MongoInvalidArgumentError('Database name must be a string');
  369. if (databaseName.length === 0)
  370. throw new error_1.MongoInvalidArgumentError('Database name cannot be the empty string');
  371. if (databaseName === '$external')
  372. return;
  373. const invalidChars = [' ', '.', '$', '/', '\\'];
  374. for (let i = 0; i < invalidChars.length; i++) {
  375. if (databaseName.indexOf(invalidChars[i]) !== -1)
  376. throw new error_1.MongoAPIError(`database names cannot contain the character '${invalidChars[i]}'`);
  377. }
  378. }
  379. //# sourceMappingURL=db.js.map