forked from cujojs/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleRestStore.js
162 lines (139 loc) · 4.69 KB
/
SimpleRestStore.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
/*
* Copyright 2012-2013 the original author or authors
* @license MIT, see LICENSE.txt for details
*
* @author Scott Andrews
*/
(function (define) {
'use strict';
define(function (require) {
var defaultClient, mixin;
defaultClient = require('../../rest');
mixin = require('../util/mixin');
/**
* A REST based object store.
*
* The base path for requests is commonly provided by the
* `rest/interceptor/pathPrefix` interceptor.
*
* @param {SimpleRestStore} [options] configuration information that will be
* mixed into the store
*/
function SimpleRestStore(options) {
mixin(this, options);
this.client = this.client || defaultClient;
}
SimpleRestStore.prototype = {
/**
* @field {Client} client rest client for this store
*/
client: null,
/**
* @field {string} [idProperty='id'] property to use as the identity property. The values of this property should be unique.
*/
idProperty: 'id',
/**
* @field {boolean} [ignoreId=false] if true, add() will always do a POST even if the data item already has an id
*/
ignoreId: false,
/**
* Retrieves an object by its identity. This will trigger a GET request to the server using the url `id`.
*
* @param {string|number} id identity to use to lookup the object
* @param {Object} [options] reserved for future use
*
* @returns {Object} record in the store that matches the given id
*/
get: function (id /*, options */) {
return this.client({
path: id
});
},
/**
* Resolves a records identity using the configured idProperty
*
* @param object to get the identity for
*
* @returns {string|number} the identity
*/
getIdentity: function (object) {
return object[this.idProperty];
},
/**
* Stores a record.
*
* Will trigger a PUT request to the server if the object has an id, otherwise it will trigger a POST request. Unless ignoreId is configured true, in which case POST will always be used.
*
* @param {Object} object record to store
* @param {string|number} [options.id] explicit ID for the record
* @param {boolean} [options.ignoreId] treat the record as if it does not have an ID property
* @param {boolean} [options.overwrite] adds If-Match or If-None-Match header to the request
* @param {boolean} [options.incremental=false] uses POST intead of PUT for a record with an ID
*
* @returns {Promise<Response>} promissed response
*/
put: function (object, options) {
var id, hasId, headers, ignoreId;
options = options || {};
ignoreId = ('ignoreId' in options) ? options.ignoreId : this.ignoreId;
id = ('id' in options) ? options.id : this.getIdentity(object);
hasId = !ignoreId && typeof id !== 'undefined';
headers = {};
if ('overwrite' in options) {
headers[options.overwrite ? 'If-Match' : 'If-None-Match'] = '*';
}
return this.client({
method: hasId && !options.incremental ? 'put' : 'post',
path: hasId ? id : '',
entity: object,
headers: headers
});
},
/**
* Stores a new record.
*
* Will trigger a PUT request to the server if the object has an id, otherwise it will trigger a POST request. Unless ignoreId is configured true, in which case POST will always be used.
*
* @param {Object} object record to add
* @param {string|number} [options.id] explicit ID for the record
* @param {boolean} [options.ignoreId] treat the record as if it does not have an ID property
* @param {boolean} [options.incremental=false] uses POST intead of PUT for a record with an ID
*
* @returns {Promise<Response>} promissed response
*/
add: function (object, options) {
options = options || {};
options.overwrite = false;
return this.put(object, options);
},
/**
* Deletes a record by its identity. This will trigger a DELETE request to the server.
*
* @param {string|number} id identity of the record to delete
*
* @returns {Promise<Response>} promissed response
*/
remove: function (id) {
return this.client({
method: 'delete',
path: id
});
},
/**
* Queries the store for objects. This will trigger a GET request to the server, with the query added as a query string.
*
* @param {Object} query params used for the query string
* @param {Object} [options] reserved for future use
*
* @returns {Promise} query results
*/
query: function (query /*, options */) {
return this.client({ params: query });
}
};
return SimpleRestStore;
});
}(
typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
// Boilerplate for AMD and Node
));