-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathcursor.js
More file actions
529 lines (485 loc) · 16.3 KB
/
cursor.js
File metadata and controls
529 lines (485 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import { Meteor } from 'meteor/meteor';
/**
* @class FileCursor
* @private
* @locus Anywhere
* @param {FileObj} _fileRef - Mongo–style file document or selector.
* @param {FilesCollection} _collection - FilesCollection instance.
* @summary Internal class representing a single file document (as returned from `.findOne()` or iterated via `.each()`).
*/
export class FileCursor {
constructor(_fileRef, _collection) {
if (!_fileRef) {
throw new Meteor.Error(404, 'No file reference provided');
}
this._fileRef = _fileRef;
this._collection = _collection;
// Merge file document properties into this instance.
Object.assign(this, _fileRef);
}
/**
* Remove document, client only
* @locus Client
* @param {function} [callback] - Triggered after item is removed or failed to be removed
* @returns {FileCursor}
*/
remove(callback) {
this._collection._debug('[FilesCollection] [FileCursor] [remove()]');
if (this._fileRef && this._fileRef._id) {
this._collection.remove(this._fileRef._id, callback);
} else {
callback && callback(new Meteor.Error(404, 'No such file'));
}
return this;
}
/**
* Remove document asynchronously.
* @locus Anywhere
* @returns {Promise<FileCursor>}
* @throws {Meteor.Error} If no file reference is provided.
*/
async removeAsync() {
this._collection._debug('[FilesCollection] [FileCursor] [removeAsync()]');
if (this._fileRef && this._fileRef._id) {
await this._collection.removeAsync(this._fileRef._id);
} else {
throw new Meteor.Error(404, 'No such file');
}
return this;
}
/**
* Returns a downloadable URL to the file.
* @locus Anywhere
* @param {string} [version='original'] - Name of the file’s subversion.
* @param {string} [uriBase] - Optional URI base.
* @returns {string}
*/
link(version = 'original', uriBase) {
this._collection._debug(`[FilesCollection] [FileCursor] [link(${version})]`);
if (this._fileRef && this._fileRef._id) {
return this._collection.link(this._fileRef, version, uriBase);
}
return '';
}
/**
* Returns the underlying file document (or the value of a specified property).
* @locus Anywhere
* @param {string} [property] - Name of the property to return.
* @returns {FileObj | any}
*/
get(property) {
this._collection._debug(`[FilesCollection] [FileCursor] [get(${property})]`);
if (property) {
return this._fileRef[property];
}
return this._fileRef;
}
/**
* Returns the file document wrapped in an array.
* @locus Anywhere
* @returns {Array<FileObj>}
*/
fetch() {
this._collection._debug('[FilesCollection] [FileCursor] [fetch()]');
return [this._fileRef];
}
/**
* Asynchronously returns the file document wrapped in an array.
* @locus Anywhere
* @returns {Promise<Array<FileObj>>}
*/
async fetchAsync() {
this._collection._debug('[FilesCollection] [FileCursor] [fetchAsync()]');
return [this._fileRef];
}
/**
* Returns a reactive version of the current FileCursor by merging in reactive fields.
* Useful for Blaze template helpers (e.g. `{{#with}}`).
* @locus Client
* @returns {FileCursor}
*/
with() {
this._collection._debug('[FilesCollection] [FileCursor] [with()]');
const reactiveProps = this._collection.collection.findOne(this._fileRef._id);
Object.assign(this, reactiveProps);
return this;
}
/**
* Returns a reactive version of the current FileCursor by merging in reactive fields.
* Useful for Blaze template helpers (e.g. `{{#with}}`).
* @locus Anywhere
* @returns {Promise<FileCursor>}
*/
async withAsync() {
this._collection._debug('[FilesCollection] [FileCursor] [withAsync()]');
const reactiveProps = await this._collection.collection.findOneAsync(this._fileRef._id);
Object.assign(this, reactiveProps);
return this;
}
}
/**
* @class FilesCursor
* @private
* @locus Anywhere
* @param {Mongo.Selector | Mongo.ObjectID | string} _selector - Mongo–style selector for the query.
* @param {Mongo.Options} options - Query options.
* @param {FilesCollection} _collection - FilesCollection instance.
* @summary Implementation of a cursor for FilesCollection.
*/
export class FilesCursor {
constructor(_selector = {}, options = {}, _collection) {
this._collection = _collection;
this._selector = _selector;
// Initialize the current index for iteration.
this._current = -1;
// Underlying Mongo cursor.
this.cursor = this._collection.collection.find(this._selector, options);
}
/**
* Returns all matching file documents as an array.
* Alias of `.fetch()`.
* @locus Anywhere
* @returns {Array<FileObj>}
*/
get() {
this._collection._debug('[FilesCollection] [FilesCursor] [get()]');
return this.fetch();
}
/**
* Asynchronously returns all matching file documents as an array.
* @locus Anywhere
* @returns {Promise<Array<FileObj>>}
*/
async getAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [getAsync()]');
return await this.fetchAsync();
}
/**
* Returns `true` if there is a next item available.
* @locus Anywhere
* @deprecated since v3.0.0. use {@link FilesCursor#hasNextAsync} instead.
* @returns {boolean}
*/
hasNext() {
this._collection._debug('[FilesCollection] [FilesCursor] [hasNext()]');
Meteor.deprecate('FilesCursor#hasNext() is deprecated! Use `hasNextAsync` instead');
return this._current < this.count() - 1;
}
/**
* Asynchronously returns `true` if there is a next item available.
* @locus Anywhere
* @returns {Promise<boolean>}
*/
async hasNextAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [hasNextAsync()]');
const count = await this.countDocuments();
return this._current < count - 1;
}
/**
* Returns the next file document, if available.
* @locus Anywhere
* @returns {FileObj | undefined}
*/
next() {
this._collection._debug('[FilesCollection] [FilesCursor] [next()]');
const allFiles = this.fetch();
this._current++;
return allFiles[this._current];
}
/**
* Asynchronously returns the next file document, if available.
* @locus Anywhere
* @returns {Promise<FileObj | undefined>}
*/
async nextAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [nextAsync()]');
const allFiles = await this.fetchAsync();
this._current++;
return allFiles[this._current];
}
/**
* Returns `true` if there is a previous item available.
* @locus Anywhere
* @returns {boolean}
*/
hasPrevious() {
this._collection._debug('[FilesCollection] [FilesCursor] [hasPrevious()]');
return this._current > 0;
}
/**
* Asynchronously returns `true` if there is a previous item available.
* @locus Anywhere
* @returns {Promise<boolean>}
*/
async hasPreviousAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [hasPreviousAsync()]');
return this._current > 0;
}
/**
* Returns the previous file document, if available.
* @locus Anywhere
* @returns {FileObj | undefined}
*/
previous() {
this._collection._debug('[FilesCollection] [FilesCursor] [previous()]');
this._current = Math.max(this._current - 1, 0);
return this.fetch()[this._current];
}
/**
* Asynchronously returns the previous file document, if available.
* @locus Anywhere
* @returns {Promise<FileObj | undefined>}
*/
async previousAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [previousAsync()]');
this._current = Math.max(this._current - 1, 0);
const allFiles = await this.fetchAsync();
return allFiles[this._current];
}
/**
* Returns all matching file documents as an array.
* @locus Anywhere
* @returns {Array<FileObj>}
*/
fetch() {
this._collection._debug('[FilesCollection] [FilesCursor] [fetch()]');
return this.cursor.fetch() || [];
}
/**
* Asynchronously returns all matching file documents as an array.
* @locus Anywhere
* @returns {Promise<Array<FileObj>>}
*/
async fetchAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [fetchAsync()]');
return (await this.cursor.fetchAsync()) || [];
}
/**
* Returns the first file document, if available.
* @locus Anywhere
* @returns {FileObj | undefined}
*/
first() {
this._collection._debug('[FilesCollection] [FilesCursor] [first()]');
this._current = 0;
const allFiles = this.fetch();
return allFiles[this._current];
}
/**
* Asynchronously returns the first file document, if available.
* @locus Anywhere
* @returns {Promise<FileObj | undefined>}
*/
async firstAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [firstAsync()]');
this._current = 0;
const allFiles = await this.fetchAsync();
return allFiles[this._current];
}
/**
* Returns the last file document, if available.
* @locus Anywhere
* @returns {FileObj | undefined}
*/
last() {
this._collection._debug('[FilesCollection] [FilesCursor] [last()]');
const count = this.count();
this._current = count - 1;
const allFiles = this.fetch();
return count > 0 ? allFiles[this._current] : undefined;
}
/**
* Asynchronously returns the last file document, if available.
* @locus Anywhere
* @returns {Promise<FileObj | undefined>}
*/
async lastAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [lastAsync()]');
const count = await this.countDocuments();
this._current = count - 1;
const allFiles = await this.fetchAsync();
return count > 0 ? allFiles[this._current] : undefined;
}
/**
* Returns the number of file documents that match the query.
* @locus Anywhere
* @deprecated since v3.0.0. use {@link FilesCursor#countDocuments} instead.
* @returns {number}
*/
count() {
this._collection._debug('[FilesCollection] [FilesCursor] [count()]');
Meteor.deprecate('FilesCursor#count() is deprecated! Use `countDocuments` instead');
return this.cursor.count();
}
/**
* Asynchronously returns the number of file documents that match the query.
* @locus Anywhere
* @deprecated since v3.0.0. use {@link FilesCursor#countDocuments} instead.
* @returns {Promise<number>}
*/
async countAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [countAsync()]');
Meteor.deprecate('FilesCursor#countAsync() is deprecated! Use `countDocuments` instead');
return await this.cursor.countAsync();
}
/**
* Asynchronously returns the number of file documents that match the query.
* @locus Anywhere
* @param {Mongo.CountDocumentsOptions} [options] - CountDocumentsOptions
* @returns {Promise<number>}
*/
async countDocuments(options) {
this._collection._debug('[FilesCollection] [FilesCursor] [countDocuments()]');
return await this._collection.countDocuments(this._selector, options);
}
/**
* Removes all file documents that match the query.
* @locus Client
* @param {function} [callback=() => {}] - Callback with error and number of removed records.
* @returns {FilesCursor}
*/
remove(callback = () => {}) {
this._collection._debug('[FilesCollection] [FilesCursor] [remove()]');
this._collection.remove(this._selector, callback);
return this;
}
/**
* Asynchronously removes all file documents that match the query.
* @locus Anywhere
* @returns {Promise<number>}
*/
async removeAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [removeAsync()]');
return await this._collection.removeAsync(this._selector);
}
/**
* Synchronously iterates over each matching file document.
* @locus Anywhere
* @param {function} callback - Function invoked with (file, index, cursor).
* @param {Object} [context={}] - The context for the callback.
* @returns {FilesCursor}
*/
forEach(callback, context = {}) {
this._collection._debug('[FilesCollection] [FilesCursor] [forEach()]');
this.cursor.forEach(callback, context);
return this;
}
/**
* Asynchronously iterates over each matching file document.
* @locus Anywhere
* @param {function} callback - Function invoked with (file, index, cursor).
* @param {Object} [context={}] - The context for the callback.
* @returns {Promise<FilesCursor>}
*/
async forEachAsync(callback, context = {}) {
this._collection._debug('[FilesCollection] [FilesCursor] [forEachAsync()]');
await this.cursor.forEachAsync(callback, context);
return this;
}
/**
* Returns an array of FileCursor instances (one per file document).
* Useful for Blaze’s `{{#each}}` helper.
* @locus Anywhere
* @returns {Array<FileCursor>}
*/
each() {
this._collection._debug('[FilesCollection] [FilesCursor] [each()]');
return this.map((file) => new FileCursor(file, this._collection));
}
/**
* Asynchronously returns an array of FileCursor instances (one per file document).
* @locus Anywhere
* @returns {Promise<Array<FileCursor>>}
*/
async eachAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [eachAsync()]');
return await this.mapAsync((file) => new FileCursor(file, this._collection));
}
/**
* Synchronously maps a callback over all matching file documents.
* @locus Anywhere
* @param {function} callback - Function invoked with (file, index, cursor).
* @param {Object} [context={}] - The context for the callback.
* @returns {Array<any>}
*/
map(callback, context = {}) {
this._collection._debug('[FilesCollection] [FilesCursor] [map()]');
return this.cursor.map(callback, context);
}
/**
* Asynchronously maps a callback over all matching file documents.
* @locus Anywhere
* @param {function} callback - Function invoked with (file, index, cursor).
* @param {Object} [context={}] - The context for the callback.
* @returns {Promise<Array<any>>}
*/
async mapAsync(callback, context = {}) {
this._collection._debug('[FilesCollection] [FilesCursor] [mapAsync()]');
return await this.cursor.mapAsync(callback, context);
}
/**
* Returns the current file document in the cursor.
* @locus Anywhere
* @returns {FileObj | undefined}
*/
current() {
this._collection._debug('[FilesCollection] [FilesCursor] [current()]');
if (this._current < 0) {
this._current = 0;
}
return this.fetch()[this._current];
}
/**
* Asynchronously returns the current file document in the cursor.
* @locus Anywhere
* @returns {Promise<FileObj | undefined>}
*/
async currentAsync() {
this._collection._debug('[FilesCollection] [FilesCursor] [currentAsync()]');
if (this._current < 0) {
this._current = 0;
}
const files = await this.fetchAsync();
return files[this._current];
}
/**
* Watches a query and receives callbacks as the result set changes.
* @locus Anywhere
* @param {Mongo.ObserveCallbacks} callbacks - The observe callbacks.
* @returns {Meteor.LiveQueryHandle}
*/
observe(callbacks) {
this._collection._debug('[FilesCollection] [FilesCursor] [observe()]');
return this.cursor.observe(callbacks);
}
/**
* Asynchronously watches a query and receives callbacks as the result set changes.
* @locus Anywhere
* @param {Mongo.ObserveCallbacks} callbacks - The observe callbacks.
* @returns {Promise<Meteor.LiveQueryHandle>}
*/
async observeAsync(callbacks) {
this._collection._debug('[FilesCollection] [FilesCursor] [observeAsync()]');
return await this.cursor.observeAsync(callbacks);
}
/**
* Watches a query for changes (only the differences) and receives callbacks.
* @locus Anywhere
* @param {Mongo.ObserveChangesCallbacks} callbacks - The observeChanges callbacks.
* @returns {Meteor.LiveQueryHandle}
*/
observeChanges(callbacks) {
this._collection._debug('[FilesCollection] [FilesCursor] [observeChanges()]');
return this.cursor.observeChanges(callbacks);
}
/**
* Asynchronously watches a query for changes (only the differences) and receives callbacks.
* @locus Anywhere
* @param {Mongo.ObserveChangesCallbacks} callbacks - The observeChanges callbacks.
* @returns {Promise<Meteor.LiveQueryHandle>}
*/
async observeChangesAsync(callbacks) {
this._collection._debug('[FilesCollection] [FilesCursor] [observeChangesAsync()]');
return await this.cursor.observeChangesAsync(callbacks);
}
}