-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
369 lines (325 loc) · 10.5 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
var Promise = require('bluebird');
var _ = require('lodash');
const create_special_where = (operators) => {
const predicates = _.entries(operators).map(([operator, compare_value]) => {
if (operator === '>') {
return (value) => value > compare_value;
} else if (operator === '<' ) {
return (value) => value < compare_value;
} else if (operator === 'like') {
const regexp = new RegExp(compare_value.replace(/%/g, '.*'));
return (value) => regexp.test(value);
} else {
throw new Error(`Unknown operator '${operator}'`);
}
});
return (value) => predicates.every(pred => pred(value));
}
const create_where = (criteria = {}) => {
if (criteria == null) {
throw new Error(`Criteria to create_where can't be null`);
}
if (typeof criteria === 'number') {
return create_where({ id: criteria });
}
if (criteria.where) {
return create_where(criteria.where);
}
const predicates = _.entries(criteria).map(([key, expected]) => {
if (expected instanceof Date) {
throw new Error(`TODO Date?!`);
}
if (key === 'and') {
const sub_predicates = expected.map(sub_criteria => {
const sub_predicate = create_where(sub_criteria);
return (obj) => sub_criteria(obj);
});
return (obj) => sub_predicates.every(pred => pred(obj));
} else if (key === 'or') {
const sub_predicates = expected.map(sub_criteria => {
const sub_predicate = create_where(sub_criteria);
return (obj) => sub_predicate(obj);
});
return (obj) => sub_predicates.some(pred => pred(obj));
} else if (_.isArray(expected)) {
const sub_predicates = expected.map(exp => {
const items = {
[key]: exp
}
const sub_predicate = create_where(items);
return (obj) => sub_predicate(obj)
});
return (obj) => sub_predicates.some(pred => pred(obj))
}
else if (typeof expected === 'object') {
const sub_predicate = create_special_where(expected);
return (obj) => sub_predicate(obj[key]);
}
else {
return (obj) => {
return obj[key] === expected;
}
}
});
return (obj) => predicates.every(pred => pred(obj));
}
const invariant = (predicate, message) => {
if (!predicate) {
console.warn(`INVARIANT: ${message}`);
throw new Error(message);
}
}
const wrap_item_with_save = (item, model) => {
const prototype = {
save(possible_cb) {
return model.update(item.id, item).then(changed_items => {
if (typeof possible_cb === 'function') {
possible_cb(null, changed_items[0]);
}
return changed_items;
}).catch(err => {
console.log(`err:`, err);
if (typeof possible_cb === 'function') {
possible_cb(err);
}
})
}
}
if (item ) Object.setPrototypeOf(item, prototype);
return item;
}
class Query {
constructor(items, model, collections) {
invariant(typeof items.then === 'function', `First argument to Query should be a promise, got ${items}`);
this.items = items;
this.model = model;
this.collections = collections;
}
populate_collection(on_column, { collection, via }, { where }) {
const new_items_promise = this.items.then(item_or_items => {
invariant(item_or_items != null, `item_or_items is null in .populate { collection: ${collection}, via: ${via} }.`);
const join_mock = this.collections.get_by_name(collection);
const join_node = join_mock.model._attributes[via];
const is_single_item = !Array.isArray(item_or_items);
const items = is_single_item ? [item_or_items] : item_or_items;
if (!join_node.collection && join_node.model) {
const join_table = this.collections.get_by_name(join_node.model);
return Promise.map(items, (item) => {
return join_mock.find(Object.assign({}, {
[via]: item.id,
}, where))
.then(joined_items => {
return Object.assign({}, item, {
[on_column]: joined_items,
})
})
})
.then(new_items => {
return is_single_item ? new_items[0] : new_items;
})
} else {
const us_to_join = `${collection}_${via}`.toLowerCase();
const join_to_them = `${join_node.collection}_${join_node.via}`.toLowerCase();
const intermediate_table_name = [us_to_join, join_to_them].sort().join(`_`);
const intermediate_table = this.collections.get_by_name(intermediate_table_name);
return Promise.map(items, (item) => {
return intermediate_table.find({
[us_to_join]: item.id,
})
.then((intermediate_items) => {
// For every of these items, find the correct one in the join table
return Promise.map(intermediate_items, item => {
return join_mock.findOne({ id: item[join_to_them] });
});
})
.then(join_items => {
const where_clause = create_where(where);
const filter_item = where ? join_items.filter(item => where_clause(item)) : join_items;
return Object.assign({}, item, {
[on_column]: filter_item,
})
})
})
.then(fully_populated_items => {
return is_single_item ? fully_populated_items[0] : fully_populated_items;
});
}
});
return new Query(new_items_promise, this.model, this.collections);
}
populate(on_column, options) {
options = options || {};
let where = options.where || {};
const new_items_promise = this.items.then(item_or_items => {
const attr = this.model.attributes[on_column];
if (!attr.model && !attr.collection)
throw new Error('Attribute is not a model')
if (attr.collection && attr.via) {
try {
return this.populate_collection(on_column, attr, { where: where });
} catch (e) {
console.log('e:', e);
}
}
invariant(!options.where, `Where not yet supported for simple joins`);
const join_model = this.collections.get_by_name(attr.model);
if (Array.isArray(item_or_items)) {
return Promise.map(item_or_items, item => {
return join_model.findOne({ id: item[on_column] }).then(result => {
return Object.assign({}, item, {
[on_column]: result,
});
})
})
} else if (item_or_items) {
let item = item_or_items;
return join_model.findOne({ id: item[on_column] }).then(result => {
return Object.assign({}, item, {
[on_column]: result,
});
})
} else {
return this;
}
});
return new Query(new_items_promise, this.model, this.collections);
}
then(...args) {
return Promise.resolve(this.items).then(...args);
}
catch(fn) {
return this.items.catch(fn);
}
}
class MockTable {
constructor(model, items, collections) {
this.items = items;
this.model = model;
this.collections = collections;
this.changes = []
}
readonly() {
return this;
}
find(criteria) {
const where_clause = create_where(criteria);
const items = this.items.filter(item => where_clause(item));
const wrapped_items = items.map(item => {
return wrap_item_with_save(item, this);
});
return new Query(Promise.resolve(wrapped_items), this.model, this.collections);
}
findOne(criteria) {
let where_clause = create_where(criteria);
const items = this.items.filter(item => where_clause(item));
const wrapped_items = items.map(item => {
return wrap_item_with_save(item, this);
});
return new Query(Promise.resolve(wrapped_items[0]), this.model, this.collections);
}
findOrCreate(criteria) {
const item = this.findOne(criteria);
if (!item) {
this.create(criteria);
}
return this.findOne(criteria)
}
update(criteria, update, query) {
const where_clause = create_where(criteria);
const updated_things = this.items.filter(where_clause);
const updated_ids = updated_things.map(x => x.id);
this.items = this.items.map(item => {
if (where_clause(item)){
var newObject = Object.assign({}, item, update);
return newObject;
} else {
return item;
}
});
this.changes.push({
type: 'update',
createdAt: new Date(),
updated_ids: updated_ids,
query: query || 2,
update: update,
});
return Promise.resolve(updated_things);
}
create(item) {
const with_id = Object.assign({}, item, {
id: this.items.length + 1,
});
this.changes.push({
type: 'create',
createdAt: new Date(),
item: with_id,
});
this.items.push(with_id);
const wrapped_item = wrap_item_with_save(with_id, this);
return new Query(Promise.resolve(wrapped_item), this.model, this.collections);
}
destroy(criteria) {
const where_clause = create_where(criteria);
this.items = this.items.filter(item => !where_clause(item));
this.changes.push({
type: 'destroy',
createdAt: new Date(),
});
return Promise.resolve();
}
};
class MockModels {
constructor() {
this.collections = {};
}
get_all_changes() {
return _.flatten(_.entries(this.collections).map(([collectionName, collection]) => {
return collection.mock.changes.map(change => {
return Object.assign({}, change, {
collectionName: collection.name,
})
});
}))
.sort((a, b) => a.createdAt - b.createdAt);
}
get_by_name(name) {
if (this.collections[name.toLowerCase()]) {
return this.collections[name.toLowerCase()].mock;
}
else {
throw new Error(`No collection found called '${name}`);
}
}
get_collection_by_name (name) {
if (this.collections[name.toLowerCase()]) {
return this.collections[name.toLowerCase()].mock;
}
else {
throw new Error(`No collection found called '${name}`);
}
}
mock_table(name, items) {
let mock = new MockTable(global[name], items, this);
if (global[name]) {
global[name].name = name;
} else {
console.log('Not a table?', name, global[name]);
}
this.collections[name.toLowerCase()] = {
original: global[name],
name: name,
mock: mock,
};
}
apply() {
Object.values(this.collections).forEach(col => {
global[col.name] = col.mock;
})
}
de_apply() {
Object.values(this.collections).forEach(col => {
global[col.name] = col.original;
})
}
}
module.exports.MockModels = MockModels;