/
model.js
1040 lines (904 loc) · 26 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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
// References
// ---
// https://github.om/jsantell/backbone-promised/blob/master/index.js
var _ = require('lodash');
var Bluebird = require('bluebird');
var Backbone = require('backbone');
var moment = require('moment');
var debug = require('./debug');
var MuniError = require('./error');
var Mixins = require('./mixins');
// Cache ensured indexes
var INDEXED = {};
module.exports = Backbone.Model.extend({
/**
* _.mergeWith with a customizer function
* Do not merge arrays and empty objects
* Arrays always want to be overwritten explicitly (empty or not)
* Objects want to be overwritten explicitly (when empty)
*
* @return {Object}
*/
_mergeSafe: function(object, other) {
return _.mergeWith(object, other, function mergeDeep(objValue, srcValue) {
if (_.isArray(objValue)) {
// If array, do not deep merge
return objValue;
} else if (_.isPlainObject(objValue) && _.isEmpty(objValue)) {
// If empty object, do not merge
return objValue;
} else if (_.isPlainObject(objValue)) {
return _.mergeWith(objValue, srcValue, mergeDeep);
}
return objValue;
});
},
/**
* Responsible for setting attributes after a database call
* Takes the mongodb response and calls the Backbone success method
*
* @param {Object} options
* @return {Function}
*/
_wrapResponse: function(options) {
return function(err, resp) {
if (err) {
options.error(err);
} else {
options.success(resp);
}
};
},
/**
* Remove attributes
*
* Does not work for objects embedded inside arrays
*
* @param {Object} attrs
* @param {Object} attrsToRemove
*/
_removeAttributes: function(attrs, attrsToRemove) {
_.forEach(attrs, function(val, key) {
// shouldRemove is either an object or a boolean
var shouldRemove = attrsToRemove[key];
if (_.isUndefined(shouldRemove)) {
return;
}
// Support nested object
if (_.isPlainObject(val) && !_.isArray(val) && _.isPlainObject(shouldRemove)) {
return this._removeAttributes(val, shouldRemove);
}
if (shouldRemove) {
delete attrs[key];
}
}.bind(this));
},
/**
* Remove expandable attributes
*
* Does not work for objects embedded inside arrays
*
* @param {Object} attrs
* @param {Object} attrsToRemove
*/
_removeExpandableAttributes: function(attrs, attrsToRemove) {
_.forEach(attrs, function(val, key) {
// shouldRemove is either an object or a boolean
var shouldRemove = attrsToRemove[key];
if (_.isUndefined(shouldRemove)) {
return;
}
// Support nested object
if (_.isPlainObject(val) && !_.isArray(val) && _.isPlainObject(shouldRemove)) {
return this._removeExpandableAttributes(val, shouldRemove);
}
// Make sure attribute is an object
// Strip all nested properties except for `_id`
if (_.isPlainObject(attrs[key]) && shouldRemove) {
attrs[key] = _.pick(attrs[key], ['_id']);
}
}.bind(this));
},
/**
* Verifies that all attributes are defined in the schema
* If an attribute is not defined in the schema, it is removed
*
* Note: Mutates `attrs` in place
*
* @param {Object} attrs
* @param {Object} schema
* @param {Object} defaults
* @return {Object}
*/
_validateAttributes: function(attrs, schema, defaults) {
// NOTE: `attrs` can be either an Object or Array
if (!_.isObject(attrs) ||
_.isUndefined(schema) ||
_.isNull(schema) ||
_.isEmpty(schema)) {
return;
}
// Iterate over all attributes
_.forEach(attrs, function(val, key) {
// NOTE: `schema` might be either an Object or String
var schemaType = _.isPlainObject(schema) ? schema[key] : schema;
var schemaDefault = _.isPlainObject(defaults) ? defaults[key] : defaults;
// if the schema for this key does not exist
// remove it as a property completely
if (_.isNull(schemaType) || _.isUndefined(schemaType)) {
delete attrs[key];
return;
}
// Allow the use of `null` to unset
if (_.isNull(val) || _.isUndefined(val)) {
switch (schemaType) {
case 'integer':
case 'uinteger':
case 'float':
case 'ufloat':
case 'boolean':
// Certain types cannot be null
attrs[key] = schemaDefault;
break;
default:
attrs[key] = null;
break;
}
return;
}
// Objects and Arrays
if (_.isArray(schemaType)) {
// Empty array is a loosely defined schema, no-op
// That means allow anything inside
// Ex: []
if (!schemaType.length) {
return;
}
// The schema type is defined by the first element in the array
schemaType = schemaType[0];
if (_.isArray(schemaDefault)) {
schemaDefault = schemaDefault[0];
}
// Array with an empty object, no-op
// Ex. [{}]
if (_.isPlainObject(schemaType) && _.isEmpty(schemaType)) {
return;
}
// Iteratively recursively validate inside each object in the array
// Ex. [{...}]
if (_.isPlainObject(schemaType)) {
_.forEach(val, function(arrVal) {
// Apply defaults to each object value
if (schemaDefault) {
_.defaultsDeep(arrVal, schemaDefault);
}
// Recursively validate the array values
this._validateAttributes(arrVal, schemaType, schemaDefault);
}.bind(this));
return;
}
// Recursively validate the array
// Ex: ['string'] or ['integer']
this._validateAttributes(val, schemaType, schemaDefault);
// Remove `null` and `undefined` from array values
attrs[key] = _.without(attrs[key], null, undefined);
return;
} else if (_.isPlainObject(schemaType)) {
// Ex. {...}
// Recursively validate the object
this._validateAttributes(val, schemaType, schemaDefault);
return
}
// All other types are defined as a string
switch (schemaType) {
case 'id':
if (!Mixins.isObjectId(attrs[key])) {
delete attrs[key];
}
break;
case 'string':
// try to coerce to string
if (_.hasIn(attrs[key], 'toString')) {
attrs[key] = attrs[key].toString();
} else {
delete attrs[key];
}
break;
case 'integer':
attrs[key] = _.parseInt(attrs[key]);
if (!_.isFinite(attrs[key])) {
delete attrs[key];
}
break;
case 'uinteger':
attrs[key] = _.parseInt(attrs[key]);
if (!_.isFinite(attrs[key]) || attrs[key] < 0) {
delete attrs[key];
}
break;
case 'float':
attrs[key] = parseFloat(attrs[key]);
if (!_.isFinite(attrs[key])) {
delete attrs[key];
}
break;
case 'ufloat':
attrs[key] = parseFloat(attrs[key]);
if (!_.isFinite(attrs[key]) || attrs[key] < 0) {
delete attrs[key];
}
break;
case 'boolean':
if (attrs[key] === 'true' || attrs[key] === 1) {
attrs[key] = true;
}
if (attrs[key] === 'false' || attrs[key] === false || attrs[key] === 0) {
attrs[key] = false;
}
if (!_.isBoolean(attrs[key])) {
delete attrs[key];
}
break;
case 'timestamp':
if (!Mixins.isTimestamp(attrs[key])) {
delete attrs[key];
}
break;
case 'date':
// Also support ISO8601 strings, convert to date
if (Mixins.isValidISO8601String(attrs[key])) {
attrs[key] = new Date(attrs[key]);
} else if (!_.isDate(attrs[key])) {
delete attrs[key];
}
break;
default:
// Unsupported type
delete attrs[key];
break;
}
}.bind(this));
},
// Reserved attribute definitions
idAttribute: '_id',
userIdAttribute: 'user_id',
// The mongodb collection name
urlRoot: 'models',
// Flag to force all updates to be patches on `sync`
updateUsingPatch: true,
/**
* Return the default value for a schema type
*
* @param {string} schemaType
* @param {*} schemaDefault
* @return {*}
*/
_defaultVal: function(schemaType, schemaDefault) {
if (!_.isUndefined(schemaDefault)) {
return schemaDefault;
}
switch (schemaType) {
case 'integer':
case 'uinteger':
case 'float':
case 'ufloat':
return 0;
case 'boolean':
return false;
case 'timestamp':
return new Date().getTime(); // ms
case 'date':
return new Date(); // iso
default:
return null;
}
},
/**
* Get the default attribute values for your model.
* When creating an instance of the model,
* any unspecified attributes will be set to their default value.
*
* Define defaults as a function.
*
* @param {Object} def
* @param {boolean} withArray
* @return {Object}
*/
defaults: function(def, withArray) {
def = def ? def : _.result(this, 'definition');
return _.reduce(def, function(defaults, attr, key) {
if (attr.computed) {
return defaults;
}
if (attr.default !== undefined) {
defaults[key] = _.result(attr, 'default');
} else if (attr.type === 'object') {
defaults[key] = this.defaults(attr.fields || {});
} else if (attr.type === 'array') {
// withArray to populate nested array values for _validateAttributes
defaults[key] = withArray ? [this.defaults(attr.fields || {})] : [];
} else {
defaults[key] = this._defaultVal(attr.type);
}
return defaults;
}.bind(this), {});
},
/**
* Get the types of each attribute.
*
* Define schema as a function.
*
* See `model.spec.js` for how to use
*
* @param {Object} def
* @return {Object}
*/
schema: function(def) {
def = def ? def : _.result(this, 'definition');
return _.reduce(def, function(schema, attr, key) {
if (attr.type === 'object') {
schema[key] = this.schema(attr.fields || {});
} else if (attr.type === 'array') {
if (attr.value_type === 'object') {
schema[key] = [this.schema(attr.fields || {})];
} else if (attr.value_type) {
schema[key] = [attr.value_type];
} else {
schema[key] = [];
}
} else {
schema[key] = attr.type;
}
return schema;
}.bind(this), {});
},
/**
* Define attributes that are not settable from the request
*
* @param {String} prop
* @param {Object} def
* @return {Object}
*/
findAttributes: function(prop, def) {
def = def ? def : _.result(this, 'definition');
return _.reduce(def, function(attrs, attr, key) {
if (attr.type === 'object') {
var nested = this.findAttributes(prop, attr.fields || {});
if (!_.isEmpty(nested)) {
attrs[key] = nested;
}
} if (attr[prop]) {
attrs[key] = true;
}
return attrs;
}.bind(this), {});
},
/**
* New and improved way to define model attributes.
* Used to derive `schema`, `defaults`, and other
* properties that can be defined independently.
*
* @return {Object}
*/
definition: function() {
return {};
},
/**
* Define db indexes
*
* @return {Array}
*/
indexes: function() {
return [];
},
// http://backbonejs.org/docs/backbone.html#section-35
constructor: function(attributes, options) {
var attrs = attributes || {};
options || (options = {});
this.cid = _.uniqueId('c');
this.attributes = {};
if (options.collection) this.collection = options.collection;
if (options.parse) attrs = this.parse(attrs, options) || {};
this._schema = this.schema();
this._defaults = this.defaults(undefined, true);
attrs = _.defaultsDeep({}, attrs, this.defaults(undefined, false));
this.set(attrs, options);
this.changed = {};
this.initialize.apply(this, arguments);
},
initialize: function() {
this.db; // reference to a mongodb client/connection
this.changedFromRequest = {};
this.previousFromRequest = {};
},
/**
* Backbone `parse` extended with support for defaults
*
* @param {Object|Array} resp
* @param {Object} options
* @return {Object}
*/
parse: function(resp, options) {
// Mongodb sometimes returns an array of one document
if (_.isArray(resp)) {
resp = resp[0];
}
resp = _.defaultsDeep({}, resp, this.defaults());
return resp;
},
/**
* Backbone `set` extended with support for schema
*
* TODO @ptshih Extend with lodash `set` (nested/deep)
*
* @return {*}
*/
set: function(key, val, options) {
var attrs;
if (key === null) return this;
if (typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
options || (options = {});
// Don't override unset
if (options.unset) {
return Backbone.Model.prototype.set.apply(this, arguments);
}
// Apply schema
this._validateAttributes(attrs, this._schema, this._defaults);
return Backbone.Model.prototype.set.call(this, attrs, options);
},
/**
* Backbone `get` extended with support for deep/nested get
*
* Examples:
*
* - 'foo'
* - 'foo.bar'
* - 'foo.bar.0'
* - 'foo.bar.1.baz'
*
* Lodash Examples:
*
* - 'foo'
* - 'foo.bar'
* - 'foo.bar[0]'
* - 'foo.bar[1].baz'
*
* @param {String} attr
* @return {*}
*/
get: function(attr) {
return this.getDeep(this.attributes, attr);
},
// DEPRECATED 2015-05-08
// Soon `get` will use lodash `get` instead of `getDeep`
getDeep: function(attrs, attr) {
if (!_.isString(attr)) {
return undefined;
}
var keys = attr.split('.');
var key;
var val = attrs;
var context = this;
for (var i = 0, n = keys.length; i < n; i++) {
// get key
key = keys[i];
// Hold reference to the context when diving deep into nested keys
if (i > 0) {
context = val;
}
// get value for key
val = val[key];
// value for key does not exist
// break out of loop early
if (_.isUndefined(val) || _.isNull(val)) {
break;
}
}
// Eval computed properties that are functions
if (_.isFunction(val)) {
// Call it with the proper context (see above)
val = val.call(context);
}
return val;
},
/**
* Backbone `toJSON` extended with support for lodash `cloneDeep`
*/
toJSON: function(options) {
var json = _.cloneDeep(this.attributes);
return json;
},
/**
* Converts model attributes into a pojo (json object)
* Also removes all attributes that are defined to be hidden
* Uses `toJSON`
*
* @return {Object} POJO/JSON
*/
render: function() {
var json = this.toJSON();
var hiddenAttributes = this.findAttributes('hidden');
this._removeAttributes(json, hiddenAttributes);
return json;
},
/**
* Alias for `render`
*/
toResponse: function() {
return this.render();
},
/**
* Used to set attributes from a request body
* Assume `this.attributes` is populated with existing data
*
* @param {Object} body This is the request params/body
* @return {Promise.<Model>}
*/
setFromRequest: Bluebird.method(function(body) {
body = this._mergeSafe(body, this.attributes);
// Remove read only attributes
var readOnlyAttributes = this.findAttributes('readonly');
this._removeAttributes(body, readOnlyAttributes);
// Remove computed attributes
var computedAttributes = this.findAttributes('computed');
this._removeAttributes(this.attributes, computedAttributes);
// Set new attributes
this.set(body);
// At this point, we take a snapshot of the changed attributes
// A copy of the `changed` attributes right after the request body is set
this.changedFromRequest = _.cloneDeep(this.changed);
this.previousFromRequest = _.cloneDeep(this.previousAttributes());
return this;
}),
/**
* Lifecycle Methods
*
* These can either return a promise or a value
*/
beforeFetch: Bluebird.method(function() {
return this;
}),
afterFetch: Bluebird.method(function() {
return this;
}),
beforeCreate: Bluebird.method(function() {
return this;
}),
beforeUpdate: Bluebird.method(function() {
return this;
}),
afterCreate: Bluebird.method(function() {
return this;
}),
afterUpdate: Bluebird.method(function() {
return this;
}),
beforeSave: Bluebird.method(function() {
return this;
}),
afterSave: Bluebird.method(function() {
return this;
}),
/**
* Override the backbone sync method for use with mongodb
*
* Also, if `updateUsingPatch` is enabled,
* All updates (PUT) will be aliased to patches (PATCH)
*
* The `options` object can contains 2 callbacks:
* Both callbacks have parameters (model, resp, options)
* `resp` is either a `document` or an `error` object
*
* - `success`
* - `error`
*
* Events:
*
* - A `request` event is fired before with parameters (model, op, options)
* - A `sync` event is fired after with parameters (model, resp, options)
*
* @param {String} method
* @param {Model} model
* @param {Object} options
* @return {Promise.<Collection>}
*/
sync: Bluebird.method(function(method, model, options) {
// Force all `update` to actually be `patch` if configured
if (this.updateUsingPatch && method === 'update') {
method = 'patch';
}
var op = this[method].call(this, model, options);
model.trigger('request', model, op, options);
return op;
}),
/**
* Backbone `fetch` extended and promisified
* Support `before` and `after` lifecycle methods
*
* @return {Promise.<Model>}
*/
fetch: Bluebird.method(function() {
var originalArguments = arguments;
return Bluebird.bind(this).tap(function() {
return this.beforeFetch.apply(this, originalArguments);
}).tap(function() {
return Backbone.Model.prototype.fetch.apply(this, originalArguments);
}).tap(function() {
return this.afterFetch.apply(this, originalArguments);
}).return(this);
}),
/**
* Backbone `save` extended and promisified
*
* @return {Promise.<Model>}
*/
save: Bluebird.method(function() {
debug.info('Model [%s] save called', this.urlRoot);
var originalArguments = arguments;
// Remove computed attributes
var computedAttributes = this.findAttributes('computed');
this._removeAttributes(this.attributes, computedAttributes);
// Remove expandable attributes
var expandableAttributes = this.findAttributes('expandable');
this._removeExpandableAttributes(this.attributes, expandableAttributes);
var beforeFn, afterFn;
if (this.isNew()) {
beforeFn = this.beforeCreate;
afterFn = this.afterCreate;
} else {
beforeFn = this.beforeUpdate;
afterFn = this.afterUpdate;
}
return Bluebird.bind(this).tap(function() {
return beforeFn.apply(this, originalArguments);
}).tap(function() {
return this.beforeSave.apply(this, originalArguments);
}).tap(function() {
return Backbone.Model.prototype.save.apply(this, originalArguments);
}).tap(function() {
if (this.validationError instanceof Error) {
throw this.validationError;
}
}).tap(function() {
return afterFn.apply(this, originalArguments);
}).tap(function() {
return this.afterSave.apply(this, originalArguments);
}).return(this);
}),
/**
* Inserts a mongodb document
*
* @param {Model} model
* @param {Object} options
* @return {Promise.<Model>}
*/
create: Bluebird.method(function(model, options) {
debug.info('Model [%s] create called', this.urlRoot);
return this.db.insert(
this.urlRoot,
model.toJSON(),
this._wrapResponse(options)
).return(this);
}),
/**
* Updates a mongodb document
*
* NOTE: This replaces the entire document with the model attributes
*
* The parameter `options` has the following properties:
*
* - `require` If true, will throw an error if document is not found
*
* @param {Model} model
* @param {Object} options
* @return {Promise.<Model>}
*/
update: Bluebird.method(function(model, options) {
// If no ID in query, error out
if (model.isNew()) {
var err = new MuniError('Cannot update a new model.', 400);
options.error(err);
throw err;
}
// Build query against the model's id
var query = {};
query[this.idAttribute] = model.id;
if (!!model.get(this.userIdAttribute)) {
query[this.userIdAttribute] = model.get(this.userIdAttribute);
}
// Mongo options
// Don't support `multi`, this is a single model
var mongoOptions = _.pick(options, ['require']) || {};
debug.info(
'Model [%s] update with query: %s and options: %s',
this.urlRoot,
JSON.stringify(query),
JSON.stringify(mongoOptions)
);
return this.db.findAndModify(
this.urlRoot,
query,
model.toJSON(),
mongoOptions,
this._wrapResponse(options)
).return(this);
}),
/**
* Sets a mongodb document
*
* NOTE: This sets only explicitly provided model attributes
*
* The parameter `options` has the following properties:
*
* - `require` If true, will throw an error if document is not found
*
* @param {Model} model
* @param {Object} options
* @return {Promise.<Model>}
*/
patch: Bluebird.method(function(model, options) {
// If no ID in query, error out
if (model.isNew()) {
var err = new MuniError('Cannot patch a new model.', 400);
options.error(err);
throw err;
}
// Build query against the model's id
var query = {};
query[this.idAttribute] = model.id;
if (!!model.get(this.userIdAttribute)) {
query[this.userIdAttribute] = model.get(this.userIdAttribute);
}
// Patch attributes with mongodb set
var attrs = model.toJSON();
delete attrs[this.idAttribute];
// Use mongodb set to only update explicit attributes using `$set`
var obj = {
'$set': attrs
};
// Mongo options
// Don't support `multi`, this is a single model
var mongoOptions = _.pick(options, ['require']) || {};
debug.info(
'Model [%s] patch with query: %s and options: %s',
this.urlRoot,
JSON.stringify(query),
JSON.stringify(options)
);
return this.db.findAndModify(
this.urlRoot,
query,
obj,
mongoOptions,
this._wrapResponse(options)
).return(this);
}),
/**
* Deletes a mongodb document
*
* Note this Promise returns a Number not a Model
*
* @param {Model} model
* @param {Object} options
* @return {Promise.<Number>} Number of documents deleted
*/
delete: Bluebird.method(function(model, options) {
// If no ID in query, error out
if (model.isNew()) {
var err = new MuniError('Cannot delete a new model.', 400);
options.error(err);
throw err;
}
// Build query against the model's id
var query = {};
query[this.idAttribute] = model.id;
debug.info(
'Model [%s] delete with query: %s',
this.urlRoot,
JSON.stringify(query)
);
return this.db.remove(
this.urlRoot,
query,
this._wrapResponse(options)
);
}),
/**
* Finds a single mongodb document
*
* The parameter `options` has the following properties:
*
* - `query` The query to use for the database
* - `require` If true, will throw an error if document is not found
* - `readPreference` Use a read preference when running this query
*
* @param {Model} model
* @param {Object} options
* @return {Promise.<Model>}
*/
read: Bluebird.method(function(model, options) {
var query = {};
if (_.isPlainObject(options.query)) {
// Build query
query = options.query;
} else {
if (model.isNew()) {
// If no ID in query, error out
var err = new MuniError('Cannot read a new model.', 400);
options.error(err);
throw err;
}
// Build query against the model's id and user_id if it exists
query[this.idAttribute] = model.id;
if (!!model.get(this.userIdAttribute)) {
query[this.userIdAttribute] = model.get(this.userIdAttribute);
}
}
// Mongo options
var mongoOptions = _.pick(options, [
'require',
'readPreference',
'sort',
'fields',
'limit',
'skip'
]) || {};
debug.info(
'Model [%s] read with query: %s and options: %s',
this.urlRoot,
JSON.stringify(mongoOptions),
JSON.stringify(query)
);
return Bluebird.bind(this).tap(function() {
return this.ensureIndexes();
}).tap(function() {
return this.db.findOne(
this.urlRoot,
query,
mongoOptions,
this._wrapResponse(options)
);
}).return(this);
}),
/**
* Ensure indexes are created if defined