-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
180 lines (165 loc) · 5.09 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
// TODO Incorporate Store#fetchOrCreate (requires matching to query motifs)
var debug = require('debug')('fixtures')
, inspect = require('util').inspect
, objectUtils = require('racer-util/object')
, deepEqual = objectUtils.deepEqual
, assign = objectUtils.assign
, Promise = require('./Promise')
, deepCopy = require('racer-util/object').deepCopy
;
module.exports = {
fixture: fixture
, unique: unique
, loadFixtures: loadFixtures
};
function fixture (ns, alias, prop) {
return {
$fixture: true
, ns: ns
, alias: alias
, prop: prop
}
}
function unique (value) {
return {
$unique: true
, value: value
}
}
function loadFixtures (fixtures, store, cb, altNsTargets) {
var model = store.createModel();
var promisesByNs = {};
var collectionPromises = {};
for (var currNs in fixtures) {
var docs = fixtures[currNs];
promisesByNs[currNs] = promisesByNs[currNs] || {};
for (var currAlias in docs) {
var doc = deepCopy(docs[currAlias]);
promisesByNs[currNs][currAlias] = promisesByNs[currNs][currAlias] || new Promise;
var deps = [];
var ignore = ['id'];
parseDoc(doc, {
onFixture: function (path, ns, alias, prop) {
ignore.push(path);
deps.push({
ns: ns
, alias: alias
, prop: prop
, property: path
});
}
, onIgnore: function (property, value) {
ignore.push(property);
value = value.value;
}
});
var collPromise = collectionPromises[currNs] = new Promise;
if (altNsTargets && altNsTargets[currNs]) {
altNsTargets[currNs](model, collPromise.resolve.bind(collPromise));
} else {
model.fetch(currNs, (function (currNs, collPromise) {
return function (err) {
if (err) return collPromise.resolve(err);
collPromise.resolve(null, model.get(currNs));
};
})(currNs, collPromise));
}
if (deps.length) {
var depPromises = {};
for (var i = deps.length; i--; ) {
var dep = deps[i]
var ns = dep.ns
, alias = dep.alias
, property = dep.property
, prop = dep.prop;
promisesByNs[ns] = promisesByNs[ns] || {};
var key = ns + ':' + alias + ':' + property + ':' + (prop || '');
depPromises[key] = promisesByNs[ns][alias] = promisesByNs[ns][alias] || new Promise;
}
Promise.parallel(depPromises).on(
promiseCallback(model, doc, currNs, collPromise, currAlias, promisesByNs, ignore)
);
} else {
createDoc(model, currNs, collPromise, currAlias, doc, promisesByNs, ignore);
}
}
}
var allPromises = {};
var createdDocs = {};
for (var ns in promisesByNs) {
var proms = promisesByNs[ns];
createdDocs[ns] = {};
for (var alias in proms) {
var promise = proms[alias];
allPromises[ns + '.' + alias] = promise;
promise.on( (function (ns, alias) {
return function (err, doc) {
createdDocs[ns][alias] = doc;
};
})(ns, alias));
}
}
var totalPromise = Promise.parallel(allPromises);
totalPromise.on( function (err) { cb(err, !err, createdDocs); });
}
function promiseCallback (model, doc, currNs, collPromise, currAlias, promisesByNs, ignore) {
return function (err, values) {
if (err) throw err;
for (var key in values) {
var val = values[key]
, quad = key.split(':')
, _ns = quad[0]
, _alias = quad[1]
, _property = quad[2]
, _prop = quad[3]
assign(doc, _property, _prop ? val[_prop] : val);
}
createDoc(model, currNs, collPromise, currAlias, doc, promisesByNs, ignore);
};
}
function createDoc (model, ns, collPromise, alias, doc, promisesByNs, ignore) {
var k, v;
for (k in doc) {
v = doc[k];
if (typeof v === 'function') doc[k] = v();
}
collPromise.on( function (err, coll) {
if (err) throw err;
for (k in coll) {
if (deepEqual(doc, coll[k], ignore)) return;
}
var id = model.add(ns, doc, function (err) {
if (err) throw err;
doc.id = id;
debug('CREATED ' + ns + ' ' + alias + " \n" + inspect(doc, false, null));
promisesByNs[ns][alias].resolve(err, doc);
});
});
}
function parseDoc (doc, cbs, prefix) {
prefix = prefix || '';
var onFixture = cbs.onFixture;
var onIgnore = cbs.onIgnore;
var iterator = (Array.isArray(doc)) ?
eachMember :
eachChild;
iterator(doc, function (value, property) {
if (Array.isArray(value)) {
return parseDoc(value, cbs, (prefix ? prefix + '.' : '') + property);
}
if (value.constructor !== Object) return;
if (value.$fixture) {
onFixture((prefix ? prefix + '.' : '') + property, value.ns, value.alias, value.prop);
} else if (value.$unique) {
onIgnore((prefix ? prefix + '.' : '') + property, value);
} else {
parseDoc(value, cbs, (prefix ? prefix + '.' : '') + property);
}
});
}
function eachChild (object, cb) {
for (var k in object) cb(object[k], k);
}
function eachMember (array, cb) {
array.forEach(cb);
}