/
Model.js
715 lines (608 loc) · 26.4 KB
/
Model.js
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/// <reference path="../nodelib/node.js"/>
/// <reference path="Instance.js"/>
/// <reference path="Database.js"/>
/// <reference path="utils/validation.js"/>
var _ = require('lodash'),
async = require('async'),
debug = require('debug')('iridium:Model'),
ObjectID = require('mongodb').ObjectID,
EventEmitter = require('events').EventEmitter,
Concoction = require('concoction'),
Database = require('./Database'),
Instance = require('./Instance'),
validate = require('./utils/validation'),
NoOpCache = require('./caches/NoOpCache');
(require.modules || {}).Model = module.exports = Model;
function Model(database, collection, schema, options) {
/// <summary>Creates a new model around the specified database</summary>
/// <param name="database" type="Database">The database wrapper on which this model will operate</param>
/// <param name="collection" type="String">The name of the database collection in which objects of this type are stored</param>
/// <param name="schema" type="Object">A JSON representation of the database schema</param>
/// <param name="options" type="Object">Additional options configuring the behaviour of this model's instances</param>
if(!(this instanceof Model)) return new Model(database, collection, schema, options);
EventEmitter.call(this);
this.on('error', function(err) {
debug('error: %s', err.message);
});
if (!options) options = {};
_.defaults(options, {
hooks: {},
preprocessors: [
new Concoction.Rename({
_id: 'id'
}),
new Concoction.Convert({
id: {
apply: function (value) { return (value && value.id) ? new ObjectID(value.id).toHexString() : value; },
reverse: function (value) { return value ? ObjectID.createFromHexString(value) : undefined; }
}
})
]
});
Object.defineProperty(this, 'preprocessor', {
value: new Concoction(options.preprocessors)
});
Object.defineProperty(this, 'collection', {
get: function () { return database.db.collection(collection); },
set: function(value) { collection = value; },
enumerable: false
});
Object.defineProperty(this, 'database', {
get: function() { return database; },
set: function(value) { database = value; },
enumerable: false
});
Object.defineProperty(this, 'schema', {
get: function() { return schema; },
enumerable: false
});
Object.defineProperty(this, 'options', {
get: function () { return options; },
enumerable: false
});
Object.defineProperty(this, 'isModel', {
value: true,
enumerable: false
});
var _instance = null;
Object.defineProperty(this, 'Instance', {
get: function() { return _instance = _instance || Instance.forModel(this) },
enumerable: false
});
var _cache = options.cache;
Object.defineProperty(this, 'cache', {
get: function() { return _cache = _cache || new NoOpCache(); },
enumerable: false
})
var extraValidators = [];
for(var i = 0; i < database.plugins.length; i++) {
if(database.plugins[i].validate)
extraValidators.push(database.plugins[i].validate);
}
Object.defineProperty(this, 'extraValidators', {
get: function() { return extraValidators; }
});
for(var i = 0; i < database.plugins.length; i++) {
if(database.plugins[i].newModel) database.plugins[i].newModel.call(this, database, collection, schema, options);
}
}
Model.prototype.__proto__ = EventEmitter.prototype;
Model.prototype.fromSource = function(document) {
/// <summary>Applies the model's preprocessors to convert the document from the source</summary>
/// <param name="document" type="Object">The object to apply the preprocessors to</param>
this.preprocessor.apply(document);
};
Model.prototype.toSource = function(document) {
/// <summary>Applies the model's preprocessors to convert the document from the source</summary>
/// <param name="document" type="Object">The object to apply the preprocessors to</param>
this.preprocessor.reverse(document);
};
Model.prototype.uniqueConditions = function(document) {
/// <summary>Gets a set of MongoDB conditions which uniquely identify the given document for this model in source form/summary>
/// <param name="document" type="Object">The document to find the unique conditions for</param>
/// <returns type="Object"/>
var testDoc = _.cloneDeep(document);
this.toSource(testDoc);
var conditions = {
_id: testDoc._id
};
return conditions;
};
Model.prototype.downstreamID = function(id) {
/// <signature>
/// <summary>Gets the downstream _id field's identifier after preprocessing</summary>
/// <returns type="String"/>
/// </signature>
/// <signature>
/// <summary>Gets the set of conditions representing the downstream _id field for the given downstream identifier</summary>
/// <param name="id" type="Mixed">The identifier to create the conditions from</param>
/// <returns type="Object"/>
/// </signature>
var test_doc = {
_id: true
};
this.fromSource(test_doc);
var _id = null;
for(var k in test_doc)
if(test_doc[k] === true) {
_id = k;
break;
}
if(id) {
var conditions = {};
conditions[_id] = id;
return conditions;
} else return _id;
};
Model.prototype.wrap = function (document, isNew) {
/// <signature>
/// <summary>Wraps the given database object in this model's Instance wrapper</summary>
/// <param name="document" type="Object">The database object to be wrapped by this model</param>
/// <returns type="Instance"/>
/// </signature>
/// <signature>
/// <summary>Wraps the given database object in this model's Instance wrapper</summary>
/// <param name="document" type="Object">The database object to be wrapped by this model</param>
/// <param name="isNew" type="Boolean">Whether or not this instance is new (not in the database)</param>
/// <returns type="Instance"/>
/// </signature>
return new this.Instance(document, isNew);
};
Model.prototype.onRetrieved = function(conditions, results, callback, wrapper, options) {
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Object">The object retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Array" elementType="Object">The objects retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Object">The object retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///<param name="wrapper" type="Function">A function which converts the retrieved objects prior to submission</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Array" elementType="Object">The objects retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///<param name="wrapper" type="Function">A function which converts the retrieved objects prior to submission</param>
///</signature>
///<signature>
///<summary>Handles any post-receive hooks and the wrapping of objects from the database</summary>
///<param name="conditions" type="Object">The conditions which resulted in the object being retrieved</param>
///<param name="results" type="Array" elementType="Object">The objects retrieved from the database</param>
///<param name="callback" type="Function">The function to be called once the objects have been wrapped</param>
///<param name="wrapper" type="Function">A function which converts the retrieved objects prior to submission</param>
///<param name="options" type="Object">A set of options determining how to handle the retrieved object</param>
///</signature>
var $ = this;
wrapper = wrapper || this.wrap.bind(this);
options = options || {};
_.defaults(options, {
wrap: true,
cache: true
});
var returnArray = Array.isArray(results);
if(!returnArray) results = [results];
function doHook(hook, target, next) {
if(!hook) return next();
if(hook.length === 0) {
try {
hook.call(target);
return next();
} catch(err) {
return next(err);
}
} else {
hook.call(target, next);
}
}
async.parallel(_.map(results, function(target) {
return (function(done) {
doHook(this.options.hooks.retrieved, target, (function(err) {
if(err) {
this.emit('error', err);
return done(err);
}
this.emit('retrieved', target);
var cacheDoc = _.cloneDeep(target);
var wrapped = options.wrap ? wrapper(target) : this.fromSource(target);
doHook(this.options.hooks.ready, wrapped, (function(err) {
if(err) {
this.emit('error', err);
return done(err);
}
this.emit('ready', wrapped);
if(options.cache)
return this.cache.store(conditions, cacheDoc, function() {
return done(null, wrapped);
});
else
return done(null, wrapped);
}).bind(this));
}).bind(this));
}).bind(this);
}, this), function(err, output) {
if(err) return callback(err);
if(returnArray)
return callback(err, output);
else
return callback(err, output[0]);
});
};
Model.prototype.onCreating = function(document, callback) {
function doHook(hook, target, args, next) {
if(!hook) return next();
if(hook.length === 0) {
try {
hook.apply(target, args);
return next();
} catch(err) {
return next(err);
}
} else {
args.push(next);
hook.apply(target, args);
}
}
doHook(this.options.hooks.creating || this.options.hooks.beforeCreate, document, [], (function(err) {
if(err) this.emit('error', err);
else this.emit('creating', document);
return callback(err);
}).bind(this));
};
Model.prototype.onSaving = function(instance, changes, callback) {
function doHook(hook, target, args, next) {
if(!hook) return next();
if(hook.length === 0) {
try {
hook.apply(target, args);
return next();
} catch(err) {
return next(err);
}
} else {
args.push(next);
hook.apply(target, args);
}
}
doHook(this.options.hooks.saving, instance, [changes], (function(err) {
if(err) this.emit('error', err);
else this.emit('saving', instance, changes);
return callback(err);
}).bind(this));
};
Model.prototype.find = function (conditions, options, callback) {
/// <signature>
/// <summary>Gets all objects in the collection.</summary>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds all occurences in the collection with an _id field matching the given conditions.</summary>
/// <param name="conditions" type="Mixed">The _id field of the object to locate</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds all occurences in the collection which match the given conditions.</summary>
/// <param name="conditions" type="Object">The conditions which will be used to select matches</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Gets all objects in the collection.</summary>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds all occurences in the collection with an _id field matching the given conditions.</summary>
/// <param name="conditions" type="Mixed">The _id field of the object to locate</param>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds all occurences in the collection which match the given conditions.</summary>
/// <param name="conditions" type="Object">The conditions which will be used to select matches</param>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
var args = Array.prototype.splice.call(arguments, 0);
conditions = null;
options = null;
for(var i = 0; i < args.length; i++) {
if('function' == typeof args[i])
callback = args[i];
else if(!conditions)
conditions = args[i];
else options = args[i];
}
conditions = conditions || {};
options = options || {};
_.defaults(options, {
wrap: true
});
var $ = this;
if (!_.isPlainObject(conditions)) conditions = this.downstreamID(conditions);
this.toSource(conditions);
this.collection.find(conditions).toArray((function (err, results) {
if (err) {
this.emit('error', err);
return callback(err);
}
if (!results) return callback(null, null);
return $.onRetrieved(conditions, results, callback, null, { wrap: options.wrap, cache: false });
}).bind(this));
};
Model.prototype.findOne = Model.prototype.get = function (conditions, options, callback) {
/// <signature>
/// <summary>Gets a single object from the collection.</summary>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds the first occurence in the collection with an _id field matching the given conditions.</summary>
/// <param name="conditions" type="Mixed">The _id field of the object to locate</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds the first occurence in the collection which matches the given conditions.</summary>
/// <param name="conditions" type="Object">The conditions which will be used to select matches</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Gets a single object from the collection.</summary>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds the first occurence in the collection with an _id field matching the given conditions.</summary>
/// <param name="conditions" type="Mixed">The _id field of the object to locate</param>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
/// <signature>
/// <summary>Finds the first occurence in the collection which matches the given conditions.</summary>
/// <param name="conditions" type="Object">The conditions which will be used to select matches</param>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called with the results once they have been retrieved.</param>
/// </signature>
var args = Array.prototype.splice.call(arguments, 0);
conditions = null;
options = null;
for(var i = 0; i < args.length; i++) {
if('function' == typeof args[i])
callback = args[i];
else if(!conditions)
conditions = args[i];
else options = args[i];
}
conditions = conditions || {};
options = options || {};
_.defaults(options, {
wrap: true,
cache: true
});
if (!_.isPlainObject(conditions)) conditions = this.downstreamID(conditions);
this.toSource(conditions);
var fromDB = (function() {
this.collection.findOne(conditions, (function (err, results) {
if (err) {
this.emit('error', err);
return callback(err);
}
if (!results) return callback(null, null);
return this.onRetrieved(conditions, results, callback, null, { wrap: options.wrap, cache: options.cache });
}).bind(this));
}).bind(this);
if(options.cache && this.cache && this.cache.valid(conditions))
this.cache.fetch(conditions, (function(err, doc) {
if(!err && doc)
return this.onRetrieved(conditions, doc, callback, null, { wrap: options.wrap, cache: false });
else
return fromDB();
}).bind(this));
else
return fromDB();
};
Model.prototype.insert = Model.prototype.create = function (object, options, callback) {
/// <signature>
/// <summary>Inserts the given object into the database</summary>
/// <param name="object" type="Object">The properties to set on the newly created object</param>
/// </signature>
/// <signature>
/// <summary>Inserts the given object into the database</summary>
/// <param name="object" type="Array" elementType="Object">An array of objects representing the properties to set on the newly created objects</param>
/// </signature>
/// <signature>
/// <summary>Inserts the given object into the database</summary>
/// <param name="object" type="Object">The properties to set on the newly created object</param>
/// <param name="callback" type="Function">A function to be called once the object has been created</param>
/// </signature>
/// <signature>
/// <summary>Inserts the given object into the database</summary>
/// <param name="object" type="Array" elementType="Object">An array of objects representing the properties to set on the newly created objects</param>
/// <param name="callback" type="Function">A function to be called once the objects have been created</param>
/// </signature>
/// <summary>Inserts the given object into the database</summary>
/// <param name="object" type="Object">The properties to set on the newly created object</param>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called once the object has been created</param>
/// </signature>
/// <signature>
/// <summary>Inserts the given object into the database</summary>
/// <param name="object" type="Array" elementType="Object">An array of objects representing the properties to set on the newly created objects</param>
/// <param name="options" type="Object">Options dictating how Iridium handles this request</param>
/// <param name="callback" type="Function">A function to be called once the objects have been created</param>
/// </signature>
var $ = this;
var returnArray = true;
if(!callback) {
callback = options;
options = options || {};
}
_.defaults(options, {
wrap: true
});
if(!Array.isArray(object)) {
object = [object];
returnArray = false;
}
var end = (function(err, results) {
if (err) this.emit('error', err);
if(!callback) return;
if(!results) return callback(err);
if(!Array.isArray(results)) results = [results];
if(returnArray) return callback(err, results);
return callback(err, results[0]);
}).bind(this);
var prepComplete = (function(err, prepped) {
if(err) return end(err);
$.collection.insert(prepped, { w: callback ? 1 : 0 }, (function(err, inserted) {
if (err) return end(err);
if(callback)
return $.onRetrieved(null, inserted, end, null, { wrap: options.wrap, cache: options.cache });
return end();
}).bind(this));
}).bind(this);
async.parallel(_.map(object, function(obj) {
return function(done) {
var postHook = function(err) {
if(err) return done(err);
// Validate the object
var validation = validate($.schema, obj, undefined, $.extraValidators);
if(!validation.passed) return done(validation.toError());
// Transform the object
$.toSource(obj);
return done(null, obj);
}
$.onCreating(obj, postHook);
}
}, this), prepComplete);
};
Model.prototype.update = function (conditions, changes, callback) {
/// <signature>
/// <summary>Updates all documents in the collection which match the specified conditions - making the requested changes</summary>
/// <param name="conditions" type="Object">The conditions used to select objects to be updated</param>
/// <param name="changes" type="Object">The changes to be made to objects in the collection</param>
/// </signature>
/// <signature>
/// <summary>Updates all documents in the collection which match the specified conditions - making the requested changes</summary>
/// <param name="conditions" type="Object">The conditions used to select objects to be updated</param>
/// <param name="changes" type="Object">The changes to be made to objects in the collection</param>
/// <param name="callback" type="Function">A function to be called once the update has completed</param>
/// </signature>
this.toSource(conditions);
this.collection.update(conditions, changes, { w: callback ? 1 : 0, multi: true }, (function(err, modified) {
if (err) this.emit('error', err);
return callback(err, modified);
}).bind(this));
};
Model.prototype.count = function (conditions, callback) {
/// <signature>
/// <summary>Counts the number of documents in the collection</summary>
/// <param name="callback" type="Function">A function to be called once the documents have been counted.</param>
/// </signature>
/// <signature>
/// <summary>Counts the number of documents in the collection which match the given conditions</summary>
/// <param name="conditions" type="Object">The conditions on which to match documents for counting</param>
/// <param name="callback" type="Function">A function to be called once the documents have been counted.</param>
/// </signature>
if (!callback) {
callback = conditions;
conditions = {};
}
this.toSource(conditions);
this.collection.count(conditions, (function(err, count) {
if (err) this.emit('error', err);
return callback(err, count);
}).bind(this));
};
Model.prototype.remove = function (conditions, callback) {
/// <signature>
/// <summary>Removes all objects in the collection.</summary>
/// <param name="callback" type="Function">A function to be called once all objects have been removed.</param>
/// </signature>
/// <signature>
/// <summary>Removes all occurences in the collection with an _id field matching the given condition value.</summary>
/// <param name="conditions" type="Mixed">The _id field of the object to locate</param>
/// <param name="callback" type="Function">A function to be called once all objects have been removed.</param>
/// </signature>
/// <signature>
/// <summary>Removes all occurences in the collection which match the given conditions.</summary>
/// <param name="conditions" type="Object">The conditions which will be used to select matches</param>
/// <param name="callback" type="Function">A function to be called once all objects have been removed.</param>
/// </signature>
if (!callback) {
callback = conditions;
conditions = {};
}
if (!_.isPlainObject(conditions)) conditions = this.downstreamID(conditions);
this.toSource(conditions);
if(this.cache && this.cache.valid(conditions))
this.cache.drop(conditions, function() {
this.collection.remove(conditions, { w: callback ? 1 : 0 }, (function(err, modified) {
if (err) this.emit('error', err);
return callback(err, modified);
}).bind(this));
});
else
this.collection.remove(conditions, { w: callback ? 1 : 0 }, (function(err, modified) {
if (err) this.emit('error', err);
return callback(err, modified);
}).bind(this));
};
Model.prototype.aggregate = function () {
/// <signature>
/// <summary>Allows you to execute aggregation queries using MongoDB's aggregation framework</summary>
/// <param name="chain" type="Array" elementType="Object">The aggregation toolchain to run</param>
/// <param name="callback" type="Function">A function to be called once aggregation has been completed</param>
/// </signature>
this.collection.aggregate.apply(this.collection, arguments);
};
Model.prototype.ensureIndex = function (spec, options, callback) {
/// <signature>
/// <summary>Ensures that an index is present on the database collection</summary>
/// <param name="spec" type="Object">The description of the index to be created</param>
/// <param name="callback" type="Function">A function to be called once the operation has completed</param>
/// </signature>
/// <signature>
/// <summary>Ensures that an index is present on the database collection</summary>
/// <param name="spec" type="Object">The description of the index to be created</param>
/// <param name="options" type="Object">Options to be applied to the index (unique, sparse, background etc.)</param>
/// <param name="callback" type="Function">A function to be called once the operation has completed</param>
/// </signature>
if (!callback) {
callback = options;
options = {};
}
this.collection.ensureIndex(spec, options, callback);
};
Model.prototype.setupIndexes = function (callback) {
/// <signature>
/// <summary>Configures indexes defined for this model</summary>
/// </signature>
/// <signature>
/// <summary>Configures indexes defined for this model</summary>
/// <param name="callback" type="Function">A function to be called once all index creations have been requested</param>
/// </signature>
var $ = this;
callback = callback || function (err) {
if (err) throw err;
};
if (!this.options.indexes || this.options.indexes.length === 0) return callback(null, []);
var i = 0;
var errors = [];
var results = [];
var next = function (err, result) {
errors.push(err);
results.push(result);
if (++i === $.options.indexes.length) return callback(errors.length > 0 ? errors.pop() : null, results);
$.ensureIndex($.options.indexes[i][0], $.options.indexes[i][1], next);
};
$.ensureIndex($.options.indexes[i][0], $.options.indexes[i][1], next);
};