collection.js 12 KB

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