1 | 'use strict';
|
2 | var MemoryResource, Util;
|
3 |
|
4 | Util = require('./util');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | MemoryResource = (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 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
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 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
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 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
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 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | MemoryResource.prototype.findById = function(id) {
|
97 | return Util.clone(this.pool[id]);
|
98 | };
|
99 |
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
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 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | MemoryResource.prototype.findOne = function(filter) {
|
142 | return this.find(filter)[0];
|
143 | };
|
144 |
|
145 |
|
146 | |
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | MemoryResource.prototype.destroy = function(data) {
|
155 | return delete this.pool[data != null ? data.id : void 0];
|
156 | };
|
157 |
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | MemoryResource.prototype.destroyById = function(id) {
|
168 | return delete this.pool[id];
|
169 | };
|
170 |
|
171 |
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
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 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | MemoryResource.prototype.count = function() {
|
208 | return Object.keys(this.pool).length;
|
209 | };
|
210 |
|
211 |
|
212 | |
213 |
|
214 |
|
215 |
|
216 |
|
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 |
|
230 | module.exports = MemoryResource;
|