collection.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. 'use strict';
  2. /*!
  3. * Module dependencies.
  4. */
  5. const EventEmitter = require('events').EventEmitter;
  6. const STATES = require('./connectionstate');
  7. const immediate = require('./helpers/immediate');
  8. /**
  9. * Abstract Collection constructor
  10. *
  11. * This is the base class that drivers inherit from and implement.
  12. *
  13. * @param {String} name name of the collection
  14. * @param {Connection} conn A MongooseConnection instance
  15. * @param {Object} opts optional collection options
  16. * @api public
  17. */
  18. function Collection(name, conn, opts) {
  19. if (opts === void 0) {
  20. opts = {};
  21. }
  22. if (opts.capped === void 0) {
  23. opts.capped = {};
  24. }
  25. if (typeof opts.capped === 'number') {
  26. opts.capped = { size: opts.capped };
  27. }
  28. this.opts = opts;
  29. this.name = name;
  30. this.collectionName = name;
  31. this.conn = conn;
  32. this.queue = [];
  33. this.buffer = true;
  34. this.emitter = new EventEmitter();
  35. if (STATES.connected === this.conn.readyState) {
  36. this.onOpen();
  37. }
  38. }
  39. /**
  40. * The collection name
  41. *
  42. * @api public
  43. * @property name
  44. */
  45. Collection.prototype.name;
  46. /**
  47. * The collection name
  48. *
  49. * @api public
  50. * @property collectionName
  51. */
  52. Collection.prototype.collectionName;
  53. /**
  54. * The Connection instance
  55. *
  56. * @api public
  57. * @property conn
  58. */
  59. Collection.prototype.conn;
  60. /**
  61. * Called when the database connects
  62. *
  63. * @api private
  64. */
  65. Collection.prototype.onOpen = function() {
  66. this.buffer = false;
  67. immediate(() => this.doQueue());
  68. };
  69. /**
  70. * Called when the database disconnects
  71. *
  72. * @api private
  73. */
  74. Collection.prototype.onClose = function() {};
  75. /**
  76. * Queues a method for later execution when its
  77. * database connection opens.
  78. *
  79. * @param {String} name name of the method to queue
  80. * @param {Array} args arguments to pass to the method when executed
  81. * @api private
  82. */
  83. Collection.prototype.addQueue = function(name, args) {
  84. this.queue.push([name, args]);
  85. return this;
  86. };
  87. /**
  88. * Removes a queued method
  89. *
  90. * @param {String} name name of the method to queue
  91. * @param {Array} args arguments to pass to the method when executed
  92. * @api private
  93. */
  94. Collection.prototype.removeQueue = function(name, args) {
  95. const index = this.queue.findIndex(v => v[0] === name && v[1] === args);
  96. if (index === -1) {
  97. return false;
  98. }
  99. this.queue.splice(index, 1);
  100. return true;
  101. };
  102. /**
  103. * Executes all queued methods and clears the queue.
  104. *
  105. * @api private
  106. */
  107. Collection.prototype.doQueue = function() {
  108. for (const method of this.queue) {
  109. if (typeof method[0] === 'function') {
  110. method[0].apply(this, method[1]);
  111. } else {
  112. this[method[0]].apply(this, method[1]);
  113. }
  114. }
  115. this.queue = [];
  116. const _this = this;
  117. immediate(function() {
  118. _this.emitter.emit('queue');
  119. });
  120. return this;
  121. };
  122. /**
  123. * Abstract method that drivers must implement.
  124. */
  125. Collection.prototype.ensureIndex = function() {
  126. throw new Error('Collection#ensureIndex unimplemented by driver');
  127. };
  128. /**
  129. * Abstract method that drivers must implement.
  130. */
  131. Collection.prototype.createIndex = function() {
  132. throw new Error('Collection#createIndex unimplemented by driver');
  133. };
  134. /**
  135. * Abstract method that drivers must implement.
  136. */
  137. Collection.prototype.findAndModify = function() {
  138. throw new Error('Collection#findAndModify unimplemented by driver');
  139. };
  140. /**
  141. * Abstract method that drivers must implement.
  142. */
  143. Collection.prototype.findOneAndUpdate = function() {
  144. throw new Error('Collection#findOneAndUpdate unimplemented by driver');
  145. };
  146. /**
  147. * Abstract method that drivers must implement.
  148. */
  149. Collection.prototype.findOneAndDelete = function() {
  150. throw new Error('Collection#findOneAndDelete unimplemented by driver');
  151. };
  152. /**
  153. * Abstract method that drivers must implement.
  154. */
  155. Collection.prototype.findOneAndReplace = function() {
  156. throw new Error('Collection#findOneAndReplace unimplemented by driver');
  157. };
  158. /**
  159. * Abstract method that drivers must implement.
  160. */
  161. Collection.prototype.findOne = function() {
  162. throw new Error('Collection#findOne unimplemented by driver');
  163. };
  164. /**
  165. * Abstract method that drivers must implement.
  166. */
  167. Collection.prototype.find = function() {
  168. throw new Error('Collection#find unimplemented by driver');
  169. };
  170. /**
  171. * Abstract method that drivers must implement.
  172. */
  173. Collection.prototype.insert = function() {
  174. throw new Error('Collection#insert unimplemented by driver');
  175. };
  176. /**
  177. * Abstract method that drivers must implement.
  178. */
  179. Collection.prototype.insertOne = function() {
  180. throw new Error('Collection#insertOne unimplemented by driver');
  181. };
  182. /**
  183. * Abstract method that drivers must implement.
  184. */
  185. Collection.prototype.insertMany = function() {
  186. throw new Error('Collection#insertMany unimplemented by driver');
  187. };
  188. /**
  189. * Abstract method that drivers must implement.
  190. */
  191. Collection.prototype.save = function() {
  192. throw new Error('Collection#save unimplemented by driver');
  193. };
  194. /**
  195. * Abstract method that drivers must implement.
  196. */
  197. Collection.prototype.update = function() {
  198. throw new Error('Collection#update unimplemented by driver');
  199. };
  200. /**
  201. * Abstract method that drivers must implement.
  202. */
  203. Collection.prototype.getIndexes = function() {
  204. throw new Error('Collection#getIndexes unimplemented by driver');
  205. };
  206. /**
  207. * Abstract method that drivers must implement.
  208. */
  209. Collection.prototype.mapReduce = function() {
  210. throw new Error('Collection#mapReduce unimplemented by driver');
  211. };
  212. /**
  213. * Abstract method that drivers must implement.
  214. */
  215. Collection.prototype.watch = function() {
  216. throw new Error('Collection#watch unimplemented by driver');
  217. };
  218. /*!
  219. * ignore
  220. */
  221. Collection.prototype._shouldBufferCommands = function _shouldBufferCommands() {
  222. const opts = this.opts;
  223. if (opts.bufferCommands != null) {
  224. return opts.bufferCommands;
  225. }
  226. if (opts && opts.schemaUserProvidedOptions != null && opts.schemaUserProvidedOptions.bufferCommands != null) {
  227. return opts.schemaUserProvidedOptions.bufferCommands;
  228. }
  229. return this.conn._shouldBufferCommands();
  230. };
  231. /*!
  232. * ignore
  233. */
  234. Collection.prototype._getBufferTimeoutMS = function _getBufferTimeoutMS() {
  235. const conn = this.conn;
  236. const opts = this.opts;
  237. if (opts.bufferTimeoutMS != null) {
  238. return opts.bufferTimeoutMS;
  239. }
  240. if (opts && opts.schemaUserProvidedOptions != null && opts.schemaUserProvidedOptions.bufferTimeoutMS != null) {
  241. return opts.schemaUserProvidedOptions.bufferTimeoutMS;
  242. }
  243. if (conn.config.bufferTimeoutMS != null) {
  244. return conn.config.bufferTimeoutMS;
  245. }
  246. if (conn.base != null && conn.base.get('bufferTimeoutMS') != null) {
  247. return conn.base.get('bufferTimeoutMS');
  248. }
  249. return 10000;
  250. };
  251. /*!
  252. * Module exports.
  253. */
  254. module.exports = Collection;