-
Notifications
You must be signed in to change notification settings - Fork 1
/
collection.js
509 lines (447 loc) · 16.7 KB
/
collection.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
import _ from 'lodash';
import { Container } from 'aurelia-dependency-injection';
import { json } from 'aurelia-fetch-client';
import { Config } from './config';
/**
* Collection class. A collection of models that are
* related to a backend data store and accessible through REST API.
*/
export class Collection {
/**
* Configure the collection.
* @param {String} key : the key provided when the collection has been
* registered
* @param {String} [defaultRoute] : route to use when performing the backend
* http requests, to which the model id is to be appended where expected.
* @param {Function} [modelClass] : function to be called to be called to
* create a model.
* @param {String} [modelid] : the key to use for uniqueness of the models in
* order to store and search for them.
*/
configure(key, modelClass, defaultRoute, modelid = '_id') {
this.container = Container.instance;
if (_.isUndefined(defaultRoute)) {
defaultRoute = '/api/' + key + '/';
}
this.modelid = modelid;
this.defaultRoute = defaultRoute;
this.modelClass = modelClass;
this.collection = [];
this._httpClient = null;
}
/**
* Creates or syncs a model from data.
* @param {Object} data : the content of the model to use for its creation
* or its sync.
* @param {Object} options : modifiers for the method:
* - ignoreCollection attribute can prevent the model from being stored in the
* collection when created.
* - force attribute forces the sync of a model (when it already exists) even
* if it is complete.
* @return {Promise} a promise containg the resolved model created or got
* from the models set saved within the collection. If data is null or
* undefined, it resolves to null.
*/
fromJSON(data, options) {
if (_.isNil(data)) {
return Promise.resolve(null);
}
options = _.defaults(options, {
ignoreCollection: false,
force: false
});
let model = this._getFromCollection(data[this.modelid]);
if (_.isUndefined(model)) {
model = this.container.invoke(this.modelClass, data);
if (!options.ignoreCollection) {
this.collection.push(model);
}
} else if (!this.isComplete(model) || options.force) {
this._syncFrom(model, data);
}
return Promise.resolve(model);
}
/**
* Converts a model to a literal object. It usually calls the model's `toJSON`
* method.
* @param {Model} model : the instance to be converted.
* @param {Object} options : a placeholder argument that may be used when the
* function is overloaded.
* @return {Object} : the literal object made of the model's content.
*/
toJSON(model, options) {
return _.isFunction(model.toJSON) ? model.toJSON() : model;
}
/**
* Clears the set from the models it contained.
*/
flush() {
this.collection = [];
}
/**
* Checks if a model is complete, that is whether all the required fields
* have been filled. If a model is not complete, then it can be fetched
* again and then `_syncFrom`ed.
* @param {Model} model : the model to check for completeness.
* @return {Boolean} true if a model is considered as complete, false.
* otherwise
*/
isComplete(model) {
return true;
}
/**
* Sets the content of a model
* @param {String or Object} model : if it is a string, model is treated as
* a model's id, otherwise it is treated as the content of the model to set.
* @param {Object} options : options to be provided to the get method, with
* `force` attribute set to true to force the request to the backend and the
* assignement of the received data to the model.
* @return {Promise} a promise when the sync is done with the synced model.
*/
sync(model, options) {
return this.get(_.isString(model) ? model : model[this.modelid], _.merge({}, options, { force: true }));
}
/**
* The models the collection collects may have references to other models
* in other collections (i.e. collections).
* This method defines which keys may contain references, the name of
* the key when received from the backend's data, and its name in the model
* class (i.e. `frontendKey`)
* @return {Array} an array of objects which should be made of 3 keys:
* - a backendKey, the `old` key in the data.
* - a frontendKey, the `new` key in the model.
* - and a collection, the collection from which the items will be 'got'.
*/
refKeys() {
return [];
}
/**
* Set the http client to be used for the requests made by the collection.
* @param {Object} httpClient : an http client supporting the fetch API.
*/
_setHttpClient(httpClient) {
this._httpClient = httpClient;
}
/**
* Affects the content of data to a model.
* @param {Model} model : the instance to be modified.
* @param {Object} data : the data to set to model.
*/
_syncFrom(model, data) {
_.merge(model, data);
}
/**
* Get a model by id.
* @param {String} id : the id of the model to be searched for.
* @return {Model} : the matched model or undefined otherwise.
*/
_getFromCollection(id) {
let obj = {};
obj[this.modelid] = id;
return _.find(this.collection, obj);
}
/**
* Remove a model from the collection by id.
* @param {String} id : the id of the model to be removed.
*/
_removeFromCollection(id) {
let obj = {};
obj[this.modelid] = id;
_.remove(this.collection, obj);
}
/**
* [_getById description]
* @param {[type]} id [description]
* @param {[type]} force [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
_getById(id, options) {
const opts = options || {};
const apiRoute = opts.route || this.defaultRoute + id;
let model = this._getFromCollection(id);
if (_.isUndefined(model) || !this.isComplete(model) || opts.force) {
return this._httpClient
.fetch(apiRoute)
.then(response => response.json())
.then(data => {
return this.fromJSON(data, { force: opts.force });
});
}
return Promise.resolve(model);
}
/**
* [create description]
* @param {[type]} jsonModel [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
create(jsonModel, options) {
const opts = options || {};
const apiRoute = opts.route || this.defaultRoute.slice(0, -1);
return this._httpClient
.fetch(apiRoute, {
method: 'post',
body: options.notJson ? jsonModel : json(jsonModel)
}).then(response => response.json())
.then(data => this.get(data));
}
/**
* [destroy description]
* @param {[type]} id [description]
* @param {[type]} route [description]
* @return {[type]} [description]
*/
destroy(id, options) {
const opts = options || {};
const apiRoute = opts.route || this.defaultRoute + id;
this._removeFromCollection(id);
return this._httpClient.fetch(apiRoute, {
method: 'delete'
}).then(response => response.json());
}
/**
* force fetch all models from a collection endpoint
* @return {[Promise]} return a Promise that fullfill a get on all models fetched
*/
all(options) {
return this._httpClient.fetch(this.defaultRoute)
.then(response => response.json())
.then(data => {
return this.get(data, options);
});
}
/**
* [get description]
* @param {[type]} data [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
get(data, options) {
options = _.defaults(options, {
_child: false,
force: false,
recursive: false,
populate: false
});
let modelPromise = null;
if (_.isEmpty(data) || _.isUndefined(data)) { // you cannot get nothing
return Promise.resolve(data);
} else if (_.isArray(data)) { // you iterate on the array, and go one level deeper (for ID or JSON array)
return modelPromise = Promise.all(_.map(data, item => this.get(item, options)));
} else if (_.isObject(data)) { // you already have the json data, just instanciate the model
modelPromise = this.fromJSON(data, options);
} else { // If we end up here, then data is string. We'd better test whether it is an id, but we consider a string as a type of an id.
if (!options._child) { // we are the root level, we want the model no matter what.
modelPromise = this._getById(data, options);
} else {
if (options.populate === true) { // we are a child, so populate means we want the model level root + 1
modelPromise = this._getById(data, options);
} else { // otherwise, we want to keep the backend data (the reference IDs)
modelPromise = Promise.resolve(null);
}
}
}
return modelPromise
.then(model => {
// we don't need to inspect our children, as we are empty
if (_.isNil(model)) {
return model;
}
// if we are a grand child or more, we suppose that recursive take ownership of population for all the decendants levels
let childOpt = _.cloneDeep(options);
// Removal of the route options since it is not supposed to be relevant for children paths.
delete childOpt.route;
if (childOpt._child) {
childOpt.populate = childOpt.recursive = (childOpt.recursive === true);
}
childOpt._child = true;
// each reference attribute (described by the model class) is replaced by the matching model instance if we ask for population
return Promise.all(
_.map(this.refKeys(model), item => {
item = _.defaults(item, {
backendKey: null,
collection: null,
frontendKey: null,
backendKeyDeletion: true
});
// collection and backendKey have to be defined.
let collection = this.container.get(Config).getCollection(item.collection);
if (_.isNil(item.backendKey)) {
return;
}
// If frontendKey has not been specified,
// we consider it as equal to backendKey
if (_.isNil(item.frontendKey)) {
item.frontendKey = item.backendKey;
}
let itemData = model[item.backendKey];
let itemDataPromise = Promise.resolve(null);
// item.collection can be null if we want to keep JSON data.
if (_.isNull(item.collection)) {
itemDataPromise = Promise.resolve(itemData);
} else if (!_.isNil(collection)) {
itemDataPromise = collection.get(itemData, childOpt);
}
return itemDataPromise
.then(childrenItems => {
// Replace the model key if necessary.
if (!_.isNil(childrenItems) && isNotNullArray(childrenItems)) {
if (item.backendKeyDeletion === true) {
delete model[item.backendKey];
}
return model[item.frontendKey] = _.pull(childrenItems, null, undefined);
}
});
}))
.then(() => model);
});
}
/**
* Try to find a model that match `predicate` attributes.
* If no such model is found, try to use given fallbackUrl to retrieve it and return it.
* Otherwise return `undefined`.
*
* @param {Object} predicate Attributes to find in collection.
* @param {[String]} fallbackUrl A GET url endpoint where the model can be retrieved.
* @returns Model found, otherwise `undefined`.
* @memberof Collection
*/
find(predicate, fallbackUrl) {
return new Promise((resolve, reject) => {
let res = _.find(this.collection, predicate);
if (_.isUndefined(res)) {
if (_.isUndefined(fallbackUrl)) {
return resolve();
}
return this._httpClient.fetch(fallbackUrl)
.then(response => response.json())
.then(data => {
return this.get(data, options);
});
}
return resolve(res);
});
}
/**
* Allow to send a PUT method to the backend to update the model, and expect a response with the updated model.
* @param {[type]} model [The model instance to update]
* @param {[type]} attr [A literal object of attribute that should be updated]
* @param {[type]} options [{mergeStrategy: 'replace|array|all|ignore'}, mergeStrategy to correctly merge data recieved]
* @return {[type]} [A Promise resolving with the updated model instance]
*/
update(model, attr, options) {
const opts = options || {};
const apiRoute = opts.route || this.defaultRoute + model[this.modelid];
return this._frontToBackend(attr, opts)
.then(backAttr => {
return this._httpClient
.fetch(apiRoute, {
method: 'put',
headers: { 'Content-Type': 'application/json' },
body: options.notJson ? backAttr : json(backAttr)
}).then(response => response.json())
.then(attributes => this._backToFrontend(attributes, backAttr, model, opts));
}).then(() => model);
}
/**
* [_frontToBackend description]
* @param {[type]} attributes [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
_frontToBackend(attributes, options) {
const refKeys = this.refKeys();
let _getIdFromData = (data) => {
if (_.isString(data)) {
return data;
} else if (_.isArray(data)) {
return _.map(data, _getIdFromData);
} else if (_.isObject(data)) {
return data[this.modelid];
}
return null;
};
_.each(attributes, (value, field) => {
let item = _.find(refKeys, { frontendKey: field });
// If undefined, nothing to convert.
if (_.isUndefined(item)) {
return;
}
item = _.defaults(item, {
backendKey: null,
frontendKey: null,
backendKeyDeletion: true
});
if (item.backendKeyDeletion) {
delete attributes[item.frontendKey];
}
// browser request filter undefined fields, we need to explicitely set it to null to be sent to the backend. (in case of reseting the field)
let id = _getIdFromData(value);
attributes[item.backendKey] = _.isUndefined(id) ? null : id;
});
return Promise.resolve(attributes);
}
/**
* [_backToFrontend description]
* @param {[type]} attributes [description]
* @param {[type]} backAttr [description]
* @param {[type]} model [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
_backToFrontend(attributes, backAttr, model, options) {
const opts = options || {};
const refKeys = this.refKeys();
return Promise.all(_.map(backAttr, (value, field) => {
let frontendKey = field;
let backendKey = field;
let frontendValue = Promise.resolve(attributes[backendKey]);
// The current field is a frontend type of key.
let item = _.find(refKeys, { backendKey: field });
if (!_.isUndefined(item)) {
item = _.defaults(item, {
backendKey: null,
frontendKey: null,
collection: null,
backendKeyDeletion: true
});
frontendKey = item.frontendKey;
backendKey = item.backendKey;
// item.collection can be null if we want to keep JSON data.
if (!_.isNull(item.collection)) {
frontendValue = this.container.get(Config).getCollection(item.collection).get(attributes[backendKey]);
}
}
// Update the right key in the model, with updateStrategy to replace, merge only arrays or merge all the attribute.
return frontendValue.then(result => {
if (!_.has(opts, 'mergeStrategy') || opts.mergeStrategy === 'replace') {
model[frontendKey] = result;
} else if (opts.mergeStrategy === 'ignore') {
return Promise.resolve(model);
} else if (opts.mergeStrategy === 'array') {
if (_.isArray(model[frontendKey])) {
model[frontendKey] = _.union(model[frontendKey], result);
} else {
model[frontendKey] = result;
}
} else {
model[frontendKey] = _.merge(model[frontendKey], result);
}
return Promise.resolve(model);
});
}));
}
}
/**
* Helper function to check that an object is not an `array filled with null
* values`.
* @param {Any} arr : the `object` to check.
* @return {Boolean} false it is an array filled with `null` values, true
* otherwise.
*/
function isNotNullArray(arr) {
// If the arr is an empty array, it is NOT a null array.
// Therefore we can't compact it and check if it is empty.
return !_.isArray(arr) || _.isEmpty(arr) || _.some(arr, _.negate(_.isNil));
}