forked from balderdashy/sails-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·314 lines (231 loc) · 9.6 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*---------------------------------------------------------------
:: MemoryAdapter
-> adapter
This memory adapter is for development only!
---------------------------------------------------------------*/
var _ = require('lodash');
module.exports = (function () {
// Load criteria module
var getMatchIndices = require('waterline-criteria');
// Maintain connections to open file and memory stores
var connections = { };
// In memory representation of the data model
var data = { };
var schema = { };
var counters = { };
var adapter = {
// Whether this adapter is syncable (yes)
syncable: true,
// How this adapter should be synced
migrate: 'alter',
// Default configuration for collections
defaults: {
schema: false
},
registerCollection: function (collection, cb) {
// Save reference to collection so we have it
schema[collection.identity] = collection;
cb();
},
// Return attributes
describe: function (collectionName, cb) {
cb(null, schema[collectionName].attributes);
},
// Adapters are not responsible for checking for existence of the collection
define: function (collectionName, definition, cb) {
data[collectionName] = [];
counters[collectionName] = {};
schema[collectionName].attributes = _.clone(definition);
cb(null, schema[collectionName].attributes);
},
drop: function (collectionName, cb) {
delete data[collectionName];
delete schema[collectionName].attributes;
delete counters[collectionName];
cb();
},
find: function (collectionName, options, cb) {
// Get indices from original data which match, in order
var matchIndices = getMatchIndices(data[collectionName],options);
var resultSet = [];
_.each(matchIndices,function (matchIndex) {
resultSet.push(_.clone(data[collectionName][matchIndex]));
});
// If we're grouping
if(options.groupBy || options.sum || options.average || options.min || options.max) {
// Check if we have calculations to do
if(!options.sum && !options.average && !options.min && !options.max) {
return cb(new Error('Cannot groupBy without a calculation'));
}
// First we groupBy
// grouped results is our current resultSet, split up by group
var groupedResults = [];
// finished results is our generated results (with sums, evgs, etc)
var finishedResults = [];
if(options.groupBy) {
var groups = [];
var groupCollector = {};
// Go through the results
resultSet.forEach(function(item){
var key = '';
options.groupBy.forEach(function(groupKey){
key += item[groupKey] + '---';
});
if(groupCollector[key]) {
groupCollector[key].push(item);
} else {
groupCollector[key] = [item];
}
});
for(var key in groupCollector) {
groups.push(groupCollector[key]);
}
groupedResults = groups;
// Then we generate stub objects for adding/averaging
groups.forEach(function(group){
var stubResult = {};
// Groupresult will look like this: { type: 'count', a2: 'test' }
options.groupBy.forEach(function(groupKey) {
// Set the grouped by value to the value of the first results
stubResult[groupKey] = group[0][groupKey];
});
finishedResults.push(stubResult);
});
} else {
groupedResults = [resultSet];
finishedResults = [{}];
}
// sum all the things (specified)
if(options.sum) {
// fill in our stub object with those keys, set to sum 0
options.sum.forEach(function(sumKey) {
finishedResults.forEach(function(stub) {
stub[sumKey] = 0;
});
});
// iterate over all groups of data
groupedResults.forEach(function(group, i) {
// sum for each item
group.forEach(function(item) {
options.sum.forEach(function(sumKey) {
if(typeof item[sumKey] === 'number') {
finishedResults[i][sumKey]+=item[sumKey];
}
});
});
});
}
if(options.average) {
// fill in our stub object with those keys, set to sum 0
options.average.forEach(function(sumKey) {
finishedResults.forEach(function(stub) {
stub[sumKey] = 0;
});
});
// iterate over all groups of data
groupedResults.forEach(function(group, i) {
options.average.forEach(function(sumKey) {
// count up how many numbers we have, so we know how much to divide by
var cnt = 0;
// average for each item
group.forEach(function(item) {
if(typeof item[sumKey] === 'number') {
finishedResults[i][sumKey]+=item[sumKey];
cnt+=1;
}
});
finishedResults[i][sumKey]/=cnt;
});
});
}
if(options.min) {
// iterate over all groups of data
groupedResults.forEach(function(group, i) {
options.min.forEach(function(sumKey) {
// keep track of current minimum
var min = Infinity;
// update min
group.forEach(function(item) {
if(typeof item[sumKey] === 'number') {
if(item[sumKey] < min) {
min = item[sumKey];
}
}
});
finishedResults[i][sumKey] = isFinite(min) ? min : null;
});
});
}
if(options.max) {
// iterate over all groups of data
groupedResults.forEach(function(group, i) {
options.max.forEach(function(sumKey) {
// keep track of current maximum
var max = -Infinity;
// update max
group.forEach(function(item) {
if(typeof item[sumKey] === 'number') {
if(item[sumKey] > max) {
max = item[sumKey];
}
}
});
finishedResults[i][sumKey] = isFinite(max) ? max : null;
});
});
}
resultSet = finishedResults;
}
cb(null, resultSet);
},
create: function (collectionName, values, cb) {
for (var attrName in schema[collectionName].attributes) {
var attrDef = schema[collectionName].attributes[attrName];
if (attrDef.unique) {
for (var index in data[collectionName]) {
// Ignore uniquness check on undefined values
// ('required' check is taken care of in waterline core)
if (_.isUndefined(values[attrName])) {
continue;
}
if (values[attrName] === data[collectionName][index][attrName]) {
return cb('Uniqueness check failed on attribute: ' + attrName + ' with value: ' + values[attrName]);
}
}
}
// Only apply autoIncrement if value is not specified
if (attrDef.autoIncrement && !values[attrName]) {
// Increment AI counter
if (counters[collectionName][attrName]) {
counters[collectionName][attrName]++;
}
else counters[collectionName][attrName] = 1;
// Set data to current auto-increment value
values[attrName] = counters[collectionName][attrName];
}
}
data[collectionName].push(values);
cb(null, values);
},
update: function (collectionName, options, values, cb) {
// Get indices from original data which match, in order
var matchIndices = getMatchIndices(data[collectionName],options);
var resultSet = [];
_.each(matchIndices,function (matchIndex) {
data[collectionName][matchIndex] = _.extend(data[collectionName][matchIndex], values);
resultSet.push(_.clone(data[collectionName][matchIndex]));
});
cb(null, resultSet);
},
destroy: function (collectionName, options, cb) {
// Get indices from original data which match, in order
var matchIndices = getMatchIndices(data[collectionName], options);
// Delete data which matches the criteria
data[collectionName] = _.reject(data[collectionName], function (model, i) {
return _.contains(matchIndices, i);
});
cb();
}
};
return adapter;
})();