@@ -62,18 +62,6 @@ module.exports =
62
62
var unique = __webpack_require__ ( 5 ) ;
63
63
var toString = __webpack_require__ ( 6 ) ;
64
64
var DSUtils = JSData . DSUtils ;
65
- var keys = DSUtils . keys ;
66
- var isEmpty = DSUtils . isEmpty ;
67
- var upperCase = DSUtils . upperCase ;
68
- var omit = DSUtils . omit ;
69
- var contains = DSUtils . contains ;
70
- var forOwn = DSUtils . forOwn ;
71
- var deepMixIn = DSUtils . deepMixIn ;
72
- var filter = DSUtils . filter ;
73
- var forEach = DSUtils . forEach ;
74
- var isObject = DSUtils . isObject ;
75
- var isString = DSUtils . isString ;
76
- var removeCircular = DSUtils . removeCircular ;
77
65
78
66
var reserved = [ 'orderBy' , 'sort' , 'limit' , 'offset' , 'skip' , 'where' ] ;
79
67
@@ -84,10 +72,10 @@ module.exports =
84
72
params . orderBy = params . orderBy || params . sort ;
85
73
params . skip = params . skip || params . offset ;
86
74
87
- forEach ( keys ( params ) , function ( k ) {
75
+ DSUtils . forEach ( DSUtils . keys ( params ) , function ( k ) {
88
76
var v = params [ k ] ;
89
- if ( ! contains ( reserved , k ) ) {
90
- if ( isObject ( v ) ) {
77
+ if ( ! DSUtils . contains ( reserved , k ) ) {
78
+ if ( DSUtils . isObject ( v ) ) {
91
79
params . where [ k ] = v ;
92
80
} else {
93
81
params . where [ k ] = {
@@ -98,14 +86,14 @@ module.exports =
98
86
}
99
87
} ) ;
100
88
101
- if ( ! isEmpty ( params . where ) ) {
102
- forOwn ( params . where , function ( criteria , field ) {
103
- if ( ! isObject ( criteria ) ) {
89
+ if ( ! DSUtils . isEmpty ( params . where ) ) {
90
+ DSUtils . forOwn ( params . where , function ( criteria , field ) {
91
+ if ( ! DSUtils . isObject ( criteria ) ) {
104
92
params . where [ field ] = {
105
93
'==' : criteria
106
94
} ;
107
95
}
108
- forOwn ( criteria , function ( v , op ) {
96
+ DSUtils . forOwn ( criteria , function ( v , op ) {
109
97
if ( op === '==' || op === '===' ) {
110
98
query = query . where ( field , v ) ;
111
99
} else if ( op === '!=' || op === '!==' ) {
@@ -156,14 +144,14 @@ module.exports =
156
144
}
157
145
158
146
if ( params . orderBy ) {
159
- if ( isString ( params . orderBy ) ) {
147
+ if ( DSUtils . isString ( params . orderBy ) ) {
160
148
params . orderBy = [ [ params . orderBy , 'asc' ] ] ;
161
149
}
162
150
for ( var i = 0 ; i < params . orderBy . length ; i ++ ) {
163
- if ( isString ( params . orderBy [ i ] ) ) {
151
+ if ( DSUtils . isString ( params . orderBy [ i ] ) ) {
164
152
params . orderBy [ i ] = [ params . orderBy [ i ] , 'asc' ] ;
165
153
}
166
- query = upperCase ( params . orderBy [ i ] [ 1 ] ) === 'DESC' ? query . orderBy ( params . orderBy [ i ] [ 0 ] , 'desc' ) : query . orderBy ( params . orderBy [ i ] [ 0 ] , 'asc' ) ;
154
+ query = DSUtils . upperCase ( params . orderBy [ i ] [ 1 ] ) === 'DESC' ? query . orderBy ( params . orderBy [ i ] [ 0 ] , 'desc' ) : query . orderBy ( params . orderBy [ i ] [ 0 ] , 'asc' ) ;
167
155
}
168
156
}
169
157
@@ -189,7 +177,7 @@ module.exports =
189
177
} else {
190
178
this . query = knex ( options ) ;
191
179
}
192
- deepMixIn ( this . defaults , options ) ;
180
+ DSUtils . deepMixIn ( this . defaults , options ) ;
193
181
}
194
182
195
183
_createClass ( DSSqlAdapter , [ {
@@ -198,7 +186,6 @@ module.exports =
198
186
var _this = this ;
199
187
200
188
var instance = undefined ;
201
- var fields = [ ] ;
202
189
options = options || { } ;
203
190
options [ 'with' ] = options [ 'with' ] || [ ] ;
204
191
return this . query . select ( '*' ) . from ( resourceConfig . table || underscore ( resourceConfig . name ) ) . where ( resourceConfig . idAttribute , toString ( id ) ) . then ( function ( rows ) {
@@ -209,39 +196,67 @@ module.exports =
209
196
instance = rows [ 0 ] ;
210
197
var tasks = [ ] ;
211
198
212
- forEach ( resourceConfig . relationList , function ( def ) {
199
+ DSUtils . forEach ( resourceConfig . relationList , function ( def ) {
213
200
var relationName = def . relation ;
214
- if ( contains ( options [ 'with' ] , relationName ) || contains ( options [ 'with' ] , def . localField ) ) {
215
- DSUtils . remove ( options [ 'with' ] , relationName ) ;
216
- var task = undefined ;
217
- var params = { } ;
218
- if ( resourceConfig . allowSimpleWhere ) {
219
- params [ def . foreignKey ] = instance [ resourceConfig . idAttribute ] ;
220
- } else {
221
- params . where = { } ;
222
- params . where [ def . foreignKey ] = {
223
- '==' : instance [ resourceConfig . idAttribute ]
224
- } ;
225
- }
201
+ var relationDef = resourceConfig . getResource ( relationName ) ;
202
+ var containedName = null ;
203
+ if ( DSUtils . contains ( options [ 'with' ] , relationName ) ) {
204
+ containedName = relationName ;
205
+ } else if ( DSUtils . contains ( options [ 'with' ] , def . localField ) ) {
206
+ containedName = def . localField ;
207
+ }
208
+ if ( containedName ) {
209
+ ( function ( ) {
210
+ var __options = DSUtils . deepMixIn ( { } , options . orig ? options . orig ( ) : options ) ;
211
+ __options = DSUtils . _ ( relationDef , __options ) ;
212
+ DSUtils . remove ( __options [ 'with' ] , containedName ) ;
213
+ DSUtils . forEach ( __options [ 'with' ] , function ( relation , i ) {
214
+ if ( relation && relation . indexOf ( containedName ) === 0 && relation . length >= containedName . length && relation [ containedName . length ] === '.' ) {
215
+ __options [ 'with' ] [ i ] = relation . substr ( containedName . length + 1 ) ;
216
+ }
217
+ } ) ;
226
218
227
- if ( def . type === 'hasMany' && params [ def . foreignKey ] ) {
228
- task = _this . findAll ( resourceConfig . getResource ( relationName ) , params , options ) ;
229
- } else if ( def . type === 'hasOne' ) {
230
- if ( def . localKey && DSUtils . get ( instance , def . localKey ) ) {
231
- task = _this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , options ) ;
232
- } else if ( def . foreignKey && params [ def . foreignKey ] ) {
233
- task = _this . findAll ( resourceConfig . getResource ( relationName ) , params , options ) . then ( function ( hasOnes ) {
234
- return hasOnes . length ? hasOnes [ 0 ] : null ;
219
+ var task = undefined ;
220
+
221
+ if ( ( def . type === 'hasOne' || def . type === 'hasMany' ) && def . foreignKey ) {
222
+ task = _this . findAll ( resourceConfig . getResource ( relationName ) , {
223
+ where : _defineProperty ( { } , def . foreignKey , {
224
+ '==' : instance [ resourceConfig . idAttribute ]
225
+ } )
226
+ } , __options ) . then ( function ( relatedItems ) {
227
+ if ( def . type === 'hasOne' && relatedItems . length ) {
228
+ DSUtils . set ( instance , def . localField , relatedItems [ 0 ] ) ;
229
+ } else {
230
+ DSUtils . set ( instance , def . localField , relatedItems ) ;
231
+ }
232
+ return relatedItems ;
233
+ } ) ;
234
+ } else if ( def . type === 'hasMany' && def . localKeys ) {
235
+ var localKeys = [ ] ;
236
+ var itemKeys = instance [ def . localKeys ] || [ ] ;
237
+ itemKeys = Array . isArray ( itemKeys ) ? itemKeys : DSUtils . keys ( itemKeys ) ;
238
+ localKeys = localKeys . concat ( itemKeys || [ ] ) ;
239
+ task = _this . findAll ( resourceConfig . getResource ( relationName ) , {
240
+ where : _defineProperty ( { } , relationDef . idAttribute , {
241
+ 'in' : DSUtils . filter ( unique ( localKeys ) , function ( x ) {
242
+ return x ;
243
+ } )
244
+ } )
245
+ } , __options ) . then ( function ( relatedItems ) {
246
+ DSUtils . set ( instance , def . localField , relatedItems ) ;
247
+ return relatedItems ;
248
+ } ) ;
249
+ } else if ( def . type === 'belongsTo' || def . type === 'hasOne' && def . localKey ) {
250
+ task = _this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , __options ) . then ( function ( relatedItem ) {
251
+ DSUtils . set ( instance , def . localField , relatedItem ) ;
252
+ return relatedItem ;
235
253
} ) ;
236
254
}
237
- } else if ( DSUtils . get ( instance , def . localKey ) ) {
238
- task = _this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , options ) ;
239
- }
240
255
241
- if ( task ) {
242
- tasks . push ( task ) ;
243
- fields . push ( def . localField ) ;
244
- }
256
+ if ( task ) {
257
+ tasks . push ( task ) ;
258
+ }
259
+ } ) ( ) ;
245
260
}
246
261
} ) ;
247
262
@@ -252,10 +267,7 @@ module.exports =
252
267
253
268
if ( typeof _ret === 'object' ) return _ret . v ;
254
269
}
255
- } ) . then ( function ( loadedRelations ) {
256
- forEach ( fields , function ( field , index ) {
257
- return DSUtils . set ( instance , field , loadedRelations [ index ] ) ;
258
- } ) ;
270
+ } ) . then ( function ( ) {
259
271
return instance ;
260
272
} ) ;
261
273
}
@@ -270,21 +282,21 @@ module.exports =
270
282
return filterQuery . call ( this , resourceConfig , params , options ) . then ( function ( _items ) {
271
283
items = _items ;
272
284
var tasks = [ ] ;
273
- forEach ( resourceConfig . relationList , function ( def ) {
285
+ DSUtils . forEach ( resourceConfig . relationList , function ( def ) {
274
286
var relationName = def . relation ;
275
287
var relationDef = resourceConfig . getResource ( relationName ) ;
276
288
var containedName = null ;
277
- if ( contains ( options [ 'with' ] , relationName ) ) {
289
+ if ( DSUtils . contains ( options [ 'with' ] , relationName ) ) {
278
290
containedName = relationName ;
279
- } else if ( contains ( options [ 'with' ] , def . localField ) ) {
291
+ } else if ( DSUtils . contains ( options [ 'with' ] , def . localField ) ) {
280
292
containedName = def . localField ;
281
293
}
282
294
if ( containedName ) {
283
295
( function ( ) {
284
296
var __options = DSUtils . deepMixIn ( { } , options . orig ? options . orig ( ) : options ) ;
285
297
__options = DSUtils . _ ( relationDef , __options ) ;
286
298
DSUtils . remove ( __options [ 'with' ] , containedName ) ;
287
- forEach ( __options [ 'with' ] , function ( relation , i ) {
299
+ DSUtils . forEach ( __options [ 'with' ] , function ( relation , i ) {
288
300
if ( relation && relation . indexOf ( containedName ) === 0 && relation . length >= containedName . length && relation [ containedName . length ] === '.' ) {
289
301
__options [ 'with' ] [ i ] = relation . substr ( containedName . length + 1 ) ;
290
302
}
@@ -295,16 +307,16 @@ module.exports =
295
307
if ( ( def . type === 'hasOne' || def . type === 'hasMany' ) && def . foreignKey ) {
296
308
task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
297
309
where : _defineProperty ( { } , def . foreignKey , {
298
- 'in' : filter ( map ( items , function ( item ) {
310
+ 'in' : DSUtils . filter ( map ( items , function ( item ) {
299
311
return DSUtils . get ( item , resourceConfig . idAttribute ) ;
300
312
} ) , function ( x ) {
301
313
return x ;
302
314
} )
303
315
} )
304
316
} , __options ) . then ( function ( relatedItems ) {
305
- forEach ( items , function ( item ) {
317
+ DSUtils . forEach ( items , function ( item ) {
306
318
var attached = [ ] ;
307
- forEach ( relatedItems , function ( relatedItem ) {
319
+ DSUtils . forEach ( relatedItems , function ( relatedItem ) {
308
320
if ( DSUtils . get ( relatedItem , def . foreignKey ) === item [ resourceConfig . idAttribute ] ) {
309
321
attached . push ( relatedItem ) ;
310
322
}
@@ -320,24 +332,24 @@ module.exports =
320
332
} else if ( def . type === 'hasMany' && def . localKeys ) {
321
333
( function ( ) {
322
334
var localKeys = [ ] ;
323
- forEach ( items , function ( item ) {
335
+ DSUtils . forEach ( items , function ( item ) {
324
336
var itemKeys = item [ def . localKeys ] || [ ] ;
325
- itemKeys = Array . isArray ( itemKeys ) ? itemKeys : keys ( itemKeys ) ;
337
+ itemKeys = Array . isArray ( itemKeys ) ? itemKeys : DSUtils . keys ( itemKeys ) ;
326
338
localKeys = localKeys . concat ( itemKeys || [ ] ) ;
327
339
} ) ;
328
340
task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
329
341
where : _defineProperty ( { } , relationDef . idAttribute , {
330
- 'in' : filter ( unique ( localKeys ) , function ( x ) {
342
+ 'in' : DSUtils . filter ( unique ( localKeys ) , function ( x ) {
331
343
return x ;
332
344
} )
333
345
} )
334
346
} , __options ) . then ( function ( relatedItems ) {
335
- forEach ( items , function ( item ) {
347
+ DSUtils . forEach ( items , function ( item ) {
336
348
var attached = [ ] ;
337
349
var itemKeys = item [ def . localKeys ] || [ ] ;
338
- itemKeys = Array . isArray ( itemKeys ) ? itemKeys : keys ( itemKeys ) ;
339
- forEach ( relatedItems , function ( relatedItem ) {
340
- if ( itemKeys && contains ( itemKeys , relatedItem [ relationDef . idAttribute ] ) ) {
350
+ itemKeys = Array . isArray ( itemKeys ) ? itemKeys : DSUtils . keys ( itemKeys ) ;
351
+ DSUtils . forEach ( relatedItems , function ( relatedItem ) {
352
+ if ( itemKeys && DSUtils . contains ( itemKeys , relatedItem [ relationDef . idAttribute ] ) ) {
341
353
attached . push ( relatedItem ) ;
342
354
}
343
355
} ) ;
@@ -349,15 +361,15 @@ module.exports =
349
361
} else if ( def . type === 'belongsTo' || def . type === 'hasOne' && def . localKey ) {
350
362
task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
351
363
where : _defineProperty ( { } , relationDef . idAttribute , {
352
- 'in' : filter ( map ( items , function ( item ) {
364
+ 'in' : DSUtils . filter ( map ( items , function ( item ) {
353
365
return DSUtils . get ( item , def . localKey ) ;
354
366
} ) , function ( x ) {
355
367
return x ;
356
368
} )
357
369
} )
358
370
} , __options ) . then ( function ( relatedItems ) {
359
- forEach ( items , function ( item ) {
360
- forEach ( relatedItems , function ( relatedItem ) {
371
+ DSUtils . forEach ( items , function ( item ) {
372
+ DSUtils . forEach ( relatedItems , function ( relatedItem ) {
361
373
if ( relatedItem [ relationDef . idAttribute ] === item [ def . localKey ] ) {
362
374
DSUtils . set ( item , def . localField , relatedItem ) ;
363
375
}
@@ -383,7 +395,7 @@ module.exports =
383
395
value : function create ( resourceConfig , attrs ) {
384
396
var _this3 = this ;
385
397
386
- attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
398
+ attrs = DSUtils . removeCircular ( DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
387
399
return this . query ( resourceConfig . table || underscore ( resourceConfig . name ) ) . insert ( attrs , resourceConfig . idAttribute ) . then ( function ( ids ) {
388
400
if ( attrs [ resourceConfig . idAttribute ] ) {
389
401
return _this3 . find ( resourceConfig , attrs [ resourceConfig . idAttribute ] ) ;
@@ -399,7 +411,7 @@ module.exports =
399
411
value : function update ( resourceConfig , id , attrs ) {
400
412
var _this4 = this ;
401
413
402
- attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
414
+ attrs = DSUtils . removeCircular ( DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
403
415
return this . query ( resourceConfig . table || underscore ( resourceConfig . name ) ) . where ( resourceConfig . idAttribute , toString ( id ) ) . update ( attrs ) . then ( function ( ) {
404
416
return _this4 . find ( resourceConfig , id ) ;
405
417
} ) ;
@@ -409,7 +421,7 @@ module.exports =
409
421
value : function updateAll ( resourceConfig , attrs , params , options ) {
410
422
var _this5 = this ;
411
423
412
- attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
424
+ attrs = DSUtils . removeCircular ( DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
413
425
return filterQuery . call ( this , resourceConfig , params , options ) . then ( function ( items ) {
414
426
return map ( items , function ( item ) {
415
427
return item [ resourceConfig . idAttribute ] ;
0 commit comments