-
Notifications
You must be signed in to change notification settings - Fork 2
/
src.js
300 lines (269 loc) · 8.24 KB
/
src.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
var router = require('nested-router');
var pathToRegexp = require('path-to-regexp');
var { resolve } = require('path-browserify');
var cannotPush = (obj, path) => {
error(true, `Can't push to "${path}". Use "set"`);
};
var isTypeof = (desiredType) => {
return {
validate (val, path) {
var actualType = typeof val;
error(actualType !== desiredType, `Validation Error: Invalid value, expected "${desiredType}" but got "${actualType}" for path "${path}".`);
}
};
};
var toArray$ = (obj) => {
return Object.keys(obj).reduce((arr, key) => {
obj[key]._id = key;
arr.push(obj[key]);
return arr;
}, []);
};
var list = {
validate (val, path) {
error(true, 'Cannot call `set` on a `list`, use `ref.push`. Path '+path);
},
transform (snapshotVal) {
return toArray$(snapshotVal);
},
toString () {
return 'list';
}
};
var hash = {
toString () {
return 'hash';
},
validate (obj, path) {
error('object' !== typeof obj, `Validation Error: received non-object for path "${path}"`);
}
};
var index = (path) => {
return {
toString() {
return 'index';
},
getPath () {
return path;
}
}
};
var key = (path) => {
return {
toString () {
return 'key';
},
validate (val) {
error(typeof val !== 'string', `Validation Error: keys must be strings, got "${typeof val}"`);
},
getPath () {
return path;
}
}
};
var createRefForReals = (Firebase, path, routes, host) => {
var matchInfo = router.match(path, routes);
error(matchInfo === null, `The path "${path}" was not found in your schema`);
var handler = getLast(matchInfo.handlers);
var firebaseRef = new Firebase(`${host}/${path}`);
var set = (val, cb) => {
handler.validate(val, path);
if (handler === hash)
validateHash(val, path, routes, matchInfo);
firebaseRef.set(val, cb);
};
var push = (obj, cb) => {
validatePush(obj, path, routes, matchInfo);
firebaseRef.push(obj, cb);
};
var parse = (snapshot, cb) => {
var snapshotVal = snapshot.val();
if (snapshotVal === null)
return cb(null, null);
// all of this is garbage, each type should handle the transforms
var transformed = handler.transform ? handler.transform(snapshotVal) : snapshotVal;
if (matchInfo.route.handler == 'list')
transformed = transformListChildren(transformed, routes, path);
if (matchInfo.route.handler == 'hash')
transformed = transformHash(transformed, routes, path);
addIdForDirectRefToListChild(transformed, matchInfo.route, path);
addRelationships(transformed, matchInfo);
addIndexes(transformed, matchInfo);
cb(null, transformed);
};
var getValue = (cb) => {
firebaseRef.once('value', (snapshot) => {
parse(snapshot, cb);
});
};
var child = (childPath) => {
return createRefForReals(Firebase, `${path}/${childPath}`, routes, host);
};
var listen = (userHandler) => {
var handler = snapshot => parse(snapshot, userHandler);
firebaseRef.on('value', handler);
return {
dispose () {
firebaseRef.off('value', handler);
}
};
};
var removeChangeListener = () => {
};
return { set:set, getValue, push, child, listen };
};
var transformHash = (child, routes, path) => {
Object.keys(child).forEach((key) => {
var childPath = `${path}/${key}`;
var matchInfo = router.match(childPath, routes);
var handler = matchInfo.route.handler;
if (handler.transform)
child[key] = handler.transform(child[key]);
});
return child;
};
var transformListChildren = (children, routes, path) => {
// should probably recurse here
return children.map((child) => {
Object.keys(child).forEach((key) => {
if (key === '_id') // hmmm, I am dubious of much more of this
return;
var childPath = `${path}/${child._id}/${key}`;
var matchInfo = router.match(childPath, routes);
var handler = matchInfo.route.handler;
if (handler.transform)
child[key] = handler.transform(child[key]);
});
return child;
});
};
var addIdForDirectRefToListChild = (val, route, path) => {
if (route.parent && route.parent.handler == 'list')
val._id = path.split('/').reverse()[0];
};
var error = (shouldThrow, message) => {
if (shouldThrow)
throw new Error(`FirebaseSchema Error: ${message}`);
};
var getLast = (arr) => {
return arr[arr.length - 1];
};
var validatePush = (obj, path, routes, matchInfo) => {
error(getLast(matchInfo.handlers) !== list, `Can't push to "${path}", use the "list" type.`);
Object.keys(obj).forEach((key) => {
var childPath = `${path}/:id/${key}`;
validateChild(obj[key], childPath, routes, matchInfo);
});
};
var validateHash = (obj, path, routes, matchInfo) => {
Object.keys(obj).forEach((key) => {
var childPath = `${path}/${key}`;
validateChild(obj[key], childPath, routes, matchInfo);
});
};
var validateChild = (val, path, routes, matchInfo) => {
var matchInfo = router.match(path, routes);
error(matchInfo === null, `No child path defined at "${path}"`);
var handler = getLast(matchInfo.handlers);
handler.validate(val, path);
};
var addIndexes = (val, matchInfo, depth) => {
depth = depth === undefined ? 0 : depth;
if (Array.isArray(val)) {
val.forEach(v => addIndexes(v, matchInfo, depth + 1));
return;
}
var path = matchInfo.path;
var dependencies = [];
recurseRoutes(matchInfo.route.children, (route) => {
if (route.handler == 'index') {
var relativePath = shrinkPath(route.handler.getPath(), depth);
var refPath = resolve('/', path, relativePath).substr(1);
var valueKey = route.path.replace(route.parent.path, '').substr(1);
var childRoute = route.children[0];
var childKey = childRoute.path.replace(childRoute.parent.path, '').substr(2)
if (!val[valueKey]) // nothing in the index yet
return;
var relationships = Object.keys(val[valueKey]).reduce((relationships, key) => {
relationships.push(`${refPath}/${key}`);
return relationships;
}, []);
val._indexes = val._indexes || {};
val._indexes[valueKey] = relationships;
}
});
};
var shrinkPath = (path, depth) => {
var c = depth;
while (c > 0) {
path = path.replace(/^..\//, '');
c--;
}
return path;
};
var addRelationships = (val, matchInfo, depth) => {
// sometimes I know what I'm doing, other times there's this.
// Might have some recursion issues? My data isn't that nested
// so I don't know/don't care ... yet.
depth = depth === undefined ? 0 : depth;
if (Array.isArray(val)) {
val.forEach((v) => addRelationships(v, matchInfo, depth + 1));
return;
}
var path = matchInfo.path;
var dependencies = []; // terrible name
recurseRoutes(matchInfo.route.children, (route) => {
if (route.handler == 'key') {
var relativePath = shrinkPath(route.handler.getPath(), depth);
dependencies.push({
refPath: resolve('/', path, relativePath).substr(1),
valueKey: route.path.replace(route.parent.path, '').substr(1)
})
}
});
if (dependencies.length === 0)
return
var params = dependencies.reduce((params, dependency) => {
var keys = [];
pathToRegexp(dependency.refPath, keys);
keys.forEach((key) => {
params[key.name] = val[dependency.valueKey];
});
return params;
}, {});
var links = dependencies.reduce((links, dependency) => {
links[dependency.valueKey] = replaceParams(dependency.refPath, params);
return links;
}, {});
Object.keys(params).forEach((param) => {
if (params[param] === undefined)
console.warn(`Relationship Error: missing key "${param}" at path "${path}":`, val);
});
val._links = links;
};
var replaceParams = (path, params) => {
for (var key in params)
path = path.replace(':'+key, params[key]);
return path;
};
var recurseRoutes = (routes, iterate) => {
routes.forEach((route) => {
iterate(route);
if (route.children)
recurseRoutes(route.children, iterate);
});
};
exports.create = (Firebase, host, getRoutes) => {
var routes = router.map(getRoutes);
var createRef = path => createRefForReals(Firebase, path, routes, host);
return { createRef };
};
exports.Types = {
string: isTypeof('string'),
number: isTypeof('number'),
boolean: isTypeof('boolean'),
list,
hash,
index,
key
};