1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const EventEmitter = require('events').EventEmitter;
|
8 | const STATES = require('./connectionState');
|
9 | const immediate = require('./helpers/immediate');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function 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 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | Collection.prototype.name;
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | Collection.prototype.collectionName;
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Collection.prototype.conn;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | Collection.prototype.onOpen = function() {
|
74 | this.buffer = false;
|
75 | immediate(() => this.doQueue());
|
76 | };
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | Collection.prototype.onClose = function() {};
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | Collection.prototype.addQueue = function(name, args) {
|
96 | this.queue.push([name, args]);
|
97 | return this;
|
98 | };
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | Collection.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 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | Collection.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 |
|
141 |
|
142 |
|
143 | Collection.prototype.ensureIndex = function() {
|
144 | throw new Error('Collection#ensureIndex unimplemented by driver');
|
145 | };
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | Collection.prototype.createIndex = function() {
|
152 | throw new Error('Collection#createIndex unimplemented by driver');
|
153 | };
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | Collection.prototype.findAndModify = function() {
|
160 | throw new Error('Collection#findAndModify unimplemented by driver');
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | Collection.prototype.findOneAndUpdate = function() {
|
168 | throw new Error('Collection#findOneAndUpdate unimplemented by driver');
|
169 | };
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | Collection.prototype.findOneAndDelete = function() {
|
176 | throw new Error('Collection#findOneAndDelete unimplemented by driver');
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | Collection.prototype.findOneAndReplace = function() {
|
184 | throw new Error('Collection#findOneAndReplace unimplemented by driver');
|
185 | };
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | Collection.prototype.findOne = function() {
|
192 | throw new Error('Collection#findOne unimplemented by driver');
|
193 | };
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | Collection.prototype.find = function() {
|
200 | throw new Error('Collection#find unimplemented by driver');
|
201 | };
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | Collection.prototype.insert = function() {
|
208 | throw new Error('Collection#insert unimplemented by driver');
|
209 | };
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | Collection.prototype.insertOne = function() {
|
216 | throw new Error('Collection#insertOne unimplemented by driver');
|
217 | };
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | Collection.prototype.insertMany = function() {
|
224 | throw new Error('Collection#insertMany unimplemented by driver');
|
225 | };
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | Collection.prototype.save = function() {
|
232 | throw new Error('Collection#save unimplemented by driver');
|
233 | };
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | Collection.prototype.updateOne = function() {
|
240 | throw new Error('Collection#updateOne unimplemented by driver');
|
241 | };
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | Collection.prototype.updateMany = function() {
|
248 | throw new Error('Collection#updateMany unimplemented by driver');
|
249 | };
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | Collection.prototype.deleteOne = function() {
|
256 | throw new Error('Collection#deleteOne unimplemented by driver');
|
257 | };
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | Collection.prototype.deleteMany = function() {
|
264 | throw new Error('Collection#deleteMany unimplemented by driver');
|
265 | };
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 | Collection.prototype.getIndexes = function() {
|
272 | throw new Error('Collection#getIndexes unimplemented by driver');
|
273 | };
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | Collection.prototype.watch = function() {
|
280 | throw new Error('Collection#watch unimplemented by driver');
|
281 | };
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | Collection.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 |
|
302 |
|
303 |
|
304 | Collection.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 |
|
325 |
|
326 |
|
327 | module.exports = Collection;
|