UNPKG

4.35 kBJavaScriptView Raw
1'use strict';
2var MemoryResource, Util;
3
4Util = require('./util');
5
6
7/**
8sync memory storage, implements ResourceClientInterface
9
10@class MemoryResource
11@implements ResourceClientInterface
12 */
13
14MemoryResource = (function() {
15 MemoryResource.restore = function(obj) {
16 var currentIdNum, memoryResource, pool;
17 pool = obj.pool, currentIdNum = obj.currentIdNum;
18 memoryResource = new MemoryResource();
19 memoryResource.pool = pool;
20 memoryResource.currentIdNum = currentIdNum;
21 return memoryResource;
22 };
23
24 function MemoryResource() {
25 this.currentIdNum = 1;
26 this.pool = {};
27 }
28
29
30 /**
31 Generate id
32
33 @method generateId
34 @public
35 @param {Object} data
36 @return {String}
37 */
38
39 MemoryResource.prototype.generateId = function() {
40 var id;
41 id = this.currentIdNum;
42 while (this.pool[id] != null) {
43 id = ++this.currentIdNum;
44 }
45 return id.toString();
46 };
47
48
49 /**
50 Create new instance of Model class, saved in database
51
52 @method create
53 @public
54 @param {Object} data
55 @return {Object}
56 */
57
58 MemoryResource.prototype.create = function(data) {
59 if (data == null) {
60 data = {};
61 }
62 if (data.id == null) {
63 data.id = this.generateId();
64 }
65 return this.pool[data.id] = Util.clone(data);
66 };
67
68
69 /**
70 Update or insert a model instance
71 The update will override any specified attributes in the request data object. It won’t remove existing ones unless the value is set to null.
72
73 @method upsert
74 @public
75 @param {Object} data
76 @return {Object}
77 */
78
79 MemoryResource.prototype.upsert = function(data) {
80 if (data == null) {
81 data = {};
82 }
83 return this.create(data);
84 };
85
86
87 /**
88 Find object by ID.
89
90 @method findById
91 @public
92 @param {String} id
93 @return {Object}
94 */
95
96 MemoryResource.prototype.findById = function(id) {
97 return Util.clone(this.pool[id]);
98 };
99
100
101 /**
102 Find all model instances that match filter specification.
103
104 @method find
105 @public
106 @param {Object} filter
107 @return {Array(Object)}
108 */
109
110 MemoryResource.prototype.find = function(filter) {
111 var id, obj, where;
112 if (filter == null) {
113 filter = {};
114 }
115 where = filter.where;
116 if (!where) {
117 return (function() {
118 var ref, results;
119 ref = this.pool;
120 results = [];
121 for (id in ref) {
122 obj = ref[id];
123 results.push(Util.clone(obj));
124 }
125 return results;
126 }).call(this);
127 }
128 throw new Error('"find" method with "where" is currently unimplemented.');
129 };
130
131
132 /**
133 Find one model instance that matches filter specification. Same as find, but limited to one result
134
135 @method findOne
136 @public
137 @param {Object} filter
138 @return {Object}
139 */
140
141 MemoryResource.prototype.findOne = function(filter) {
142 return this.find(filter)[0];
143 };
144
145
146 /**
147 Destroy model instance
148
149 @method destroyById
150 @public
151 @param {Object} data
152 */
153
154 MemoryResource.prototype.destroy = function(data) {
155 return delete this.pool[data != null ? data.id : void 0];
156 };
157
158
159 /**
160 Destroy model instance with the specified ID.
161
162 @method destroyById
163 @public
164 @param {String} id
165 */
166
167 MemoryResource.prototype.destroyById = function(id) {
168 return delete this.pool[id];
169 };
170
171
172 /**
173 Update set of attributes.
174
175 @method updateAttributes
176 @public
177 @param {Object} data
178 @return {Object}
179 */
180
181 MemoryResource.prototype.updateAttributes = function(id, data) {
182 var k, pooledData, v;
183 pooledData = this.pool[id];
184 throw new Error("id " + id + " is not found")((function() {
185 var results;
186 if (pooledData) {
187 results = [];
188 for (k in data) {
189 v = data[k];
190 results.push(pooledData[k] = v);
191 }
192 return results;
193 }
194 })());
195 this.pool[id] = pooledData;
196 return Util.clone(pooledData);
197 };
198
199
200 /**
201 Count all registered data
202
203 @method count
204 @return {Number} total
205 */
206
207 MemoryResource.prototype.count = function() {
208 return Object.keys(this.pool).length;
209 };
210
211
212 /**
213 create plain object
214
215 @method toPlainObject
216 @return {Object} plainObject
217 */
218
219 MemoryResource.prototype.toPlainObject = function() {
220 return {
221 pool: Util.clone(this.pool),
222 currentIdNum: this.currentIdNum
223 };
224 };
225
226 return MemoryResource;
227
228})();
229
230module.exports = MemoryResource;