| 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 |
3×
3×
3×
3×
3×
155×
102×
102×
102×
102×
102×
102×
102×
102×
102×
981×
981×
981×
981×
981×
981×
981×
981×
981×
981×
11×
11×
970×
697×
697×
697×
697×
697×
697×
697×
697×
309×
309×
309×
309×
309×
309×
309×
309×
299×
180×
5×
5×
5×
5×
5×
2×
2×
5×
5×
5×
2×
2×
2×
2×
306×
306×
306×
306×
306×
306×
306×
306×
306×
306×
306×
12×
12×
294×
74×
74×
74×
74×
74×
74×
74×
| /**
* Module Dependencies
*/
var normalize = require('../utils/normalize');
var schema = require('../utils/schema');
var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
var _ = require('lodash');
/**
* DQL Adapter Normalization
*/
module.exports = {
hasJoin: function() {
return hasOwnProperty(this.dictionary, 'join');
},
/**
* join()
*
* If `join` is defined in the adapter, Waterline will use it to optimize
* the `.populate()` implementation when joining collections within the same
* database connection.
*
* @param {[type]} criteria
* @param {Function} cb
*/
join: function(criteria, cb, metaContainer) {
// Normalize Arguments
criteria = normalize.criteria(criteria);
cb = normalize.callback(cb);
// Build Default Error Message
var err = 'No join() method defined in adapter!';
// Find the connection to run this on
Iif (!hasOwnProperty(this.dictionary, 'join')) return cb(new Error(err));
var connName = this.dictionary.join;
var adapter = this.connections[connName]._adapter;
Iif (!hasOwnProperty(adapter, 'join')) return cb(new Error(err));
// Parse Join Criteria and set references to any collection tableName properties.
// This is done here so that everywhere else in the codebase can use the collection identity.
criteria = schema.serializeJoins(criteria, this.query.waterline.schema);
adapter.join(connName, this.collection, criteria, cb, metaContainer);
},
/**
* create()
*
* Create one or more models.
*
* @param {[type]} values [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
create: function(values, cb, metaContainer) {
var globalId = this.query.globalId;
// Normalize Arguments
cb = normalize.callback(cb);
Iif (Array.isArray(values)) {
return this.createEach.call(this, values, cb, metaContainer);
}
// Build Default Error Message
var err = 'No create() method defined in adapter!';
// Find the connection to run this on
Iif (!hasOwnProperty(this.dictionary, 'create')) return cb(new Error(err));
var connName = this.dictionary.create;
var adapter = this.connections[connName]._adapter;
Iif (!hasOwnProperty(adapter, 'create')) return cb(new Error(err));
adapter.create(connName, this.collection, values, normalize.callback(function afterwards(err, createdRecord) {
if (err) {
Eif (typeof err === 'object') err.model = globalId;
return cb(err);
}
else return cb(null, createdRecord);
}), metaContainer);
},
/**
* find()
*
* Find a set of models.
*
* @param {[type]} criteria [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
find: function(criteria, cb, metaContainer) {
// Normalize Arguments
criteria = normalize.criteria(criteria);
cb = normalize.callback(cb);
// Build Default Error Message
var err = 'No find() method defined in adapter!';
// Find the connection to run this on
Iif (!hasOwnProperty(this.dictionary, 'find')) return cb(new Error(err));
var connName = this.dictionary.find;
var adapter = this.connections[connName]._adapter;
Iif (!adapter.find) return cb(new Error(err));
adapter.find(connName, this.collection, criteria, cb, metaContainer);
},
/**
* findOne()
*
* Find exactly one model.
*
* @param {[type]} criteria [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
findOne: function(criteria, cb, metaContainer) {
// make shallow copy of criteria so original does not get modified
criteria = _.clone(criteria);
// Normalize Arguments
cb = normalize.callback(cb);
// Build Default Error Message
var err = '.findOne() requires a criteria. If you want the first record try .find().limit(1)';
// If no criteria is specified or where is empty return an error
Iif (!criteria || criteria.where === null) return cb(new Error(err));
// Detects if there is a `findOne` in the adapter. Use it if it exists.
Iif (hasOwnProperty(this.dictionary, 'findOne')) {
var connName = this.dictionary.findOne;
var adapter = this.connections[connName]._adapter;
if (adapter.findOne) {
// Normalize Arguments
criteria = normalize.criteria(criteria);
return adapter.findOne(connName, this.collection, criteria, cb, metaContainer);
}
}
// Fallback to use `find()` to simulate a `findOne()`
// Enforce limit to 1
criteria.limit = 1;
this.find(criteria, function(err, models) {
if (!models) return cb(err);
if (models.length < 1) return cb(err);
cb(null, models);
}, metaContainer);
},
/**
* [count description]
* @param {[type]} criteria [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
count: function(criteria, cb, metaContainer) {
var connName;
// Normalize Arguments
cb = normalize.callback(cb);
criteria = normalize.criteria(criteria);
// Build Default Error Message
var err = '.count() requires the adapter define either a count method or a find method';
// Find the connection to run this on
if (!hasOwnProperty(this.dictionary, 'count')) {
// If a count method isn't defined make sure a find method is
Iif (!hasOwnProperty(this.dictionary, 'find')) return cb(new Error(err));
// Use the find method
connName = this.dictionary.find;
}
if (!connName) connName = this.dictionary.count;
var adapter = this.connections[connName]._adapter;
if (hasOwnProperty(adapter, 'count')) return adapter.count(connName, this.collection, criteria, cb, metaContainer);
this.find(criteria, function(err, models) {
Iif (err) return cb(err);
var count = models && models.length || 0;
cb(err, count);
}, metaContainer);
},
/**
* [update description]
* @param {[type]} criteria [description]
* @param {[type]} values [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
update: function(criteria, values, cb, metaContainer) {
var globalId = this.query.globalId;
// Normalize Arguments
cb = normalize.callback(cb);
criteria = normalize.criteria(criteria);
Iif (criteria === false) {
return cb(null, []);
} else Iif (!criteria) {
return cb(new Error('No criteria or id specified!'));
}
// Build Default Error Message
var err = 'No update() method defined in adapter!';
// Find the connection to run this on
Iif (!hasOwnProperty(this.dictionary, 'update')) return cb(new Error(err));
var connName = this.dictionary.update;
var adapter = this.connections[connName]._adapter;
adapter.update(connName, this.collection, criteria, values, normalize.callback(function afterwards(err, updatedRecords) {
if (err) {
Eif (typeof err === 'object') err.model = globalId;
return cb(err);
}
return cb(null, updatedRecords);
}), metaContainer);
},
/**
* [destroy description]
* @param {[type]} criteria [description]
* @param {Function} cb [description]
* @return {[type]} [description]
*/
destroy: function(criteria, cb, metaContainer) {
// Normalize Arguments
cb = normalize.callback(cb);
criteria = normalize.criteria(criteria);
// Build Default Error Message
var err = 'No destroy() method defined in adapter!';
// Find the connection to run this on
Iif (!hasOwnProperty(this.dictionary, 'destroy')) return cb(new Error(err));
var connName = this.dictionary.destroy;
var adapter = this.connections[connName]._adapter;
adapter.destroy(connName, this.collection, criteria, cb, metaContainer);
}
};
|