UNPKG

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