collection.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const MongooseCollection = require('../../collection');
  6. const MongooseError = require('../../error/mongooseError');
  7. const Collection = require('mongodb').Collection;
  8. const ObjectId = require('./objectid');
  9. const get = require('../../helpers/get');
  10. const getConstructorName = require('../../helpers/getConstructorName');
  11. const stream = require('stream');
  12. const util = require('util');
  13. /**
  14. * A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
  15. *
  16. * All methods methods from the [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver are copied and wrapped in queue management.
  17. *
  18. * @inherits Collection
  19. * @api private
  20. */
  21. function NativeCollection(name, conn, options) {
  22. this.collection = null;
  23. this.Promise = options.Promise || Promise;
  24. this.modelName = options.modelName;
  25. delete options.modelName;
  26. this._closed = false;
  27. MongooseCollection.apply(this, arguments);
  28. }
  29. /*!
  30. * Inherit from abstract Collection.
  31. */
  32. NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
  33. /**
  34. * Called when the connection opens.
  35. *
  36. * @api private
  37. */
  38. NativeCollection.prototype.onOpen = function() {
  39. const _this = this;
  40. _this.collection = _this.conn.db.collection(_this.name);
  41. MongooseCollection.prototype.onOpen.call(_this);
  42. return _this.collection;
  43. };
  44. /**
  45. * Called when the connection closes
  46. *
  47. * @api private
  48. */
  49. NativeCollection.prototype.onClose = function(force) {
  50. MongooseCollection.prototype.onClose.call(this, force);
  51. };
  52. /*!
  53. * ignore
  54. */
  55. const syncCollectionMethods = { watch: true, find: true, aggregate: true };
  56. /*!
  57. * Copy the collection methods and make them subject to queues
  58. */
  59. function iter(i) {
  60. NativeCollection.prototype[i] = function() {
  61. const collection = this.collection;
  62. const args = Array.from(arguments);
  63. const _this = this;
  64. const debug = get(_this, 'conn.base.options.debug');
  65. const lastArg = arguments[arguments.length - 1];
  66. const opId = new ObjectId();
  67. // If user force closed, queueing will hang forever. See #5664
  68. if (this.conn.$wasForceClosed) {
  69. const error = new MongooseError('Connection was force closed');
  70. if (args.length > 0 &&
  71. typeof args[args.length - 1] === 'function') {
  72. args[args.length - 1](error);
  73. return;
  74. } else {
  75. throw error;
  76. }
  77. }
  78. let _args = args;
  79. let callback = null;
  80. if (this._shouldBufferCommands() && this.buffer) {
  81. if (syncCollectionMethods[i] && typeof lastArg !== 'function') {
  82. throw new Error('Collection method ' + i + ' is synchronous');
  83. }
  84. this.conn.emit('buffer', {
  85. _id: opId,
  86. modelName: _this.modelName,
  87. collectionName: _this.name,
  88. method: i,
  89. args: args
  90. });
  91. let callback;
  92. let _args = args;
  93. let promise = null;
  94. let timeout = null;
  95. if (syncCollectionMethods[i]) {
  96. this.addQueue(() => {
  97. lastArg.call(this, null, this[i].apply(this, _args.slice(0, _args.length - 1)));
  98. }, []);
  99. } else if (typeof lastArg === 'function') {
  100. callback = function collectionOperationCallback() {
  101. if (timeout != null) {
  102. clearTimeout(timeout);
  103. }
  104. return lastArg.apply(this, arguments);
  105. };
  106. _args = args.slice(0, args.length - 1).concat([callback]);
  107. } else {
  108. promise = new this.Promise((resolve, reject) => {
  109. callback = function collectionOperationCallback(err, res) {
  110. if (timeout != null) {
  111. clearTimeout(timeout);
  112. }
  113. if (err != null) {
  114. return reject(err);
  115. }
  116. resolve(res);
  117. };
  118. _args = args.concat([callback]);
  119. this.addQueue(i, _args);
  120. });
  121. }
  122. const bufferTimeoutMS = this._getBufferTimeoutMS();
  123. timeout = setTimeout(() => {
  124. const removed = this.removeQueue(i, _args);
  125. if (removed) {
  126. const message = 'Operation `' + this.name + '.' + i + '()` buffering timed out after ' +
  127. bufferTimeoutMS + 'ms';
  128. const err = new MongooseError(message);
  129. this.conn.emit('buffer-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, error: err });
  130. callback(err);
  131. }
  132. }, bufferTimeoutMS);
  133. if (!syncCollectionMethods[i] && typeof lastArg === 'function') {
  134. this.addQueue(i, _args);
  135. return;
  136. }
  137. return promise;
  138. } else if (!syncCollectionMethods[i] && typeof lastArg === 'function') {
  139. callback = function collectionOperationCallback(err, res) {
  140. if (err != null) {
  141. _this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, error: err });
  142. } else {
  143. _this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, result: res });
  144. }
  145. return lastArg.apply(this, arguments);
  146. };
  147. _args = args.slice(0, args.length - 1).concat([callback]);
  148. }
  149. if (debug) {
  150. if (typeof debug === 'function') {
  151. debug.apply(_this,
  152. [_this.name, i].concat(args.slice(0, args.length - 1)));
  153. } else if (debug instanceof stream.Writable) {
  154. this.$printToStream(_this.name, i, args, debug);
  155. } else {
  156. const color = debug.color == null ? true : debug.color;
  157. const shell = debug.shell == null ? false : debug.shell;
  158. this.$print(_this.name, i, args, color, shell);
  159. }
  160. }
  161. this.conn.emit('operation-start', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, params: _args });
  162. try {
  163. if (collection == null) {
  164. const message = 'Cannot call `' + this.name + '.' + i + '()` before initial connection ' +
  165. 'is complete if `bufferCommands = false`. Make sure you `await mongoose.connect()` if ' +
  166. 'you have `bufferCommands = false`.';
  167. throw new MongooseError(message);
  168. }
  169. if (syncCollectionMethods[i] && typeof lastArg === 'function') {
  170. return lastArg.call(this, null, collection[i].apply(collection, _args.slice(0, _args.length - 1)));
  171. }
  172. const ret = collection[i].apply(collection, _args);
  173. if (ret != null && typeof ret.then === 'function') {
  174. return ret.then(
  175. res => {
  176. this.conn.emit('operation-end', { _id: opId, modelName: this.modelName, collectionName: this.name, method: i, result: res });
  177. return res;
  178. },
  179. err => {
  180. this.conn.emit('operation-end', { _id: opId, modelName: this.modelName, collectionName: this.name, method: i, error: err });
  181. throw err;
  182. }
  183. );
  184. }
  185. return ret;
  186. } catch (error) {
  187. // Collection operation may throw because of max bson size, catch it here
  188. // See gh-3906
  189. if (typeof lastArg === 'function') {
  190. return lastArg(error);
  191. } else {
  192. this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, error: error });
  193. throw error;
  194. }
  195. }
  196. };
  197. }
  198. for (const key of Object.getOwnPropertyNames(Collection.prototype)) {
  199. // Janky hack to work around gh-3005 until we can get rid of the mongoose
  200. // collection abstraction
  201. const descriptor = Object.getOwnPropertyDescriptor(Collection.prototype, key);
  202. // Skip properties with getters because they may throw errors (gh-8528)
  203. if (descriptor.get !== undefined) {
  204. continue;
  205. }
  206. if (typeof Collection.prototype[key] !== 'function') {
  207. continue;
  208. }
  209. iter(key);
  210. }
  211. /**
  212. * Debug print helper
  213. *
  214. * @api public
  215. * @method $print
  216. */
  217. NativeCollection.prototype.$print = function(name, i, args, color, shell) {
  218. const moduleName = color ? '\x1B[0;36mMongoose:\x1B[0m ' : 'Mongoose: ';
  219. const functionCall = [name, i].join('.');
  220. const _args = [];
  221. for (let j = args.length - 1; j >= 0; --j) {
  222. if (this.$format(args[j]) || _args.length) {
  223. _args.unshift(this.$format(args[j], color, shell));
  224. }
  225. }
  226. const params = '(' + _args.join(', ') + ')';
  227. console.info(moduleName + functionCall + params);
  228. };
  229. /**
  230. * Debug print helper
  231. *
  232. * @api public
  233. * @method $print
  234. */
  235. NativeCollection.prototype.$printToStream = function(name, i, args, stream) {
  236. const functionCall = [name, i].join('.');
  237. const _args = [];
  238. for (let j = args.length - 1; j >= 0; --j) {
  239. if (this.$format(args[j]) || _args.length) {
  240. _args.unshift(this.$format(args[j]));
  241. }
  242. }
  243. const params = '(' + _args.join(', ') + ')';
  244. stream.write(functionCall + params, 'utf8');
  245. };
  246. /**
  247. * Formatter for debug print args
  248. *
  249. * @api public
  250. * @method $format
  251. */
  252. NativeCollection.prototype.$format = function(arg, color, shell) {
  253. const type = typeof arg;
  254. if (type === 'function' || type === 'undefined') return '';
  255. return format(arg, false, color, shell);
  256. };
  257. /*!
  258. * Debug print helper
  259. */
  260. function inspectable(representation) {
  261. const ret = {
  262. inspect: function() { return representation; }
  263. };
  264. if (util.inspect.custom) {
  265. ret[util.inspect.custom] = ret.inspect;
  266. }
  267. return ret;
  268. }
  269. function map(o) {
  270. return format(o, true);
  271. }
  272. function formatObjectId(x, key) {
  273. x[key] = inspectable('ObjectId("' + x[key].toHexString() + '")');
  274. }
  275. function formatDate(x, key, shell) {
  276. if (shell) {
  277. x[key] = inspectable('ISODate("' + x[key].toUTCString() + '")');
  278. } else {
  279. x[key] = inspectable('new Date("' + x[key].toUTCString() + '")');
  280. }
  281. }
  282. function format(obj, sub, color, shell) {
  283. if (obj && typeof obj.toBSON === 'function') {
  284. obj = obj.toBSON();
  285. }
  286. if (obj == null) {
  287. return obj;
  288. }
  289. const clone = require('../../helpers/clone');
  290. let x = clone(obj, { transform: false });
  291. const constructorName = getConstructorName(x);
  292. if (constructorName === 'Binary') {
  293. x = 'BinData(' + x.sub_type + ', "' + x.toString('base64') + '")';
  294. } else if (constructorName === 'ObjectID') {
  295. x = inspectable('ObjectId("' + x.toHexString() + '")');
  296. } else if (constructorName === 'Date') {
  297. x = inspectable('new Date("' + x.toUTCString() + '")');
  298. } else if (constructorName === 'Object') {
  299. const keys = Object.keys(x);
  300. const numKeys = keys.length;
  301. let key;
  302. for (let i = 0; i < numKeys; ++i) {
  303. key = keys[i];
  304. if (x[key]) {
  305. let error;
  306. if (typeof x[key].toBSON === 'function') {
  307. try {
  308. // `session.toBSON()` throws an error. This means we throw errors
  309. // in debug mode when using transactions, see gh-6712. As a
  310. // workaround, catch `toBSON()` errors, try to serialize without
  311. // `toBSON()`, and rethrow if serialization still fails.
  312. x[key] = x[key].toBSON();
  313. } catch (_error) {
  314. error = _error;
  315. }
  316. }
  317. const _constructorName = getConstructorName(x[key]);
  318. if (_constructorName === 'Binary') {
  319. x[key] = 'BinData(' + x[key].sub_type + ', "' +
  320. x[key].buffer.toString('base64') + '")';
  321. } else if (_constructorName === 'Object') {
  322. x[key] = format(x[key], true);
  323. } else if (_constructorName === 'ObjectID') {
  324. formatObjectId(x, key);
  325. } else if (_constructorName === 'Date') {
  326. formatDate(x, key, shell);
  327. } else if (_constructorName === 'ClientSession') {
  328. x[key] = inspectable('ClientSession("' +
  329. get(x[key], 'id.id.buffer', '').toString('hex') + '")');
  330. } else if (Array.isArray(x[key])) {
  331. x[key] = x[key].map(map);
  332. } else if (error != null) {
  333. // If there was an error with `toBSON()` and the object wasn't
  334. // already converted to a string representation, rethrow it.
  335. // Open to better ideas on how to handle this.
  336. throw error;
  337. }
  338. }
  339. }
  340. }
  341. if (sub) {
  342. return x;
  343. }
  344. return util.
  345. inspect(x, false, 10, color).
  346. replace(/\n/g, '').
  347. replace(/\s{2,}/g, ' ');
  348. }
  349. /**
  350. * Retrieves information about this collections indexes.
  351. *
  352. * @param {Function} callback
  353. * @method getIndexes
  354. * @api public
  355. */
  356. NativeCollection.prototype.getIndexes = NativeCollection.prototype.indexInformation;
  357. /*!
  358. * Module exports.
  359. */
  360. module.exports = NativeCollection;