collection.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*!
  2. * Module dependencies.
  3. */
  4. var EventEmitter = require('events').EventEmitter;
  5. var STATES = require('./connectionstate');
  6. /**
  7. * Abstract Collection constructor
  8. *
  9. * This is the base class that drivers inherit from and implement.
  10. *
  11. * @param {String} name name of the collection
  12. * @param {Connection} conn A MongooseConnection instance
  13. * @param {Object} opts optional collection options
  14. * @api public
  15. */
  16. function Collection(name, conn, opts) {
  17. if (opts === void 0) {
  18. opts = {};
  19. }
  20. if (opts.capped === void 0) {
  21. opts.capped = {};
  22. }
  23. opts.bufferCommands = undefined === opts.bufferCommands
  24. ? true
  25. : opts.bufferCommands;
  26. if (typeof opts.capped === 'number') {
  27. opts.capped = {size: opts.capped};
  28. }
  29. this.opts = opts;
  30. this.name = name;
  31. this.collectionName = name;
  32. this.conn = conn;
  33. this.queue = [];
  34. this.buffer = this.opts.bufferCommands;
  35. this.emitter = new EventEmitter();
  36. if (STATES.connected === this.conn.readyState) {
  37. this.onOpen();
  38. }
  39. }
  40. /**
  41. * The collection name
  42. *
  43. * @api public
  44. * @property name
  45. */
  46. Collection.prototype.name;
  47. /**
  48. * The collection name
  49. *
  50. * @api public
  51. * @property collectionName
  52. */
  53. Collection.prototype.collectionName;
  54. /**
  55. * The Connection instance
  56. *
  57. * @api public
  58. * @property conn
  59. */
  60. Collection.prototype.conn;
  61. /**
  62. * Called when the database connects
  63. *
  64. * @api private
  65. */
  66. Collection.prototype.onOpen = function() {
  67. this.buffer = false;
  68. this.doQueue();
  69. };
  70. /**
  71. * Called when the database disconnects
  72. *
  73. * @api private
  74. */
  75. Collection.prototype.onClose = function() {
  76. if (this.opts.bufferCommands) {
  77. this.buffer = true;
  78. }
  79. };
  80. /**
  81. * Queues a method for later execution when its
  82. * database connection opens.
  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.addQueue = function(name, args) {
  89. this.queue.push([name, args]);
  90. return this;
  91. };
  92. /**
  93. * Executes all queued methods and clears the queue.
  94. *
  95. * @api private
  96. */
  97. Collection.prototype.doQueue = function() {
  98. for (var i = 0, l = this.queue.length; i < l; i++) {
  99. if (typeof this.queue[i][0] === 'function') {
  100. this.queue[i][0].apply(this, this.queue[i][1]);
  101. } else {
  102. this[this.queue[i][0]].apply(this, this.queue[i][1]);
  103. }
  104. }
  105. this.queue = [];
  106. var _this = this;
  107. process.nextTick(function() {
  108. _this.emitter.emit('queue');
  109. });
  110. return this;
  111. };
  112. /**
  113. * Abstract method that drivers must implement.
  114. */
  115. Collection.prototype.ensureIndex = function() {
  116. throw new Error('Collection#ensureIndex unimplemented by driver');
  117. };
  118. /**
  119. * Abstract method that drivers must implement.
  120. */
  121. Collection.prototype.findAndModify = function() {
  122. throw new Error('Collection#findAndModify unimplemented by driver');
  123. };
  124. /**
  125. * Abstract method that drivers must implement.
  126. */
  127. Collection.prototype.findOne = function() {
  128. throw new Error('Collection#findOne unimplemented by driver');
  129. };
  130. /**
  131. * Abstract method that drivers must implement.
  132. */
  133. Collection.prototype.find = function() {
  134. throw new Error('Collection#find unimplemented by driver');
  135. };
  136. /**
  137. * Abstract method that drivers must implement.
  138. */
  139. Collection.prototype.insert = function() {
  140. throw new Error('Collection#insert unimplemented by driver');
  141. };
  142. /**
  143. * Abstract method that drivers must implement.
  144. */
  145. Collection.prototype.save = function() {
  146. throw new Error('Collection#save unimplemented by driver');
  147. };
  148. /**
  149. * Abstract method that drivers must implement.
  150. */
  151. Collection.prototype.update = function() {
  152. throw new Error('Collection#update unimplemented by driver');
  153. };
  154. /**
  155. * Abstract method that drivers must implement.
  156. */
  157. Collection.prototype.getIndexes = function() {
  158. throw new Error('Collection#getIndexes unimplemented by driver');
  159. };
  160. /**
  161. * Abstract method that drivers must implement.
  162. */
  163. Collection.prototype.mapReduce = function() {
  164. throw new Error('Collection#mapReduce unimplemented by driver');
  165. };
  166. /*!
  167. * Module exports.
  168. */
  169. module.exports = Collection;