UNPKG

6.86 kBJavaScriptView Raw
1'use strict';
2
3/*!
4 * Module dependencies.
5 */
6
7const EventEmitter = require('events').EventEmitter;
8const STATES = require('./connectionState');
9const immediate = require('./helpers/immediate');
10
11/**
12 * Abstract Collection constructor
13 *
14 * This is the base class that drivers inherit from and implement.
15 *
16 * @param {String} name name of the collection
17 * @param {Connection} conn A MongooseConnection instance
18 * @param {Object} [opts] optional collection options
19 * @api public
20 */
21
22function Collection(name, conn, opts) {
23 if (opts === void 0) {
24 opts = {};
25 }
26
27 this.opts = opts;
28 this.name = name;
29 this.collectionName = name;
30 this.conn = conn;
31 this.queue = [];
32 this.buffer = true;
33 this.emitter = new EventEmitter();
34
35 if (STATES.connected === this.conn.readyState) {
36 this.onOpen();
37 }
38}
39
40/**
41 * The collection name
42 *
43 * @api public
44 * @property name
45 */
46
47Collection.prototype.name;
48
49/**
50 * The collection name
51 *
52 * @api public
53 * @property collectionName
54 */
55
56Collection.prototype.collectionName;
57
58/**
59 * The Connection instance
60 *
61 * @api public
62 * @property conn
63 */
64
65Collection.prototype.conn;
66
67/**
68 * Called when the database connects
69 *
70 * @api private
71 */
72
73Collection.prototype.onOpen = function() {
74 this.buffer = false;
75 immediate(() => this.doQueue());
76};
77
78/**
79 * Called when the database disconnects
80 *
81 * @api private
82 */
83
84Collection.prototype.onClose = function() {};
85
86/**
87 * Queues a method for later execution when its
88 * database connection opens.
89 *
90 * @param {String} name name of the method to queue
91 * @param {Array} args arguments to pass to the method when executed
92 * @api private
93 */
94
95Collection.prototype.addQueue = function(name, args) {
96 this.queue.push([name, args]);
97 return this;
98};
99
100/**
101 * Removes a queued method
102 *
103 * @param {String} name name of the method to queue
104 * @param {Array} args arguments to pass to the method when executed
105 * @api private
106 */
107
108Collection.prototype.removeQueue = function(name, args) {
109 const index = this.queue.findIndex(v => v[0] === name && v[1] === args);
110 if (index === -1) {
111 return false;
112 }
113 this.queue.splice(index, 1);
114 return true;
115};
116
117/**
118 * Executes all queued methods and clears the queue.
119 *
120 * @api private
121 */
122
123Collection.prototype.doQueue = function() {
124 for (const method of this.queue) {
125 if (typeof method[0] === 'function') {
126 method[0].apply(this, method[1]);
127 } else {
128 this[method[0]].apply(this, method[1]);
129 }
130 }
131 this.queue = [];
132 const _this = this;
133 immediate(function() {
134 _this.emitter.emit('queue');
135 });
136 return this;
137};
138
139/**
140 * Abstract method that drivers must implement.
141 */
142
143Collection.prototype.ensureIndex = function() {
144 throw new Error('Collection#ensureIndex unimplemented by driver');
145};
146
147/**
148 * Abstract method that drivers must implement.
149 */
150
151Collection.prototype.createIndex = function() {
152 throw new Error('Collection#createIndex unimplemented by driver');
153};
154
155/**
156 * Abstract method that drivers must implement.
157 */
158
159Collection.prototype.findAndModify = function() {
160 throw new Error('Collection#findAndModify unimplemented by driver');
161};
162
163/**
164 * Abstract method that drivers must implement.
165 */
166
167Collection.prototype.findOneAndUpdate = function() {
168 throw new Error('Collection#findOneAndUpdate unimplemented by driver');
169};
170
171/**
172 * Abstract method that drivers must implement.
173 */
174
175Collection.prototype.findOneAndDelete = function() {
176 throw new Error('Collection#findOneAndDelete unimplemented by driver');
177};
178
179/**
180 * Abstract method that drivers must implement.
181 */
182
183Collection.prototype.findOneAndReplace = function() {
184 throw new Error('Collection#findOneAndReplace unimplemented by driver');
185};
186
187/**
188 * Abstract method that drivers must implement.
189 */
190
191Collection.prototype.findOne = function() {
192 throw new Error('Collection#findOne unimplemented by driver');
193};
194
195/**
196 * Abstract method that drivers must implement.
197 */
198
199Collection.prototype.find = function() {
200 throw new Error('Collection#find unimplemented by driver');
201};
202
203/**
204 * Abstract method that drivers must implement.
205 */
206
207Collection.prototype.insert = function() {
208 throw new Error('Collection#insert unimplemented by driver');
209};
210
211/**
212 * Abstract method that drivers must implement.
213 */
214
215Collection.prototype.insertOne = function() {
216 throw new Error('Collection#insertOne unimplemented by driver');
217};
218
219/**
220 * Abstract method that drivers must implement.
221 */
222
223Collection.prototype.insertMany = function() {
224 throw new Error('Collection#insertMany unimplemented by driver');
225};
226
227/**
228 * Abstract method that drivers must implement.
229 */
230
231Collection.prototype.save = function() {
232 throw new Error('Collection#save unimplemented by driver');
233};
234
235/**
236 * Abstract method that drivers must implement.
237 */
238
239Collection.prototype.updateOne = function() {
240 throw new Error('Collection#updateOne unimplemented by driver');
241};
242
243/**
244 * Abstract method that drivers must implement.
245 */
246
247Collection.prototype.updateMany = function() {
248 throw new Error('Collection#updateMany unimplemented by driver');
249};
250
251/**
252 * Abstract method that drivers must implement.
253 */
254
255Collection.prototype.deleteOne = function() {
256 throw new Error('Collection#deleteOne unimplemented by driver');
257};
258
259/**
260 * Abstract method that drivers must implement.
261 */
262
263Collection.prototype.deleteMany = function() {
264 throw new Error('Collection#deleteMany unimplemented by driver');
265};
266
267/**
268 * Abstract method that drivers must implement.
269 */
270
271Collection.prototype.getIndexes = function() {
272 throw new Error('Collection#getIndexes unimplemented by driver');
273};
274
275/**
276 * Abstract method that drivers must implement.
277 */
278
279Collection.prototype.watch = function() {
280 throw new Error('Collection#watch unimplemented by driver');
281};
282
283/*!
284 * ignore
285 */
286
287Collection.prototype._shouldBufferCommands = function _shouldBufferCommands() {
288 const opts = this.opts;
289
290 if (opts.bufferCommands != null) {
291 return opts.bufferCommands;
292 }
293 if (opts && opts.schemaUserProvidedOptions != null && opts.schemaUserProvidedOptions.bufferCommands != null) {
294 return opts.schemaUserProvidedOptions.bufferCommands;
295 }
296
297 return this.conn._shouldBufferCommands();
298};
299
300/*!
301 * ignore
302 */
303
304Collection.prototype._getBufferTimeoutMS = function _getBufferTimeoutMS() {
305 const conn = this.conn;
306 const opts = this.opts;
307
308 if (opts.bufferTimeoutMS != null) {
309 return opts.bufferTimeoutMS;
310 }
311 if (opts && opts.schemaUserProvidedOptions != null && opts.schemaUserProvidedOptions.bufferTimeoutMS != null) {
312 return opts.schemaUserProvidedOptions.bufferTimeoutMS;
313 }
314 if (conn.config.bufferTimeoutMS != null) {
315 return conn.config.bufferTimeoutMS;
316 }
317 if (conn.base != null && conn.base.get('bufferTimeoutMS') != null) {
318 return conn.base.get('bufferTimeoutMS');
319 }
320 return 10000;
321};
322
323/*!
324 * Module exports.
325 */
326
327module.exports = Collection;