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.

188 lines
3.5 KiB

  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.conn = conn;
  27. this.queue = [];
  28. this.buffer = this.opts.bufferCommands;
  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 Connection instance
  42. *
  43. * @api public
  44. * @property conn
  45. */
  46. Collection.prototype.conn;
  47. /**
  48. * Called when the database connects
  49. *
  50. * @api private
  51. */
  52. Collection.prototype.onOpen = function () {
  53. var self = this;
  54. this.buffer = false;
  55. self.doQueue();
  56. };
  57. /**
  58. * Called when the database disconnects
  59. *
  60. * @api private
  61. */
  62. Collection.prototype.onClose = function () {
  63. if (this.opts.bufferCommands) {
  64. this.buffer = true;
  65. }
  66. };
  67. /**
  68. * Queues a method for later execution when its
  69. * database connection opens.
  70. *
  71. * @param {String} name name of the method to queue
  72. * @param {Array} args arguments to pass to the method when executed
  73. * @api private
  74. */
  75. Collection.prototype.addQueue = function (name, args) {
  76. this.queue.push([name, args]);
  77. return this;
  78. };
  79. /**
  80. * Executes all queued methods and clears the queue.
  81. *
  82. * @api private
  83. */
  84. Collection.prototype.doQueue = function () {
  85. for (var i = 0, l = this.queue.length; i < l; i++){
  86. this[this.queue[i][0]].apply(this, this.queue[i][1]);
  87. }
  88. this.queue = [];
  89. return this;
  90. };
  91. /**
  92. * Abstract method that drivers must implement.
  93. */
  94. Collection.prototype.ensureIndex = function(){
  95. throw new Error('Collection#ensureIndex unimplemented by driver');
  96. };
  97. /**
  98. * Abstract method that drivers must implement.
  99. */
  100. Collection.prototype.findAndModify = function(){
  101. throw new Error('Collection#findAndModify unimplemented by driver');
  102. };
  103. /**
  104. * Abstract method that drivers must implement.
  105. */
  106. Collection.prototype.findOne = function(){
  107. throw new Error('Collection#findOne unimplemented by driver');
  108. };
  109. /**
  110. * Abstract method that drivers must implement.
  111. */
  112. Collection.prototype.find = function(){
  113. throw new Error('Collection#find unimplemented by driver');
  114. };
  115. /**
  116. * Abstract method that drivers must implement.
  117. */
  118. Collection.prototype.insert = function(){
  119. throw new Error('Collection#insert unimplemented by driver');
  120. };
  121. /**
  122. * Abstract method that drivers must implement.
  123. */
  124. Collection.prototype.save = function(){
  125. throw new Error('Collection#save unimplemented by driver');
  126. };
  127. /**
  128. * Abstract method that drivers must implement.
  129. */
  130. Collection.prototype.update = function(){
  131. throw new Error('Collection#update unimplemented by driver');
  132. };
  133. /**
  134. * Abstract method that drivers must implement.
  135. */
  136. Collection.prototype.getIndexes = function(){
  137. throw new Error('Collection#getIndexes unimplemented by driver');
  138. };
  139. /**
  140. * Abstract method that drivers must implement.
  141. */
  142. Collection.prototype.mapReduce = function(){
  143. throw new Error('Collection#mapReduce unimplemented by driver');
  144. };
  145. /*!
  146. * Module exports.
  147. */
  148. module.exports = Collection;