-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
312 lines (306 loc) · 8.99 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
"use strict";
const _ = require('lodash');
const fs = require('fs');
const moment = require('moment');
const graphml = require('graphml-parser');
const glob = require('glob').sync;
const path = require('path');
const ObjectID = require('mongodb').ObjectID;
class MongoView {
constructor(name, source, options) {
const ast = graphml.parse(source);
this.models = options.models;
this.dataSources = options.dataSources;
this.name = name;
this.rootType = ast.type;
this.keys = [
{
type: this.rootType,
idKey: (id) => {
return {
_id: ObjectID(id)
};
}
}
];
this.rootTree = this.parseTreeWithModels(ast.root);
}
parseTreeWithModels(ast) {
let type = ast.type;
if (!type) {
type = ast.type = this.rootType;
ast.isRoot = true;
}
let model = this.models[type];
if (!model) {
return;
throw new Error(
'occuring an error when parsing the model `' + type + '`.');
}
let relations = model.relations;
for (let name in ast.methods) {
let method = ast.methods[name];
let relation = relations[name];
if (method.args && method.args.$relation) {
relation = relations[method.args.$relation];
}
if (!relation) {
throw new TypeError('cannot handle with method: ' + name);
}
method.name = name;
method.type = relation.model;
method.relation = relation;
if (ast.isRoot && method.relation.type === 'hasMany') {
method.filterConfig = {
name: method.name,
base: {
[method.name]: {
$elemMatch: null
}
},
hasArray: true,
isHasMany: true
};
} else {
let parentConfig = method.parent.filterConfig;
let currentConfig = {
name: method.name
};
if (!parentConfig) {
currentConfig.base = currentConfig.name;
} else {
if (parentConfig.hasArray === true) {
currentConfig.hasArray = true;
if (parentConfig.isHasMany) {
currentConfig.superBase = parentConfig;
currentConfig.base = currentConfig.name;
} else {
currentConfig.superBase = parentConfig.superBase;
currentConfig.base = parentConfig.base + '.' + currentConfig.name;
}
} else {
currentConfig.base = parentConfig.base + '.' + currentConfig.name;
}
}
method.filterConfig = currentConfig;
}
this.keys.push({
type: relation.model,
idKey: (id) => {
let config = method.filterConfig;
id = ObjectID(id);
if (!config) {
return {_id: id};
} else if (config.hasArray === true) {
let filter;
if (config.superBase) {
filter = Object.assign({}, config.superBase.base);
filter[config.superBase.name].$elemMatch = {
[config.base + '._id']: id
};
} else {
filter = Object.assign({}, config.base);
filter[config.name].$elemMatch = {
_id: id
};
}
return filter;
} else {
return {[config.base + '._id']: id};
}
}
});
this.parseTreeWithModels(method);
}
return ast;
}
fetchAll() {
return this.fetchItemOrItems(this.rootTree)
.then((results) => {
return this.uploadToViewsDb(results);
});
}
fetch(where) {
let coll = this.dataSources.view.collection(this.name);
return coll.find(where).toArray().then((items) => {
return items.map((item) => ObjectID(item._id));
}).then((ids) => {
if (ids.length > 0) {
return this.fetchItemOrItems(this.rootTree, ids);
} else {
return [];
}
}).then((results) => {
if (results.length > 0) {
return this.updateToViewsDb(results);
}
})
}
fetchItemOrItems(config, id) {
let coll = this.dataSources.rest.collection(config.type);
let cursor = coll.find();
let filter = {};
if (id) {
if (Array.isArray(id)) {
filter = {
_id: {$in: id}
};
} else if (id.isPlainObject) {
delete id.isPlainObject;
filter = Object.assign({}, id);
} else {
let newId;
try {
newId = ObjectID(id);
} catch (err) {
newId = id;
}
filter = {
_id: newId,
};
}
}
const queryArgs = Object.assign({}, config.args);
if (queryArgs) {
delete queryArgs.$relation;
if (typeof queryArgs.$limit === 'number') {
cursor = cursor.limit(queryArgs.$limit);
delete queryArgs.$limit;
}
if (typeof queryArgs.$orderBy === 'string') {
cursor = cursor.sort({
[queryArgs.$orderBy]: 1
});
delete queryArgs.$orderBy;
}
if (typeof queryArgs.$lastWeekBy === 'string') {
queryArgs[queryArgs.$lastWeekBy] = {
$gt: moment().startOf('isoWeek').toDate(),
$lt: moment().endOf('isoWeek').toDate()
};
delete queryArgs.$lastWeekBy;
}
if (Object.keys(queryArgs).length > 0) {
filter = Object.assign(filter, queryArgs);
}
}
return cursor.filter(filter).toArray().then((items) => {
let filteredResults = items.map(item => {
return config.fields.concat('_id').reduce((newItem, field) => {
newItem[field] = item[field];
if (field === '_id') {
newItem.id = item._id;
}
return newItem;
}, {
_raw: item
});
}).map(item => {
// copy item._raw into the variable `rawItem` for later usage
let rawItem = Object.assign({}, item._raw);
// delete the original _raw field
delete item._raw;
// start define the monads/promises
let tasks = Object.keys(config.methods).map((methodName) => {
let method = config.methods[methodName];
let foreignKey;
try {
foreignKey = method.relation.foreignKey;
if (!foreignKey) {
throw new TypeError('unknown foreignKey');
}
} catch (err) {
console.error(method, err);
}
let $promise;
switch (method.relation.type) {
case 'belongsTo':
let foreignId = rawItem[foreignKey];
if (foreignId === undefined) {
return null;
}
$promise = this.fetchItemOrItems(method, foreignId)
.then((data) => {
return {
data: data[0],
name: method.name
};
});
break;
case 'hasMany':
$promise = this.fetchItemOrItems(method, {
// in hasMany mode, taking the id of this mode, and put the following
// { [foreignKey]: id }
[foreignKey]: rawItem._id,
isPlainObject: true
})
.then((data) => {
return {
data: data,
name: method.name
}
});
break;
case 'referencesMany':
$promise = this.fetchItemOrItems(method, rawItem.roleIds)
.then((data) => {
return {
data: data,
name: method.name,
};
});
break;
default:
$promise = null;
}
return $promise;
}).filter((item) => {
return item !== null;
});
if (tasks.length === 0) {
return item;
} else {
return Promise.all(tasks).then((results) => {
for (let result of results) {
item[result.name] = result.data;
}
return item;
});
}
});
return Promise.all(filteredResults);
});
}
uploadToViewsDb(results) {
let coll = this.dataSources.view.collection(this.name);
return coll.deleteMany({
_id: {$in: _.map(results, '_id')}
}).then(() => {
if (results.length > 0) {
return coll.insertMany(results);
} else {
return results;
}
});
}
updateToViewsDb(results) {
let coll = this.dataSources.view.collection(this.name);
let upsertTasks = results.map((data) => {
return coll.update({_id: data.id}, data);
});
return Promise.all(upsertTasks);
}
}
function parseGraphs (models, dataSources, dir) {
const graphPath = path.join(dir, './specs');
return glob(graphPath + '/*.graphml').map((p) => {
const name = path.basename(p, '.graphml');
const source = fs.readFileSync(p).toString('utf8');
return new MongoView(name, source, {
dataSources: dataSources,
models: models
});
});
}
exports.MongoView = MongoView;
exports.parseGraphs = parseGraphs;