forked from balderdashy/waterline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
226 lines (189 loc) · 4.89 KB
/
schema.js
File metadata and controls
226 lines (189 loc) · 4.89 KB
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
/**
* Dependencies
*/
var _ = require('lodash'),
types = require('./types'),
callbacks = require('./callbacks'),
hasOwnProperty = require('./helpers').object.hasOwnProperty;
/**
* Expose schema
*/
var schema = module.exports = exports;
/**
* Iterate over `attrs` normalizing string values to the proper
* attribute object.
*
* Example:
* {
* name: 'STRING',
* age: {
* type: 'INTEGER'
* }
* }
*
* Returns:
* {
* name: {
* type: 'string'
* },
* age: {
* type: 'integer'
* }
* }
*
* @param {Object} attrs
* @return {Object}
*/
schema.normalizeAttributes = function(attrs) {
var attributes = {};
Object.keys(attrs).forEach(function(key) {
// Not concerned with functions
if(typeof attrs[key] === 'function') return;
// Expand shorthand type
if(typeof attrs[key] === 'string') {
attributes[key] = { type: attrs[key] };
} else {
attributes[key] = attrs[key];
}
// Ensure type is lower case
if(attributes[key].type && typeof attributes[key].type !== 'undefined') {
attributes[key].type = attributes[key].type.toLowerCase();
}
// Ensure Collection property is lowercased
if(hasOwnProperty(attrs[key], 'collection')) {
attrs[key].collection = attrs[key].collection.toLowerCase();
}
// Ensure Model property is lowercased
if(hasOwnProperty(attrs[key], 'model')) {
attrs[key].model = attrs[key].model.toLowerCase();
}
});
return attributes;
};
/**
* Return all methods in `attrs` that should be provided
* on the model.
*
* Example:
* {
* name: 'string',
* email: 'string',
* doSomething: function() {
* return true;
* }
* }
*
* Returns:
* {
* doSomething: function() {
* return true;
* }
* }
*
* @param {Object} attrs
* @return {Object}
*/
schema.instanceMethods = function(attrs) {
var methods = {};
if(!attrs) return methods;
Object.keys(attrs).forEach(function(key) {
if(typeof attrs[key] === 'function') {
methods[key] = attrs[key];
}
});
return methods;
};
/**
* Normalize callbacks
*
* Return all callback functions in `context`, allows for string mapping to
* functions located in `context.attributes`.
*
* Example:
* {
* attributes: {
* name: 'string',
* email: 'string',
* increment: function increment() { i++; }
* },
* afterCreate: 'increment',
* beforeCreate: function() { return true; }
* }
*
* Returns:
* {
* afterCreate: [
* function increment() { i++; }
* ],
* beforeCreate: [
* function() { return true; }
* ]
* }
*
* @param {Object} context
* @return {Object}
*/
schema.normalizeCallbacks = function(context) {
var i, _i, len, _len, fn, fns = {};
function defaultFn(fn) {
if (fn === 'beforeUpdate' || fn === 'beforeValidate') return function(values, criteria, next) { return next(); };
return function(values, next) { return next(); };
}
for(i = 0, len = callbacks.length; i < len; i = i + 1) {
fn = callbacks[i];
// Skip if the model hasn't defined this callback
if(typeof context[fn] === 'undefined') {
fns[fn] = [ defaultFn(fn) ];
continue;
}
if(Array.isArray(context[fn])) {
fns[fn] = [];
// Iterate over all functions
for(_i = 0, _len = context[fn].length; _i < _len; _i = _i + 1) {
if(typeof context[fn][_i] === 'string') {
// Attempt to map string to function
if(typeof context.attributes[context[fn][_i]] === 'function') {
fns[fn][_i] = context.attributes[context[fn][_i]];
delete context.attributes[context[fn][_i]];
} else {
throw new Error('Unable to locate callback `' + context[fn][_i] + '`');
}
} else {
fns[fn][_i] = context[fn][_i];
}
}
} else if(typeof context[fn] === 'string') {
// Attempt to map string to function
if(typeof context.attributes[context[fn]] === 'function') {
fns[fn] = [ context.attributes[context[fn]] ];
delete context.attributes[context[fn]];
} else {
throw new Error('Unable to locate callback `' + context[fn] + '`');
}
} else {
// Just add a single function
fns[fn] = [ context[fn] ];
}
}
return fns;
};
/**
* Replace any Join Criteria references with the defined tableName for a collection.
*
* @param {Object} criteria
* @param {Object} collections
* @return {Object}
* @api public
*/
schema.serializeJoins = function(criteria, collections) {
if(!criteria.joins) return criteria;
var joins = _.cloneDeep(criteria.joins);
joins.forEach(function(join) {
if(!hasOwnProperty(collections[join.parent], 'tableName')) return;
if(!hasOwnProperty(collections[join.child], 'tableName')) return;
join.parent = collections[join.parent].tableName;
join.child = collections[join.child].tableName;
});
criteria.joins = joins;
return criteria;
};