mongo_client.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MongoClient = exports.ServerApiVersion = void 0;
  4. const bson_1 = require("./bson");
  5. const change_stream_1 = require("./change_stream");
  6. const connection_string_1 = require("./connection_string");
  7. const db_1 = require("./db");
  8. const error_1 = require("./error");
  9. const mongo_types_1 = require("./mongo_types");
  10. const connect_1 = require("./operations/connect");
  11. const promise_provider_1 = require("./promise_provider");
  12. const utils_1 = require("./utils");
  13. /** @public */
  14. exports.ServerApiVersion = Object.freeze({
  15. v1: '1'
  16. });
  17. /** @internal */
  18. const kOptions = Symbol('options');
  19. /**
  20. * The **MongoClient** class is a class that allows for making Connections to MongoDB.
  21. * @public
  22. *
  23. * @remarks
  24. * The programmatically provided options take precedent over the URI options.
  25. *
  26. * @example
  27. * ```js
  28. * // Connect using a MongoClient instance
  29. * const MongoClient = require('mongodb').MongoClient;
  30. * const test = require('assert');
  31. * // Connection url
  32. * const url = 'mongodb://localhost:27017';
  33. * // Database Name
  34. * const dbName = 'test';
  35. * // Connect using MongoClient
  36. * const mongoClient = new MongoClient(url);
  37. * mongoClient.connect(function(err, client) {
  38. * const db = client.db(dbName);
  39. * client.close();
  40. * });
  41. * ```
  42. *
  43. * @example
  44. * ```js
  45. * // Connect using the MongoClient.connect static method
  46. * const MongoClient = require('mongodb').MongoClient;
  47. * const test = require('assert');
  48. * // Connection url
  49. * const url = 'mongodb://localhost:27017';
  50. * // Database Name
  51. * const dbName = 'test';
  52. * // Connect using MongoClient
  53. * MongoClient.connect(url, function(err, client) {
  54. * const db = client.db(dbName);
  55. * client.close();
  56. * });
  57. * ```
  58. */
  59. class MongoClient extends mongo_types_1.TypedEventEmitter {
  60. constructor(url, options) {
  61. super();
  62. this[kOptions] = (0, connection_string_1.parseOptions)(url, this, options);
  63. // eslint-disable-next-line @typescript-eslint/no-this-alias
  64. const client = this;
  65. // The internal state
  66. this.s = {
  67. url,
  68. sessions: new Set(),
  69. bsonOptions: (0, bson_1.resolveBSONOptions)(this[kOptions]),
  70. namespace: (0, utils_1.ns)('admin'),
  71. hasBeenClosed: false,
  72. get options() {
  73. return client[kOptions];
  74. },
  75. get readConcern() {
  76. return client[kOptions].readConcern;
  77. },
  78. get writeConcern() {
  79. return client[kOptions].writeConcern;
  80. },
  81. get readPreference() {
  82. return client[kOptions].readPreference;
  83. },
  84. get logger() {
  85. return client[kOptions].logger;
  86. },
  87. get isMongoClient() {
  88. return true;
  89. }
  90. };
  91. }
  92. get options() {
  93. return Object.freeze({ ...this[kOptions] });
  94. }
  95. get serverApi() {
  96. return this[kOptions].serverApi && Object.freeze({ ...this[kOptions].serverApi });
  97. }
  98. /**
  99. * Intended for APM use only
  100. * @internal
  101. */
  102. get monitorCommands() {
  103. return this[kOptions].monitorCommands;
  104. }
  105. set monitorCommands(value) {
  106. this[kOptions].monitorCommands = value;
  107. }
  108. get autoEncrypter() {
  109. return this[kOptions].autoEncrypter;
  110. }
  111. get readConcern() {
  112. return this.s.readConcern;
  113. }
  114. get writeConcern() {
  115. return this.s.writeConcern;
  116. }
  117. get readPreference() {
  118. return this.s.readPreference;
  119. }
  120. get bsonOptions() {
  121. return this.s.bsonOptions;
  122. }
  123. get logger() {
  124. return this.s.logger;
  125. }
  126. connect(callback) {
  127. if (callback && typeof callback !== 'function') {
  128. throw new error_1.MongoInvalidArgumentError('Method `connect` only accepts a callback');
  129. }
  130. return (0, utils_1.maybePromise)(callback, cb => {
  131. (0, connect_1.connect)(this, this[kOptions], err => {
  132. if (err)
  133. return cb(err);
  134. cb(undefined, this);
  135. });
  136. });
  137. }
  138. close(forceOrCallback, callback) {
  139. // There's no way to set hasBeenClosed back to false
  140. Object.defineProperty(this.s, 'hasBeenClosed', {
  141. value: true,
  142. enumerable: true,
  143. configurable: false,
  144. writable: false
  145. });
  146. if (typeof forceOrCallback === 'function') {
  147. callback = forceOrCallback;
  148. }
  149. const force = typeof forceOrCallback === 'boolean' ? forceOrCallback : false;
  150. return (0, utils_1.maybePromise)(callback, callback => {
  151. if (this.topology == null) {
  152. return callback();
  153. }
  154. // clear out references to old topology
  155. const topology = this.topology;
  156. this.topology = undefined;
  157. topology.close({ force }, error => {
  158. if (error)
  159. return callback(error);
  160. const { encrypter } = this[kOptions];
  161. if (encrypter) {
  162. return encrypter.close(this, force, error => {
  163. callback(error);
  164. });
  165. }
  166. callback();
  167. });
  168. });
  169. }
  170. /**
  171. * Create a new Db instance sharing the current socket connections.
  172. *
  173. * @param dbName - The name of the database we want to use. If not provided, use database name from connection string.
  174. * @param options - Optional settings for Db construction
  175. */
  176. db(dbName, options) {
  177. options = options !== null && options !== void 0 ? options : {};
  178. // Default to db from connection string if not provided
  179. if (!dbName) {
  180. dbName = this.options.dbName;
  181. }
  182. // Copy the options and add out internal override of the not shared flag
  183. const finalOptions = Object.assign({}, this[kOptions], options);
  184. // Return the db object
  185. const db = new db_1.Db(this, dbName, finalOptions);
  186. // Return the database
  187. return db;
  188. }
  189. static connect(url, options, callback) {
  190. if (typeof options === 'function')
  191. (callback = options), (options = {});
  192. options = options !== null && options !== void 0 ? options : {};
  193. try {
  194. // Create client
  195. const mongoClient = new MongoClient(url, options);
  196. // Execute the connect method
  197. if (callback) {
  198. return mongoClient.connect(callback);
  199. }
  200. else {
  201. return mongoClient.connect();
  202. }
  203. }
  204. catch (error) {
  205. if (callback)
  206. return callback(error);
  207. else
  208. return promise_provider_1.PromiseProvider.get().reject(error);
  209. }
  210. }
  211. startSession(options) {
  212. options = Object.assign({ explicit: true }, options);
  213. if (!this.topology) {
  214. throw new error_1.MongoNotConnectedError('MongoClient must be connected to start a session');
  215. }
  216. return this.topology.startSession(options, this.s.options);
  217. }
  218. withSession(optionsOrOperation, callback) {
  219. const options = {
  220. // Always define an owner
  221. owner: Symbol(),
  222. // If it's an object inherit the options
  223. ...(typeof optionsOrOperation === 'object' ? optionsOrOperation : {})
  224. };
  225. const withSessionCallback = typeof optionsOrOperation === 'function' ? optionsOrOperation : callback;
  226. if (withSessionCallback == null) {
  227. throw new error_1.MongoInvalidArgumentError('Missing required callback parameter');
  228. }
  229. const session = this.startSession(options);
  230. const Promise = promise_provider_1.PromiseProvider.get();
  231. return Promise.resolve()
  232. .then(() => withSessionCallback(session))
  233. .then(() => {
  234. // Do not return the result of callback
  235. })
  236. .finally(() => session.endSession());
  237. }
  238. /**
  239. * Create a new Change Stream, watching for new changes (insertions, updates,
  240. * replacements, deletions, and invalidations) in this cluster. Will ignore all
  241. * changes to system collections, as well as the local, admin, and config databases.
  242. *
  243. * @remarks
  244. * watch() accepts two generic arguments for distinct usecases:
  245. * - The first is to provide the schema that may be defined for all the data within the current cluster
  246. * - 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
  247. *
  248. * @param pipeline - An array of {@link https://docs.mongodb.com/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.
  249. * @param options - Optional settings for the command
  250. * @typeParam TSchema - Type of the data being detected by the change stream
  251. * @typeParam TChange - Type of the whole change stream document emitted
  252. */
  253. watch(pipeline = [], options = {}) {
  254. // Allow optionally not specifying a pipeline
  255. if (!Array.isArray(pipeline)) {
  256. options = pipeline;
  257. pipeline = [];
  258. }
  259. return new change_stream_1.ChangeStream(this, pipeline, (0, utils_1.resolveOptions)(this, options));
  260. }
  261. /** Return the mongo client logger */
  262. getLogger() {
  263. return this.s.logger;
  264. }
  265. }
  266. exports.MongoClient = MongoClient;
  267. //# sourceMappingURL=mongo_client.js.map