@@ -62,18 +62,6 @@ module.exports =
6262 var unique = __webpack_require__ ( 5 ) ;
6363 var toString = __webpack_require__ ( 6 ) ;
6464 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 ;
7765
7866 var reserved = [ 'orderBy' , 'sort' , 'limit' , 'offset' , 'skip' , 'where' ] ;
7967
@@ -84,10 +72,10 @@ module.exports =
8472 params . orderBy = params . orderBy || params . sort ;
8573 params . skip = params . skip || params . offset ;
8674
87- forEach ( keys ( params ) , function ( k ) {
75+ DSUtils . forEach ( DSUtils . keys ( params ) , function ( k ) {
8876 var v = params [ k ] ;
89- if ( ! contains ( reserved , k ) ) {
90- if ( isObject ( v ) ) {
77+ if ( ! DSUtils . contains ( reserved , k ) ) {
78+ if ( DSUtils . isObject ( v ) ) {
9179 params . where [ k ] = v ;
9280 } else {
9381 params . where [ k ] = {
@@ -98,14 +86,14 @@ module.exports =
9886 }
9987 } ) ;
10088
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 ) ) {
10492 params . where [ field ] = {
10593 '==' : criteria
10694 } ;
10795 }
108- forOwn ( criteria , function ( v , op ) {
96+ DSUtils . forOwn ( criteria , function ( v , op ) {
10997 if ( op === '==' || op === '===' ) {
11098 query = query . where ( field , v ) ;
11199 } else if ( op === '!=' || op === '!==' ) {
@@ -156,14 +144,14 @@ module.exports =
156144 }
157145
158146 if ( params . orderBy ) {
159- if ( isString ( params . orderBy ) ) {
147+ if ( DSUtils . isString ( params . orderBy ) ) {
160148 params . orderBy = [ [ params . orderBy , 'asc' ] ] ;
161149 }
162150 for ( var i = 0 ; i < params . orderBy . length ; i ++ ) {
163- if ( isString ( params . orderBy [ i ] ) ) {
151+ if ( DSUtils . isString ( params . orderBy [ i ] ) ) {
164152 params . orderBy [ i ] = [ params . orderBy [ i ] , 'asc' ] ;
165153 }
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' ) ;
167155 }
168156 }
169157
@@ -189,7 +177,7 @@ module.exports =
189177 } else {
190178 this . query = knex ( options ) ;
191179 }
192- deepMixIn ( this . defaults , options ) ;
180+ DSUtils . deepMixIn ( this . defaults , options ) ;
193181 }
194182
195183 _createClass ( DSSqlAdapter , [ {
@@ -198,7 +186,6 @@ module.exports =
198186 var _this = this ;
199187
200188 var instance = undefined ;
201- var fields = [ ] ;
202189 options = options || { } ;
203190 options [ 'with' ] = options [ 'with' ] || [ ] ;
204191 return this . query . select ( '*' ) . from ( resourceConfig . table || underscore ( resourceConfig . name ) ) . where ( resourceConfig . idAttribute , toString ( id ) ) . then ( function ( rows ) {
@@ -209,39 +196,67 @@ module.exports =
209196 instance = rows [ 0 ] ;
210197 var tasks = [ ] ;
211198
212- forEach ( resourceConfig . relationList , function ( def ) {
199+ DSUtils . forEach ( resourceConfig . relationList , function ( def ) {
213200 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+ } ) ;
226218
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 ;
235253 } ) ;
236254 }
237- } else if ( DSUtils . get ( instance , def . localKey ) ) {
238- task = _this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , options ) ;
239- }
240255
241- if ( task ) {
242- tasks . push ( task ) ;
243- fields . push ( def . localField ) ;
244- }
256+ if ( task ) {
257+ tasks . push ( task ) ;
258+ }
259+ } ) ( ) ;
245260 }
246261 } ) ;
247262
@@ -252,10 +267,7 @@ module.exports =
252267
253268 if ( typeof _ret === 'object' ) return _ret . v ;
254269 }
255- } ) . then ( function ( loadedRelations ) {
256- forEach ( fields , function ( field , index ) {
257- return DSUtils . set ( instance , field , loadedRelations [ index ] ) ;
258- } ) ;
270+ } ) . then ( function ( ) {
259271 return instance ;
260272 } ) ;
261273 }
@@ -270,21 +282,21 @@ module.exports =
270282 return filterQuery . call ( this , resourceConfig , params , options ) . then ( function ( _items ) {
271283 items = _items ;
272284 var tasks = [ ] ;
273- forEach ( resourceConfig . relationList , function ( def ) {
285+ DSUtils . forEach ( resourceConfig . relationList , function ( def ) {
274286 var relationName = def . relation ;
275287 var relationDef = resourceConfig . getResource ( relationName ) ;
276288 var containedName = null ;
277- if ( contains ( options [ 'with' ] , relationName ) ) {
289+ if ( DSUtils . contains ( options [ 'with' ] , relationName ) ) {
278290 containedName = relationName ;
279- } else if ( contains ( options [ 'with' ] , def . localField ) ) {
291+ } else if ( DSUtils . contains ( options [ 'with' ] , def . localField ) ) {
280292 containedName = def . localField ;
281293 }
282294 if ( containedName ) {
283295 ( function ( ) {
284296 var __options = DSUtils . deepMixIn ( { } , options . orig ? options . orig ( ) : options ) ;
285297 __options = DSUtils . _ ( relationDef , __options ) ;
286298 DSUtils . remove ( __options [ 'with' ] , containedName ) ;
287- forEach ( __options [ 'with' ] , function ( relation , i ) {
299+ DSUtils . forEach ( __options [ 'with' ] , function ( relation , i ) {
288300 if ( relation && relation . indexOf ( containedName ) === 0 && relation . length >= containedName . length && relation [ containedName . length ] === '.' ) {
289301 __options [ 'with' ] [ i ] = relation . substr ( containedName . length + 1 ) ;
290302 }
@@ -295,16 +307,16 @@ module.exports =
295307 if ( ( def . type === 'hasOne' || def . type === 'hasMany' ) && def . foreignKey ) {
296308 task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
297309 where : _defineProperty ( { } , def . foreignKey , {
298- 'in' : filter ( map ( items , function ( item ) {
310+ 'in' : DSUtils . filter ( map ( items , function ( item ) {
299311 return DSUtils . get ( item , resourceConfig . idAttribute ) ;
300312 } ) , function ( x ) {
301313 return x ;
302314 } )
303315 } )
304316 } , __options ) . then ( function ( relatedItems ) {
305- forEach ( items , function ( item ) {
317+ DSUtils . forEach ( items , function ( item ) {
306318 var attached = [ ] ;
307- forEach ( relatedItems , function ( relatedItem ) {
319+ DSUtils . forEach ( relatedItems , function ( relatedItem ) {
308320 if ( DSUtils . get ( relatedItem , def . foreignKey ) === item [ resourceConfig . idAttribute ] ) {
309321 attached . push ( relatedItem ) ;
310322 }
@@ -320,24 +332,24 @@ module.exports =
320332 } else if ( def . type === 'hasMany' && def . localKeys ) {
321333 ( function ( ) {
322334 var localKeys = [ ] ;
323- forEach ( items , function ( item ) {
335+ DSUtils . forEach ( items , function ( item ) {
324336 var itemKeys = item [ def . localKeys ] || [ ] ;
325- itemKeys = Array . isArray ( itemKeys ) ? itemKeys : keys ( itemKeys ) ;
337+ itemKeys = Array . isArray ( itemKeys ) ? itemKeys : DSUtils . keys ( itemKeys ) ;
326338 localKeys = localKeys . concat ( itemKeys || [ ] ) ;
327339 } ) ;
328340 task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
329341 where : _defineProperty ( { } , relationDef . idAttribute , {
330- 'in' : filter ( unique ( localKeys ) , function ( x ) {
342+ 'in' : DSUtils . filter ( unique ( localKeys ) , function ( x ) {
331343 return x ;
332344 } )
333345 } )
334346 } , __options ) . then ( function ( relatedItems ) {
335- forEach ( items , function ( item ) {
347+ DSUtils . forEach ( items , function ( item ) {
336348 var attached = [ ] ;
337349 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 ] ) ) {
341353 attached . push ( relatedItem ) ;
342354 }
343355 } ) ;
@@ -349,15 +361,15 @@ module.exports =
349361 } else if ( def . type === 'belongsTo' || def . type === 'hasOne' && def . localKey ) {
350362 task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
351363 where : _defineProperty ( { } , relationDef . idAttribute , {
352- 'in' : filter ( map ( items , function ( item ) {
364+ 'in' : DSUtils . filter ( map ( items , function ( item ) {
353365 return DSUtils . get ( item , def . localKey ) ;
354366 } ) , function ( x ) {
355367 return x ;
356368 } )
357369 } )
358370 } , __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 ) {
361373 if ( relatedItem [ relationDef . idAttribute ] === item [ def . localKey ] ) {
362374 DSUtils . set ( item , def . localField , relatedItem ) ;
363375 }
@@ -383,7 +395,7 @@ module.exports =
383395 value : function create ( resourceConfig , attrs ) {
384396 var _this3 = this ;
385397
386- attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
398+ attrs = DSUtils . removeCircular ( DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
387399 return this . query ( resourceConfig . table || underscore ( resourceConfig . name ) ) . insert ( attrs , resourceConfig . idAttribute ) . then ( function ( ids ) {
388400 if ( attrs [ resourceConfig . idAttribute ] ) {
389401 return _this3 . find ( resourceConfig , attrs [ resourceConfig . idAttribute ] ) ;
@@ -399,7 +411,7 @@ module.exports =
399411 value : function update ( resourceConfig , id , attrs ) {
400412 var _this4 = this ;
401413
402- attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
414+ attrs = DSUtils . removeCircular ( DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
403415 return this . query ( resourceConfig . table || underscore ( resourceConfig . name ) ) . where ( resourceConfig . idAttribute , toString ( id ) ) . update ( attrs ) . then ( function ( ) {
404416 return _this4 . find ( resourceConfig , id ) ;
405417 } ) ;
@@ -409,7 +421,7 @@ module.exports =
409421 value : function updateAll ( resourceConfig , attrs , params , options ) {
410422 var _this5 = this ;
411423
412- attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
424+ attrs = DSUtils . removeCircular ( DSUtils . omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
413425 return filterQuery . call ( this , resourceConfig , params , options ) . then ( function ( items ) {
414426 return map ( items , function ( item ) {
415427 return item [ resourceConfig . idAttribute ] ;
0 commit comments