-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfactory.js
241 lines (196 loc) · 6.41 KB
/
factory.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
/* global LocalCollection */
/* global Factory:true */
const factories = {};
Factory = class Factory {
constructor(name, collection, attributes) {
this.name = name;
this.collection = collection;
this.attributes = attributes;
this.afterHooks = [];
this.sequence = 0;
}
after(fn) {
this.afterHooks.push(fn);
return this;
}
};
Factory.define = (name, collection, attributes) => {
factories[name] = new Factory(name, collection, attributes);
return factories[name];
};
Factory.get = (name) => {
const factory = factories[name];
if (!factory) {
throw new Error("Factory: There is no factory named " + name);
}
return factory;
};
Factory._build = (name, attributes = {}, userOptions = {}, options = {}) => {
const factory = Factory.get(name);
// "raw" attributes without functions evaluated, or dotted properties resolved
const extendedAttributes = _.extend({}, factory.attributes, attributes);
// either create a new factory and return its _id
// or return a 'fake' _id (since we're not inserting anything)
const makeRelation = (relName) => {
if (options.insert) {
return Factory.create(relName, {}, userOptions)._id;
}
if (options.tree) {
return Factory._build(relName, {}, userOptions, { tree: true });
}
// fake an id on build
return Random.id();
};
factory.sequence += 1;
const build = (object) => {
const result = {};
const resolve = (value) => {
if (value instanceof Factory) {
return makeRelation(value.name);
} else if (Array.isArray(value)) {
return value.map((entity) => {
return resolve(entity);
});
} else if (typeof value === "function") {
const api = { sequence: (fn) => fn(factory.sequence) };
const fnRes = value.call(extendedAttributes, api, userOptions);
return resolve(fnRes);
// if an object literal is passed in, traverse deeper into it
} else if (Object.prototype.toString.call(value) === "[object Object]") {
return build(value);
}
return value;
};
for (const key in object) {
result[key] = resolve(object[key]);
if (key !== "_id") {
// resolve dotted properties
const modifier = { $set: {} };
modifier.$set[key] = result[key];
LocalCollection._modify(result, modifier);
}
}
return result;
};
const result = build(extendedAttributes);
if (!options.tree) {
result._id = extendedAttributes._id || Random.id();
}
return result;
};
Factory._buildAsync = async (
name,
attributes = {},
userOptions = {},
options = {}
) => {
const factory = Factory.get(name);
// "raw" attributes without functions evaluated, or dotted properties resolved
const extendedAttributes = _.extend({}, factory.attributes, attributes);
// either create a new factory and return its _id
// or return a 'fake' _id (since we're not inserting anything)
const makeRelation = async (relName) => {
if (options.insert) {
const doc = await Factory.createAsync(relName, {}, userOptions);
return doc._id;
}
if (options.tree) {
return await Factory._buildAsync(relName, {}, userOptions, {
tree: true,
});
}
// fake an id on build
return Random.id();
};
factory.sequence += 1;
const build = async (object) => {
const result = {};
const resolve = async (value) => {
if (value instanceof Factory) {
return makeRelation(value.name);
} else if (Array.isArray(value)) {
return Promise.all(
value.map(async (entity) => {
return resolve(entity);
})
);
} else if (typeof value === "function") {
const api = { sequence: (fn) => fn(factory.sequence) };
const fnRes = value.call(extendedAttributes, api, userOptions);
return resolve(fnRes);
// if an object literal is passed in, traverse deeper into it
} else if (Object.prototype.toString.call(value) === "[object Object]") {
return build(value);
}
return value;
};
for (const key in object) {
result[key] = await resolve(object[key]);
if (key !== "_id") {
// resolve dotted properties
const modifier = { $set: {} };
modifier.$set[key] = result[key];
LocalCollection._modify(result, modifier);
}
}
return result;
};
const result = await build(extendedAttributes);
if (!options.tree) {
result._id = extendedAttributes._id || Random.id();
}
return result;
};
Factory.build = (name, attributes = {}, userOptions = {}) => {
return Factory._build(name, attributes, userOptions);
};
Factory.buildAsync = async (name, attributes = {}, userOptions = {}) => {
return Factory._buildAsync(name, attributes, userOptions);
};
Factory.tree = (name, attributes, userOptions = {}) => {
return Factory._build(name, attributes, userOptions, { tree: true });
};
Factory.treeAsync = async (name, attributes, userOptions = {}) => {
return await Factory._buildAsync(name, attributes, userOptions, {
tree: true,
});
};
Factory._create = (name, doc) => {
const collection = Factory.get(name).collection;
const insertId = collection.insert(doc);
const record = collection.findOne(insertId);
return record;
};
Factory._createAsync = async (name, doc) => {
const collection = Factory.get(name).collection;
const insertId = await collection.insertAsync(doc);
const record = await collection.findOneAsync(insertId);
return record;
};
Factory.create = (name, attributes = {}, userOptions = {}) => {
const doc = Factory._build(name, attributes, userOptions, { insert: true });
const record = Factory._create(name, doc);
Factory.get(name).afterHooks.forEach((cb) => cb(record));
if (Factory.get(name).afterHooks.length) {
return Factory.get(name).collection.findOne(record._id);
}
return record;
};
Factory.createAsync = async (name, attributes = {}, userOptions = {}) => {
const doc = await Factory._buildAsync(name, attributes, userOptions, {
insert: true,
});
const record = await Factory._createAsync(name, doc);
await Promise.all(
Factory.get(name).afterHooks.map((cb) => {
return cb(record);
})
);
if (Factory.get(name).afterHooks.length) {
return Factory.get(name).collection.findOneAsync(record._id);
}
return record;
};
Factory.extend = (name, attributes = {}) => {
return _.extend(_.clone(Factory.get(name).attributes), attributes);
};