collection.js 3.7 KB

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