-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathupdateAll.js
132 lines (124 loc) · 4.27 KB
/
updateAll.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
function errorPrefix(resourceName) {
return 'DS.updateAll(' + resourceName + ', attrs, params[, options]): ';
}
/**
* @doc method
* @id DS.async methods:updateAll
* @name updateAll
* @description
* The "U" in "CRUD". Update items of type `resourceName` with `attrs` according to the criteria specified by `params`.
* This is useful when you want to update multiple items with the same attributes or you don't want to update the items
* in the data store until the adapter operation succeeds. The resulting items (by default) will be injected into the
* data store.
*
* ## Signature:
* ```js
* DS.updateAll(resourceName, attrs, params[, options])
* ```
*
* ## Example:
*
* ```js
* var params = {
* where: {
* author: {
* '==': 'John Anderson'
* }
* }
* };
*
* DS.filter('document', params); // []
*
* DS.updateAll('document', {
* author: 'Sally'
* }, params).then(function (documents) {
* documents; // The documents that were updated via an adapter
* // and now reside in the data store
*
* documents[0].author; // "Sally"
* });
* ```
*
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
* @param {object} attrs The attributes with which to update the items.
* @param {object} params Parameter object that is serialized into the query string. Default properties:
*
* - `{object=}` - `where` - Where clause.
* - `{number=}` - `limit` - Limit clause.
* - `{number=}` - `skip` - Skip clause.
* - `{number=}` - `offset` - Same as skip.
* - `{string|array=}` - `orderBy` - OrderBy clause.
*
* @param {object=} options Optional configuration. Also passed along to the adapter's `updateAll` method. Properties:
*
* - `{boolean=}` - `cacheResponse` - Inject the items returned by the adapter into the data store. Default: `true`.
*
* @returns {Promise} Promise produced by the `$q` service.
*
* ## Resolves with:
*
* - `{array}` - `items` - The items returned by the adapter.
*
* ## Rejects with:
*
* - `{IllegalArgumentError}`
* - `{NonexistentResourceError}`
*/
function updateAll(resourceName, attrs, params, options) {
var DS = this;
var DSUtils = DS.utils;
var deferred = DS.$q.defer();
try {
var IA = DS.errors.IA;
var definition = DS.definitions[resourceName];
options = options || {};
if (!definition) {
throw new DS.errors.NER(errorPrefix(resourceName) + resourceName);
} else if (!DSUtils.isObject(attrs)) {
throw new IA(errorPrefix(resourceName) + 'attrs: Must be an object!');
} else if (!DSUtils.isObject(params)) {
throw new IA(errorPrefix(resourceName) + 'params: Must be an object!');
} else if (!DSUtils.isObject(options)) {
throw new IA(errorPrefix(resourceName) + 'options: Must be an object!');
}
options = DSUtils._(definition, options);
deferred.resolve(attrs);
return deferred.promise
.then(function (attrs) {
return options.beforeValidate.call(attrs, resourceName, attrs);
})
.then(function (attrs) {
return options.validate.call(attrs, resourceName, attrs);
})
.then(function (attrs) {
return options.afterValidate.call(attrs, resourceName, attrs);
})
.then(function (attrs) {
return options.beforeUpdate.call(attrs, resourceName, attrs);
})
.then(function (attrs) {
if (options.notify) {
DS.emit(definition, 'beforeUpdate', DSUtils.merge({}, attrs));
}
return DS.adapters[options.adapter || definition.defaultAdapter].updateAll(definition, options.serialize ? options.serialize(resourceName, attrs) : definition.serialize(resourceName, attrs), params, options);
})
.then(function (res) {
var attrs = options.deserialize ? options.deserialize(resourceName, res) : definition.deserialize(resourceName, res);
return options.afterUpdate.call(attrs, resourceName, attrs);
})
.then(function (attrs) {
if (options.notify) {
DS.emit(definition, 'afterUpdate', DSUtils.merge({}, attrs));
}
if (options.cacheResponse) {
return DS.inject(definition.name, attrs, options);
} else {
return attrs;
}
});
} catch (err) {
deferred.reject(err);
return deferred.promise;
}
}
module.exports = updateAll;