You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
3.9 KiB

  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. this[this.queue[i][0]].apply(this, this.queue[i][1]);
  100. }
  101. this.queue = [];
  102. var _this = this;
  103. process.nextTick(function() {
  104. _this.emitter.emit('queue');
  105. });
  106. return this;
  107. };
  108. /**
  109. * Abstract method that drivers must implement.
  110. */
  111. Collection.prototype.ensureIndex = function() {
  112. throw new Error('Collection#ensureIndex unimplemented by driver');
  113. };
  114. /**
  115. * Abstract method that drivers must implement.
  116. */
  117. Collection.prototype.findAndModify = function() {
  118. throw new Error('Collection#findAndModify unimplemented by driver');
  119. };
  120. /**
  121. * Abstract method that drivers must implement.
  122. */
  123. Collection.prototype.findOne = function() {
  124. throw new Error('Collection#findOne unimplemented by driver');
  125. };
  126. /**
  127. * Abstract method that drivers must implement.
  128. */
  129. Collection.prototype.find = function() {
  130. throw new Error('Collection#find unimplemented by driver');
  131. };
  132. /**
  133. * Abstract method that drivers must implement.
  134. */
  135. Collection.prototype.insert = function() {
  136. throw new Error('Collection#insert unimplemented by driver');
  137. };
  138. /**
  139. * Abstract method that drivers must implement.
  140. */
  141. Collection.prototype.save = function() {
  142. throw new Error('Collection#save unimplemented by driver');
  143. };
  144. /**
  145. * Abstract method that drivers must implement.
  146. */
  147. Collection.prototype.update = function() {
  148. throw new Error('Collection#update unimplemented by driver');
  149. };
  150. /**
  151. * Abstract method that drivers must implement.
  152. */
  153. Collection.prototype.getIndexes = function() {
  154. throw new Error('Collection#getIndexes unimplemented by driver');
  155. };
  156. /**
  157. * Abstract method that drivers must implement.
  158. */
  159. Collection.prototype.mapReduce = function() {
  160. throw new Error('Collection#mapReduce unimplemented by driver');
  161. };
  162. /*!
  163. * Module exports.
  164. */
  165. module.exports = Collection;