-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.js
83 lines (70 loc) · 2.24 KB
/
schemas.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
function init(sugar, engine) {
var schema = sugar.schema(engine);
var ref = sugar.ref;
var refs = sugar.refs;
var unique = sugar.unique;
var mixed = sugar.mixed;
var schemas = {};
schema(schemas, 'Middleware').fields({
pre: Boolean,
post: Boolean
});
// TODO: figure out a nice spec for middleware and implement tests
/*
Middleware.pre(function() {
Middleware.use(function(next) {
sugar.getAll(Middleware, {}, function(err, d) {
if(err) console.error(err);
console.log('setting pre true', d);
// TODO: set pre of each true. now setting just first
// TODO: implement batch update?
if(d[0]) sugar.update(Middleware, d[0]._id, {pre: true}, next);
else next();
});
});
});
Middleware.post(function() {
Middleware.use(function(next, data) {
console.log('setting post true', data);
sugar.update(Middleware, data._id, {
post: true
}, function(err) {
if(err) console.error(err);
next();
});
});
});*/
schema(schemas, 'Author').fields({
name: {type: String, required: true},
nick: String,
secret: {type: Number, unique: true},
extra: mixed()
});
schema(schemas, 'License').fields({
name: {type: String, unique: true}
});
schema(schemas, 'Library').fields({
author: ref('Author', {required: true}),
name: {type: String, required: true},
licenses: refs('License')
});
// this a bit contrived example illustrates how to use
// schemas as sort of templates. this schema is needed
// for testing delete cascade as well
schema(schemas, 'LibraryAuthor').fields({
library: ref('Library', {required: true}),
author: ref('Author', {required: true}),
role: String
});
// nested definition
schema(schemas, 'Engine').fields({
name: {type: String, required: true},
features: {
'fast': Boolean,
'robust': Boolean,
'neat': Boolean
}
});
return schemas;
}
module.exports = init;