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 | 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 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | Collection.prototype.name;
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | Collection.prototype.collectionName;
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | Collection.prototype.conn;
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | Collection.prototype.onOpen = function() {
|
85 | this.buffer = false;
|
86 | immediate(() => this.doQueue());
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | Collection.prototype.onClose = function(force) {
|
96 | if (this.opts.bufferCommands && !force) {
|
97 | this.buffer = true;
|
98 | }
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | Collection.prototype.addQueue = function(name, args) {
|
111 | this.queue.push([name, args]);
|
112 | return this;
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | Collection.prototype.doQueue = function() {
|
122 | for (let i = 0, l = this.queue.length; i < l; i++) {
|
123 | if (typeof this.queue[i][0] === 'function') {
|
124 | this.queue[i][0].apply(this, this.queue[i][1]);
|
125 | } else {
|
126 | this[this.queue[i][0]].apply(this, this.queue[i][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 |
|
139 |
|
140 |
|
141 | Collection.prototype.ensureIndex = function() {
|
142 | throw new Error('Collection#ensureIndex unimplemented by driver');
|
143 | };
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | Collection.prototype.createIndex = function() {
|
150 | throw new Error('Collection#ensureIndex unimplemented by driver');
|
151 | };
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | Collection.prototype.findAndModify = function() {
|
158 | throw new Error('Collection#findAndModify unimplemented by driver');
|
159 | };
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | Collection.prototype.findOneAndUpdate = function() {
|
166 | throw new Error('Collection#findOneAndUpdate unimplemented by driver');
|
167 | };
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | Collection.prototype.findOneAndDelete = function() {
|
174 | throw new Error('Collection#findOneAndDelete unimplemented by driver');
|
175 | };
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 | Collection.prototype.findOneAndReplace = function() {
|
182 | throw new Error('Collection#findOneAndReplace unimplemented by driver');
|
183 | };
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | Collection.prototype.findOne = function() {
|
190 | throw new Error('Collection#findOne unimplemented by driver');
|
191 | };
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | Collection.prototype.find = function() {
|
198 | throw new Error('Collection#find unimplemented by driver');
|
199 | };
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | Collection.prototype.insert = function() {
|
206 | throw new Error('Collection#insert unimplemented by driver');
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | Collection.prototype.insertOne = function() {
|
214 | throw new Error('Collection#insertOne unimplemented by driver');
|
215 | };
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | Collection.prototype.insertMany = function() {
|
222 | throw new Error('Collection#insertMany unimplemented by driver');
|
223 | };
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | Collection.prototype.save = function() {
|
230 | throw new Error('Collection#save unimplemented by driver');
|
231 | };
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | Collection.prototype.update = function() {
|
238 | throw new Error('Collection#update unimplemented by driver');
|
239 | };
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | Collection.prototype.getIndexes = function() {
|
246 | throw new Error('Collection#getIndexes unimplemented by driver');
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 | Collection.prototype.mapReduce = function() {
|
254 | throw new Error('Collection#mapReduce unimplemented by driver');
|
255 | };
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | Collection.prototype.watch = function() {
|
262 | throw new Error('Collection#watch unimplemented by driver');
|
263 | };
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | module.exports = Collection;
|