-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbase_model.js
More file actions
711 lines (654 loc) · 19.9 KB
/
base_model.js
File metadata and controls
711 lines (654 loc) · 19.9 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
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
/**
* BaseModel extends [backbone-blueprint](https://github.com/Everyplay/backbone-blueprint)'s
* ValidatingModel providing e.g. Model lifecycle conventions,
* ACL related functionality & CRUD helpers.
*
* Defining a new model requires defining schema & type:
*
* ```js
* var User = serverbone.models.BaseModel.extend({
* type: 'user',
* schema: {
* id: 'schemas/user',
* type: 'object',
* properties: {
* id: {
* type: 'integer'
* },
* username: {
* type: 'string'
* }
* }
* });
* ```
*
* Storage adapter must be changed by overriding Model's
* sync & db, e.g.
*
* ```js
* var redisStore = new RedisDb('serverbone-example', redis.createClient());
* var User = serverbone.models.BaseModel.extend({
* db: redisStore,
* sync: redisStore.sync,
* ...
* });
* ```
*
* would store the Model in Redis.
*/
var _ = require('lodash');
var parallel = require('when/parallel');
var sequence = require('when/sequence');
var Promises = require('backbone-promises');
var when = Promises.when;
var PromisedModel = Promises.Model;
var SchemaModel = require('backbone-blueprint').ValidatingModel;
var ValidationError = require('../errors').ValidationError;
var logId = 'serverbone:models:base';
var _debug = require('debug');
var debug = {
trace: _debug(logId + ':trace'),
log: _debug(logId + ':log'),
warn: _debug(logId + ':warn'),
error: _debug(logId + ':error')
};
/**
* # failWithError
*
* wrapper for converting error & rejecting with error
*
* @param {Object} options Options
* @param {Error} err error
* @returns {Promise} rejected promise
*/
var failWithError = function(options, err) {
options = options || {};
if (options.ignoreFailures) return Promises.when.resolve();
return when.reject(err);
};
/**
* # BaseModel
*/
var BaseModel = SchemaModel.extend({
/**
* ## BaseModel.prototype.initialize
*
* Init attributes & set event listeners
*
* @param {Object} attributes attributes for Model
* @param {Object} options Options
*/
initialize: function(attributes, options) {
BaseModel.__super__.initialize.apply(this, [attributes, options]);
this.options = options ? _.clone(options) : {};
this._processSchema();
// attributes which have changed since this model was synced
this._changedSinceSync = {};
// attributes which this model had when last synced
this._attributesOnSync = false;
this.on('change', this.attributesChanged, this);
this.on('sync', this.modelSynced, this);
},
_processSchema: function() {
if (!this.schema) return;
this.indexes = this.schema.indexes;
},
/**
* ## BaseModel.prototype.url
*
* `url` function is used by some backbone-db drivers, then it defines the database key
*
* @returns {String} url of this Model
*/
url: function() {
var key = this.dbBaseKey || this.type;
if (!this.has(this.idAttribute)) {
return key;
}
return key + ':' + this.get(this.idAttribute);
},
/**
* ## BaseModel.prototype.sync
*
* sync method must be implemented by the Model
*/
sync: function() {
throw new Error('sync method should be overridden!');
},
/**
* ## BaseModel.prototype.save
*
* save extends backbone-blueprint's save by adding calls to lifecycle methods
*
* @param {String/Object} key key to be saved
* @param {Any} val value
* @param {Object} options Options
* @returns {Promise} promise
*/
save: function(key, val, options) {
var opts = options || {};
var self = this;
var attrs;
/* eslint no-eq-null: 0*/
if (key == null || typeof key === 'object') {
opts = val || {};
attrs = key;
} else if (key) {
(attrs = {})[key] = val;
}
// if saving only specified attributes, call set,
// since preSave may depend on changed attributes
if (attrs) {
this.set(attrs, opts);
}
var error = opts.error;
var fns = [
_.bind(self.preSave, self, opts),
_.bind(PromisedModel.prototype.save, self, key, opts),
_.bind(self.afterSave, self, opts)
];
return sequence(fns).then(function(results) {
return results[1];
}, function(err) {
if (error) error.call(opts, self, err);
return failWithError(opts, err);
});
},
/**
* ## BaseModel.prototype.preSave
*
* preSave is called before main save (saving to db) is executed.
* This is a good place to add extended validation, ACL or other code that should be
* executed (asyncronously) before model is saved.
*
* @returns {Promise} resolves immediately unless implemented by Model
*/
preSave: function() {
return when.resolve();
},
/**
* ## BaseModel.prototype.afterSave
*
* afterSave is called after model was successfully saved to db.
* Here you can for example trigger 'hooks' that inform other parts of the application that
* model was successfully saved.
*
* @returns {Promise} resolves immediately unless implemented by Model
*/
afterSave: function() {
return when.resolve();
},
/**
* ## BaseModel.prototype.fetch
*
* fetch extends backbone-blueprint's fetch by adding calls to lifecycle methods
*
* @param {Object} options Options for fetching
* @returns {Promise} promise
*/
fetch: function(options) {
var self = this;
options = options || {};
var error = options.error;
var fns = [
_.bind(self.preFetch, self, options),
_.bind(PromisedModel.prototype.fetch, self, options),
_.bind(self.afterFetch, self, options)
];
return sequence(fns).then(function () {
self.fetchStatus = 'fetched';
return self;
}, function (err) {
if (error) error.call(options, self, err);
return failWithError(options, err);
});
},
/**
* ## BaseModel.prototype.preFetch
*
* preFetch is called before main fetch (reading from db) is executed.
*
* @returns {Promise} resolves immediately unless implemented by Model
*/
preFetch: function() {
return when.resolve();
},
/**
* ## BaseModel.prototype.afterFetch
*
* afterFetch is called after main fetch (reading from db) succeeded.
* Here you can for example fetch dependencies and trigger other functionality that
* depends on the data.
*
* @returns {Promise} resolves immediately unless implemented by Model
*/
afterFetch: function() {
return when.resolve();
},
/**
* ## BaseModel.prototype.isFetched
*
* Get fetch status of the Model
*
* @returns {Boolean} true if Model is fetched
*/
isFetched: function() {
return this.fetchStatus === 'fetched'
|| this.fetchStatus === 'all_fetched';
},
/**
* ## BaseModel.prototype.destroy
*
* fetch extends backbone-blueprint's destroy by adding calls to lifecycle methods
*
* @param {Object} options Options for destroying
* @returns {Promise} promise
*/
destroy: function(options) {
options = options || {};
var fns = [
_.bind(this.preDelete, this, _.clone(options)),
_.bind(PromisedModel.prototype.destroy, this, options),
_.bind(this.afterDelete, this, options)
];
return sequence(fns)
.then(function(results) {
return results[1];
}, _.bind(failWithError, null, options));
},
/**
* ## BaseModel.prototype.preDelete
*
* preDelete is called before main destroy (deleting from db) is run.
* Here you can for example check access control dependencies.
*
* @param {Object} options Options for deleting
* @returns {Promise} by default fetches all relations before deleting.
*/
preDelete: function(options) {
if (this.fetchAllRequired()) {
options = _.clone(options) || {};
options.ignoreFailures = true;
// no need load collection models
options.skipModelsFetch = true;
return this.fetchAll(options);
}
return when.resolve();
},
/**
* ## BaseModel.prototype.afterDelete
*
* afterDelete is called after main destroy (deleting from db) succeeds.
* Here you can for example cascade delete (delete other models that depend on deleted model).
*
* @param {Object} options Options for deleting
* @returns {Promise} resolves immediately unless implemented by Model
*/
/* eslint no-unused-vars: 0 */
afterDelete: function(options) {
return when.resolve();
},
// These ACL methods provide just the interface, default implementation is in ACLModel:
/**
* ## BaseModel.prototype.canAccess
*
* canAccess checks global level access to the model.
* @returns {Boolean} by default true this is just a stub, everyone can access
*/
canAccess: function() {
return true;
},
/**
* ## BaseModel.prototype.getRoles
*
* getRoles dynamically sets roles for this model.
* @returns {Array} returns empty array by default
*/
getRoles: function() {
var roles = [];
return roles;
},
/**
* ## BaseModel.prototype.propertiesWithAccess
*
* propertiesWithAccess return a list of properties keys of this Model
* which the actor has access to.
*
* @returns {Array} array of keys. By default return all keys.
*/
propertiesWithAccess: function() {
return Object.keys(this.schema.properties);
},
/**
* ## BaseModel.prototype.toJSON
*
* toJSON is used both when returning a JSON representation of the Model to the HTTP response and
* when saving the model to database
*
* @returns {Object} JSON object
*/
toJSON: function() {
return BaseModel.__super__.toJSON.apply(this, arguments);
},
/**
* ## BaseModel.prototype.validate
*
* validate the Model based on JSON schema
*
* @param {Object} attrs attributes
* @param {Object} options Options
* @returns {ValidationError} return ValidationError if there were errors
*/
validate: function(attrs, options) {
var validationErrors = BaseModel.__super__.validate.apply(this, arguments);
if (validationErrors) return new ValidationError({
errors: validationErrors
});
},
// Workaround for Backbone modifying the passed options object
isValid: function(options) {
return BaseModel.__super__.isValid.call(this, _.clone(options));
},
/**
* ## BaseModel.prototype.fetchAll
*
* Fetch current model & its relations
* Need to fetch current model first in order to apply correct ids to relations
*
* @param {Object} options Options
* @returns {Promise} promise
*/
fetchAll: function(options) {
debug.trace('fetchAll %s', JSON.stringify(options));
options = _.clone(options) || {};
options.action = 'read';
var self = this;
function handleError(err) {
if (options.ignoreFailures) {
return when.resolve(self);
}
return when.reject(err);
}
return this
.fetch(options)
.then(function() {
if (!self.relationDefinitions) {
return self;
}
return self
.fetchRelations(options)
.then(function() {
return self;
}, handleError);
}, handleError);
},
/**
* ## BaseModel.prototype.fetchRelations
*
* Fetches relations available based on current attributes
* if options.onlyRelations Array is specified only those relations are fetched
*
* e.g.
* ```js
* fetchRelations({onlyRelations: ['user']})
* ```
* will fetch only relation `user`
*
* @param {Object} options Options
* @returns {Promise} promise
*/
fetchRelations: function(options) {
options = options || {};
var self = this;
var fns = [];
_.each(self.relationDefinitions, function(relationAttributes, relationKey) {
if (_.isArray(options.onlyRelations)) {
if (options.onlyRelations.indexOf(relationKey) === -1) return;
}
var relation = self.get(relationKey);
if (relation && !relation.url) {
return;
}
if (relation && relation.fetch) {
// When the run is parallel, clone the options so no shared promises happen.
var fetchOpts = _.has(options.relation, relationKey)
? _.clone(options.relation[relationKey])
: _.clone(options);
fns.push(_.bind(relation.fetch, relation, fetchOpts));
}
}, this);
return parallel(fns)
.then(function relationsFetched() {
self.fetchStatus = 'all_fetched';
return self;
}, function error(err) {
if (options.ignoreFailures) {
self.fetchStatus = 'all_fetched';
return when.resolve(self);
}
return when.reject(err);
});
},
/**
* ## BaseModel.prototype.fetchRequired
*
* Fetch model/relations based on given projection.
* Tries to avoid unnecessary fetches.
* TODO: this does not support all projection options yet.
*
* @param {String/Object} projection projection opts
* @param {Object} options Options
* @returns {Promise} promise
*/
fetchRequired: function(projection, options) {
options = options || {};
if (_.isString(projection)) {
// read projection config from schema
projection = this.schema && this.schema.projection && this.schema.projection[projection];
}
projection = projection || {};
var fetchRequired;
var relationsToBeFetched = [];
if (options.onlyRelations) {
relationsToBeFetched = options.onlyRelations;
} else {
var relations = _.omit(
projection,
'onlyFields',
'removeFields',
'actor',
'requiredDependencies'
);
_.each(relations, function(attrs, relKey) {
var r = this.get(relKey);
var missingKeys = _.any(attrs, function(attr) {
return r && r.get && _.isUndefined(r.get(attr));
});
if (!r || missingKeys) relationsToBeFetched.push(relKey);
}, this);
if (projection.onlyFields) {
relationsToBeFetched = _.uniq(
relationsToBeFetched.concat(this.missingRelations(projection.onlyFields))
);
}
}
if (relationsToBeFetched.length) fetchRequired = true;
if (options.forceFetch) fetchRequired = true;
if (relationsToBeFetched.length) {
var fetchOpts = _.extend({}, options, {
onlyRelations: relationsToBeFetched
});
debug.trace('fetchRequired', relationsToBeFetched);
return this.fetchAll(fetchOpts);
}
var requiredProperties = projection.onlyFields;
if (requiredProperties && !fetchRequired) {
var defaultFields = _.result(this, 'defaults') || {};
fetchRequired = _.any(requiredProperties, function(prop) {
return _.isUndefined(this.get(prop)) || !_.isUndefined(defaultFields[prop]);
}, this);
} else {
fetchRequired = true;
}
if (fetchRequired) {
debug.trace('fetch required, just model without relations');
return this.fetch(_.clone(options));
}
debug.trace('no fetch required', this.type, this.id);
return when.resolve();
},
/**
* ## BaseModel.prototype.saveAll
*
* Save Model & its relations
* Note: Collection relations are not saved
*
* @param {Object} options Options
* @returns {Promise} promise
*/
saveAll: function(options) {
options = options || {};
var fns = [
_.bind(this.save, this, null, options)
];
_.each(this.relationDefinitions, function(relationAttributes, relationKey) {
var relation = this.get(relationKey);
if (relation && _.isFunction(relation.save)) {
fns.push(_.bind(relation.save, relation, null, _.clone(options)));
}
}, this);
return sequence(fns);
},
/**
* ## BaseModel.prototype.applyToAll
*
* applyToAll applies function to this model & its relations
* @param {String} fnStr which function to call
* @param {Object} options pass options to the called function
* @returns {Promise} return a joined promise of the called functions
*/
applyToAll: function(fnStr, options) {
options = options || {};
var fns = [
this[fnStr].apply(this, [options])
];
_.each(this.relationDefinitions, function(relationAttributes, relationKey) {
var relation = this.get(relationKey);
if (relation) {
fns.push(relation[fnStr](options));
}
}, this);
return Promises.when.all(fns);
},
/**
* ## BaseModel.prototype.fetchAllRequired
*
* Check if fetchAll should be called
* @returns {Boolean} `true` if fetchAll should be called
*/
fetchAllRequired: function() {
return this.relationDefinitions
&& Object.keys(this.relationDefinitions).length
&& this.fetchStatus !== 'all_fetched';
},
/**
* ## BaseModel.prototype.changedAttributesWithoutRelations
*
* omits relations from changedAttributes
* @returns {Object} object containing changed attributes
*/
changedAttributesWithoutRelations: function() {
var attrs = this.changedAttributes();
var relationKeys = this.relationDefinitions ? Object.keys(this.relationDefinitions) : [];
attrs = _.omit(attrs, relationKeys);
return attrs;
},
/**
* ## BaseModel.prototype.attributesChanged
*
* Called when attributes of this model are changed. Used for keeping track of which
* attributes have been changed since this model was last synced (fetched or saved).
* @param {Model} model this
* @param {Object} options Options
*/
attributesChanged: function(model, options) {
var attrs = this.changedAttributesWithoutRelations();
this._changedSinceSync = _.extend({}, this._changedSinceSync, attrs);
},
modelSynced: function(model, resp, options) {
if (!options || (!options.action || options.action === 'create')) return;
var relationKeys = this.relationDefinitions ? Object.keys(this.relationDefinitions) : [];
this._attributesOnSync = _.cloneDeep(_.omit(this.attributes, relationKeys));
},
changedSinceSync: function() {
return this._changedSinceSync;
},
parse: function(resp, options) {
this._changedSinceSync = {};
this._attributesOnSync = false;
return BaseModel.__super__.parse.apply(this, arguments);
},
// in context of serverbone `Model.previousAttributes` returns
// attributes that were set when last synced
previousAttributes: function() {
if (this._attributesOnSync) return _.clone(this._attributesOnSync);
return _.clone(this._previousAttributes);
},
/**
* ## BaseModel.prototype.missingRelations
*
* Check missing relations based on keys array.
* @param {Array} keys array of strings
* @returns {Array} keys of missing relations
*/
missingRelations: function(keys) {
var missingRelations = [];
var relationKeys = this.relationDefinitions ? Object.keys(this.relationDefinitions) : [];
_.each(keys, function(key) {
var isRelation = relationKeys.indexOf(key) > -1;
if (isRelation && _.isUndefined(this.get(key))) {
missingRelations.push(key);
}
}, this);
return _.uniq(missingRelations);
}
});
//
/**
* ## BaseModel.setDbDriver
*
* allows overriding db config during runtime
*
* @param {Object} dbSettings object with keys {db: ..., sync: ..., indexDb: ...}
*/
BaseModel.setDbDriver = function(dbSettings) {
this.prototype.db = dbSettings.db;
this.prototype.sync = dbSettings.sync;
this.prototype.indexDb = dbSettings.indexDb;
};
module.exports = BaseModel;
/**
* # formatProperties
*
* Format 'templated' properties of object like {type: '{type}'}
* using given this-context for the actual value
*
* @param {Object} properties properties to format
* @returns {Object} formatted properties, i.e. templated values replaced by real value
*/
var formatProperties = function(properties) {
var self = this;
var realProperties = {};
function getTemplateValue(property) {
function getThisValue(match) {
var cleanedValue = match.substring(1, match.length - 1);
return self[cleanedValue];
}
return property.replace(/{([^}]+)}+/g, getThisValue);
}
_.each(properties, function(property, propertyKey) {
realProperties[propertyKey] = getTemplateValue(property);
});
return realProperties;
};
module.exports.formatProperties = formatProperties;