1 | /*
|
2 | * Copyright 2012-2013 the original author or authors
|
3 | * @license MIT, see LICENSE.txt for details
|
4 | *
|
5 | * @author Scott Andrews
|
6 | */
|
7 |
|
8 | (function (define) {
|
9 | ;
|
10 |
|
11 | define(function (require) {
|
12 |
|
13 | var defaultClient, mixin;
|
14 |
|
15 | defaultClient = require('../../rest');
|
16 | mixin = require('../util/mixin');
|
17 |
|
18 | /**
|
19 | * A REST based object store.
|
20 | *
|
21 | * The base path for requests is commonly provided by the
|
22 | * `rest/interceptor/pathPrefix` interceptor.
|
23 | *
|
24 | * @param {SimpleRestStore} [options] configuration information that will be
|
25 | * mixed into the store
|
26 | */
|
27 | function SimpleRestStore(options) {
|
28 | mixin(this, options);
|
29 | this.client = this.client || defaultClient;
|
30 | }
|
31 |
|
32 | SimpleRestStore.prototype = {
|
33 |
|
34 | /**
|
35 | * @field {Client} client rest client for this store
|
36 | */
|
37 | client: null,
|
38 |
|
39 | /**
|
40 | * @field {string} [idProperty='id'] property to use as the identity property. The values of this property should be unique.
|
41 | */
|
42 | idProperty: 'id',
|
43 |
|
44 | /**
|
45 | * @field {boolean} [ignoreId=false] if true, add() will always do a POST even if the data item already has an id
|
46 | */
|
47 | ignoreId: false,
|
48 |
|
49 | /**
|
50 | * Retrieves an object by its identity. This will trigger a GET request to the server using the url `id`.
|
51 | *
|
52 | * @param {string|number} id identity to use to lookup the object
|
53 | * @param {Object} [options] reserved for future use
|
54 | *
|
55 | * @returns {Object} record in the store that matches the given id
|
56 | */
|
57 | get: function (id /*, options */) {
|
58 | return this.client({
|
59 | path: id
|
60 | });
|
61 | },
|
62 |
|
63 | /**
|
64 | * Resolves a records identity using the configured idProperty
|
65 | *
|
66 | * @param object to get the identity for
|
67 | *
|
68 | * @returns {string|number} the identity
|
69 | */
|
70 | getIdentity: function (object) {
|
71 | return object[this.idProperty];
|
72 | },
|
73 |
|
74 | /**
|
75 | * Stores a record.
|
76 | *
|
77 | * 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.
|
78 | *
|
79 | * @param {Object} object record to store
|
80 | * @param {string|number} [options.id] explicit ID for the record
|
81 | * @param {boolean} [options.ignoreId] treat the record as if it does not have an ID property
|
82 | * @param {boolean} [options.overwrite] adds If-Match or If-None-Match header to the request
|
83 | * @param {boolean} [options.incremental=false] uses POST intead of PUT for a record with an ID
|
84 | *
|
85 | * @returns {Promise<Response>} promissed response
|
86 | */
|
87 | put: function (object, options) {
|
88 | var id, hasId, headers, ignoreId;
|
89 |
|
90 | options = options || {};
|
91 |
|
92 | ignoreId = ('ignoreId' in options) ? options.ignoreId : this.ignoreId;
|
93 | id = ('id' in options) ? options.id : this.getIdentity(object);
|
94 |
|
95 | hasId = !ignoreId && typeof id !== 'undefined';
|
96 | headers = {};
|
97 |
|
98 | if ('overwrite' in options) {
|
99 | headers[options.overwrite ? 'If-Match' : 'If-None-Match'] = '*';
|
100 | }
|
101 |
|
102 | return this.client({
|
103 | method: hasId && !options.incremental ? 'put' : 'post',
|
104 | path: hasId ? id : '',
|
105 | entity: object,
|
106 | headers: headers
|
107 | });
|
108 | },
|
109 |
|
110 | /**
|
111 | * Stores a new record.
|
112 | *
|
113 | * 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.
|
114 | *
|
115 | * @param {Object} object record to add
|
116 | * @param {string|number} [options.id] explicit ID for the record
|
117 | * @param {boolean} [options.ignoreId] treat the record as if it does not have an ID property
|
118 | * @param {boolean} [options.incremental=false] uses POST intead of PUT for a record with an ID
|
119 | *
|
120 | * @returns {Promise<Response>} promissed response
|
121 | */
|
122 | add: function (object, options) {
|
123 | options = options || {};
|
124 | options.overwrite = false;
|
125 | return this.put(object, options);
|
126 | },
|
127 |
|
128 | /**
|
129 | * Deletes a record by its identity. This will trigger a DELETE request to the server.
|
130 | *
|
131 | * @param {string|number} id identity of the record to delete
|
132 | *
|
133 | * @returns {Promise<Response>} promissed response
|
134 | */
|
135 | remove: function (id) {
|
136 | return this.client({
|
137 | method: 'delete',
|
138 | path: id
|
139 | });
|
140 | },
|
141 |
|
142 | /**
|
143 | * Queries the store for objects. This will trigger a GET request to the server, with the query added as a query string.
|
144 | *
|
145 | * @param {Object} query params used for the query string
|
146 | * @param {Object} [options] reserved for future use
|
147 | *
|
148 | * @returns {Promise} query results
|
149 | */
|
150 | query: function (query /*, options */) {
|
151 | return this.client({ params: query });
|
152 | }
|
153 | };
|
154 |
|
155 | return SimpleRestStore;
|
156 |
|
157 | });
|
158 |
|
159 | }(
|
160 | typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
|
161 | // Boilerplate for AMD and Node
|
162 | ));
|