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