Ohm-Management - Projektarbeit B-ME
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.

collection.js 5.3KB

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