arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/mongoose/lib/collection.js
node_modules/mongoose/lib/collection.jsRaw
  1
  2/*!
  3 * Module dependencies.
  4 */
  5
  6var STATES = require('./connectionstate')
  7
  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
 19function Collection (name, conn, opts) {
 20  if (undefined === opts) opts = {};
 21  if (undefined === opts.capped) opts.capped = {};
 22
 23  opts.bufferCommands = undefined === opts.bufferCommands
 24    ? true
 25    : opts.bufferCommands;
 26
 27  if ('number' == typeof opts.capped) {
 28    opts.capped = { size: opts.capped };
 29  }
 30
 31  this.opts = opts;
 32  this.name = name;
 33  this.conn = conn;
 34  this.queue = [];
 35  this.buffer = this.opts.bufferCommands;
 36
 37  if (STATES.connected == this.conn.readyState) {
 38    this.onOpen();
 39  }
 40};
 41
 42/**
 43 * The collection name
 44 *
 45 * @api public
 46 * @property name
 47 */
 48
 49Collection.prototype.name;
 50
 51/**
 52 * The Connection instance
 53 *
 54 * @api public
 55 * @property conn
 56 */
 57
 58Collection.prototype.conn;
 59
 60/**
 61 * Called when the database connects
 62 *
 63 * @api private
 64 */
 65
 66Collection.prototype.onOpen = function () {
 67  var self = this;
 68  this.buffer = false;
 69  self.doQueue();
 70};
 71
 72/**
 73 * Called when the database disconnects
 74 *
 75 * @api private
 76 */
 77
 78Collection.prototype.onClose = function () {
 79  if (this.opts.bufferCommands) {
 80    this.buffer = true;
 81  }
 82};
 83
 84/**
 85 * Queues a method for later execution when its
 86 * database connection opens.
 87 *
 88 * @param {String} name name of the method to queue
 89 * @param {Array} args arguments to pass to the method when executed
 90 * @api private
 91 */
 92
 93Collection.prototype.addQueue = function (name, args) {
 94  this.queue.push([name, args]);
 95  return this;
 96};
 97
 98/**
 99 * Executes all queued methods and clears the queue.
100 *
101 * @api private
102 */
103
104Collection.prototype.doQueue = function () {
105  for (var i = 0, l = this.queue.length; i < l; i++){
106    this[this.queue[i][0]].apply(this, this.queue[i][1]);
107  }
108  this.queue = [];
109  return this;
110};
111
112/**
113 * Abstract method that drivers must implement.
114 */
115
116Collection.prototype.ensureIndex = function(){
117  throw new Error('Collection#ensureIndex unimplemented by driver');
118};
119
120/**
121 * Abstract method that drivers must implement.
122 */
123
124Collection.prototype.findAndModify = function(){
125  throw new Error('Collection#findAndModify unimplemented by driver');
126};
127
128/**
129 * Abstract method that drivers must implement.
130 */
131
132Collection.prototype.findOne = function(){
133  throw new Error('Collection#findOne unimplemented by driver');
134};
135
136/**
137 * Abstract method that drivers must implement.
138 */
139
140Collection.prototype.find = function(){
141  throw new Error('Collection#find unimplemented by driver');
142};
143
144/**
145 * Abstract method that drivers must implement.
146 */
147
148Collection.prototype.insert = function(){
149  throw new Error('Collection#insert unimplemented by driver');
150};
151
152/**
153 * Abstract method that drivers must implement.
154 */
155
156Collection.prototype.save = function(){
157  throw new Error('Collection#save unimplemented by driver');
158};
159
160/**
161 * Abstract method that drivers must implement.
162 */
163
164Collection.prototype.update = function(){
165  throw new Error('Collection#update unimplemented by driver');
166};
167
168/**
169 * Abstract method that drivers must implement.
170 */
171
172Collection.prototype.getIndexes = function(){
173  throw new Error('Collection#getIndexes unimplemented by driver');
174};
175
176/**
177 * Abstract method that drivers must implement.
178 */
179
180Collection.prototype.mapReduce = function(){
181  throw new Error('Collection#mapReduce unimplemented by driver');
182};
183
184/*!
185 * Module exports.
186 */
187
188module.exports = Collection;