collection.js 6.3 KB

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