@@ -51,13 +51,16 @@ module.exports =
51
51
52
52
var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( 'value' in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
53
53
54
+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
55
+
54
56
function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( 'Cannot call a class as a function' ) ; } }
55
57
56
- var knex = __webpack_require__ ( 1 ) ;
57
- var JSData = __webpack_require__ ( 2 ) ;
58
- var map = __webpack_require__ ( 3 ) ;
59
- var underscore = __webpack_require__ ( 4 ) ;
60
- var toString = __webpack_require__ ( 5 ) ;
58
+ var knex = __webpack_require__ ( 2 ) ;
59
+ var JSData = __webpack_require__ ( 3 ) ;
60
+ var map = __webpack_require__ ( 4 ) ;
61
+ var underscore = __webpack_require__ ( 1 ) ;
62
+ var unique = __webpack_require__ ( 5 ) ;
63
+ var toString = __webpack_require__ ( 6 ) ;
61
64
var DSUtils = JSData . DSUtils ;
62
65
var keys = DSUtils . keys ;
63
66
var isEmpty = DSUtils . isEmpty ;
@@ -66,6 +69,7 @@ module.exports =
66
69
var contains = DSUtils . contains ;
67
70
var forOwn = DSUtils . forOwn ;
68
71
var deepMixIn = DSUtils . deepMixIn ;
72
+ var filter = DSUtils . filter ;
69
73
var forEach = DSUtils . forEach ;
70
74
var isObject = DSUtils . isObject ;
71
75
var isString = DSUtils . isString ;
@@ -207,7 +211,8 @@ module.exports =
207
211
208
212
forEach ( resourceConfig . relationList , function ( def ) {
209
213
var relationName = def . relation ;
210
- if ( contains ( options [ 'with' ] , relationName ) ) {
214
+ if ( contains ( options [ 'with' ] , relationName ) || contains ( options [ 'with' ] , def . localField ) ) {
215
+ DSUtils . remove ( options [ 'with' ] , relationName ) ;
211
216
var task = undefined ;
212
217
var params = { } ;
213
218
if ( resourceConfig . allowSimpleWhere ) {
@@ -222,15 +227,15 @@ module.exports =
222
227
if ( def . type === 'hasMany' && params [ def . foreignKey ] ) {
223
228
task = _this . findAll ( resourceConfig . getResource ( relationName ) , params , options ) ;
224
229
} else if ( def . type === 'hasOne' ) {
225
- if ( def . localKey && instance [ def . localKey ] ) {
226
- task = _this . find ( resourceConfig . getResource ( relationName ) , instance [ def . localKey ] , options ) ;
230
+ if ( def . localKey && DSUtils . get ( instance , def . localKey ) ) {
231
+ task = _this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , options ) ;
227
232
} else if ( def . foreignKey && params [ def . foreignKey ] ) {
228
233
task = _this . findAll ( resourceConfig . getResource ( relationName ) , params , options ) . then ( function ( hasOnes ) {
229
234
return hasOnes . length ? hasOnes [ 0 ] : null ;
230
235
} ) ;
231
236
}
232
- } else if ( instance [ def . localKey ] ) {
233
- task = _this . find ( resourceConfig . getResource ( relationName ) , instance [ def . localKey ] , options ) ;
237
+ } else if ( DSUtils . get ( instance , def . localKey ) ) {
238
+ task = _this . find ( resourceConfig . getResource ( relationName ) , DSUtils . get ( instance , def . localKey ) , options ) ;
234
239
}
235
240
236
241
if ( task ) {
@@ -249,27 +254,141 @@ module.exports =
249
254
}
250
255
} ) . then ( function ( loadedRelations ) {
251
256
forEach ( fields , function ( field , index ) {
252
- return instance [ field ] = loadedRelations [ index ] ;
257
+ return DSUtils . set ( instance , field , loadedRelations [ index ] ) ;
253
258
} ) ;
254
259
return instance ;
255
260
} ) ;
256
261
}
257
262
} , {
258
263
key : 'findAll' ,
259
264
value : function findAll ( resourceConfig , params , options ) {
260
- return filterQuery . call ( this , resourceConfig , params , options ) ;
265
+ var _this2 = this ;
266
+
267
+ var items = null ;
268
+ options = options || { } ;
269
+ options [ 'with' ] = options [ 'with' ] || [ ] ;
270
+ return filterQuery . call ( this , resourceConfig , params , options ) . then ( function ( _items ) {
271
+ items = _items ;
272
+ var tasks = [ ] ;
273
+ forEach ( resourceConfig . relationList , function ( def ) {
274
+ var relationName = def . relation ;
275
+ var relationDef = resourceConfig . getResource ( relationName ) ;
276
+ var containedName = null ;
277
+ if ( contains ( options [ 'with' ] , relationName ) ) {
278
+ containedName = relationName ;
279
+ } else if ( contains ( options [ 'with' ] , def . localField ) ) {
280
+ containedName = def . localField ;
281
+ }
282
+ if ( containedName ) {
283
+ ( function ( ) {
284
+ var __options = DSUtils . deepMixIn ( { } , options . orig ? options . orig ( ) : options ) ;
285
+ __options = DSUtils . _ ( relationDef , __options ) ;
286
+ DSUtils . remove ( __options [ 'with' ] , containedName ) ;
287
+ forEach ( __options [ 'with' ] , function ( relation , i ) {
288
+ if ( relation && relation . indexOf ( containedName ) === 0 && relation . length >= containedName . length && relation [ containedName . length ] === '.' ) {
289
+ __options [ 'with' ] [ i ] = relation . substr ( containedName . length + 1 ) ;
290
+ }
291
+ } ) ;
292
+
293
+ var task = undefined ;
294
+
295
+ if ( ( def . type === 'hasOne' || def . type === 'hasMany' ) && def . foreignKey ) {
296
+ task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
297
+ where : _defineProperty ( { } , def . foreignKey , {
298
+ 'in' : filter ( map ( items , function ( item ) {
299
+ return DSUtils . get ( item , resourceConfig . idAttribute ) ;
300
+ } ) , function ( x ) {
301
+ return x ;
302
+ } )
303
+ } )
304
+ } , __options ) . then ( function ( relatedItems ) {
305
+ forEach ( items , function ( item ) {
306
+ var attached = [ ] ;
307
+ forEach ( relatedItems , function ( relatedItem ) {
308
+ if ( DSUtils . get ( relatedItem , def . foreignKey ) === item [ resourceConfig . idAttribute ] ) {
309
+ attached . push ( relatedItem ) ;
310
+ }
311
+ } ) ;
312
+ if ( def . type === 'hasOne' && attached . length ) {
313
+ DSUtils . set ( item , def . localField , attached [ 0 ] ) ;
314
+ } else {
315
+ DSUtils . set ( item , def . localField , attached ) ;
316
+ }
317
+ } ) ;
318
+ return relatedItems ;
319
+ } ) ;
320
+ } else if ( def . type === 'hasMany' && def . localKeys ) {
321
+ ( function ( ) {
322
+ var localKeys = [ ] ;
323
+ forEach ( items , function ( item ) {
324
+ var itemKeys = item [ def . localKeys ] || [ ] ;
325
+ itemKeys = Array . isArray ( itemKeys ) ? itemKeys : keys ( itemKeys ) ;
326
+ localKeys = localKeys . concat ( itemKeys || [ ] ) ;
327
+ } ) ;
328
+ task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
329
+ where : _defineProperty ( { } , relationDef . idAttribute , {
330
+ 'in' : filter ( unique ( localKeys ) , function ( x ) {
331
+ return x ;
332
+ } )
333
+ } )
334
+ } , __options ) . then ( function ( relatedItems ) {
335
+ forEach ( items , function ( item ) {
336
+ var attached = [ ] ;
337
+ 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 ] ) ) {
341
+ attached . push ( relatedItem ) ;
342
+ }
343
+ } ) ;
344
+ DSUtils . set ( item , def . localField , attached ) ;
345
+ } ) ;
346
+ return relatedItems ;
347
+ } ) ;
348
+ } ) ( ) ;
349
+ } else if ( def . type === 'belongsTo' || def . type === 'hasOne' && def . localKey ) {
350
+ task = _this2 . findAll ( resourceConfig . getResource ( relationName ) , {
351
+ where : _defineProperty ( { } , relationDef . idAttribute , {
352
+ 'in' : filter ( map ( items , function ( item ) {
353
+ return DSUtils . get ( item , def . localKey ) ;
354
+ } ) , function ( x ) {
355
+ return x ;
356
+ } )
357
+ } )
358
+ } , __options ) . then ( function ( relatedItems ) {
359
+ forEach ( items , function ( item ) {
360
+ forEach ( relatedItems , function ( relatedItem ) {
361
+ if ( relatedItem [ relationDef . idAttribute ] === item [ def . localKey ] ) {
362
+ DSUtils . set ( item , def . localField , relatedItem ) ;
363
+ }
364
+ } ) ;
365
+ } ) ;
366
+ return relatedItems ;
367
+ } ) ;
368
+ }
369
+
370
+ if ( task ) {
371
+ tasks . push ( task ) ;
372
+ }
373
+ } ) ( ) ;
374
+ }
375
+ } ) ;
376
+ return DSUtils . Promise . all ( tasks ) ;
377
+ } ) . then ( function ( ) {
378
+ return items ;
379
+ } ) ;
261
380
}
262
381
} , {
263
382
key : 'create' ,
264
383
value : function create ( resourceConfig , attrs ) {
265
- var _this2 = this ;
384
+ var _this3 = this ;
266
385
267
386
attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
268
387
return this . query ( resourceConfig . table || underscore ( resourceConfig . name ) ) . insert ( attrs , resourceConfig . idAttribute ) . then ( function ( ids ) {
269
388
if ( attrs [ resourceConfig . idAttribute ] ) {
270
- return _this2 . find ( resourceConfig , attrs [ resourceConfig . idAttribute ] ) ;
389
+ return _this3 . find ( resourceConfig , attrs [ resourceConfig . idAttribute ] ) ;
271
390
} else if ( ids . length ) {
272
- return _this2 . find ( resourceConfig , ids [ 0 ] ) ;
391
+ return _this3 . find ( resourceConfig , ids [ 0 ] ) ;
273
392
} else {
274
393
throw new Error ( 'Failed to create!' ) ;
275
394
}
@@ -278,30 +397,30 @@ module.exports =
278
397
} , {
279
398
key : 'update' ,
280
399
value : function update ( resourceConfig , id , attrs ) {
281
- var _this3 = this ;
400
+ var _this4 = this ;
282
401
283
402
attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
284
403
return this . query ( resourceConfig . table || underscore ( resourceConfig . name ) ) . where ( resourceConfig . idAttribute , toString ( id ) ) . update ( attrs ) . then ( function ( ) {
285
- return _this3 . find ( resourceConfig , id ) ;
404
+ return _this4 . find ( resourceConfig , id ) ;
286
405
} ) ;
287
406
}
288
407
} , {
289
408
key : 'updateAll' ,
290
409
value : function updateAll ( resourceConfig , attrs , params , options ) {
291
- var _this4 = this ;
410
+ var _this5 = this ;
292
411
293
412
attrs = removeCircular ( omit ( attrs , resourceConfig . relationFields || [ ] ) ) ;
294
413
return filterQuery . call ( this , resourceConfig , params , options ) . then ( function ( items ) {
295
414
return map ( items , function ( item ) {
296
415
return item [ resourceConfig . idAttribute ] ;
297
416
} ) ;
298
417
} ) . then ( function ( ids ) {
299
- return filterQuery . call ( _this4 , resourceConfig , params , options ) . update ( attrs ) . then ( function ( ) {
418
+ return filterQuery . call ( _this5 , resourceConfig , params , options ) . update ( attrs ) . then ( function ( ) {
300
419
var _params = { where : { } } ;
301
420
_params . where [ resourceConfig . idAttribute ] = {
302
421
'in' : ids
303
422
} ;
304
- return filterQuery . call ( _this4 , resourceConfig , _params , options ) ;
423
+ return filterQuery . call ( _this5 , resourceConfig , _params , options ) ;
305
424
} ) ;
306
425
} ) ;
307
426
}
@@ -331,28 +450,34 @@ module.exports =
331
450
/* 1 */
332
451
/***/ function ( module , exports ) {
333
452
334
- module . exports = require ( "knex " ) ;
453
+ module . exports = require ( "mout/string/underscore " ) ;
335
454
336
455
/***/ } ,
337
456
/* 2 */
338
457
/***/ function ( module , exports ) {
339
458
340
- module . exports = require ( "js-data " ) ;
459
+ module . exports = require ( "knex " ) ;
341
460
342
461
/***/ } ,
343
462
/* 3 */
344
463
/***/ function ( module , exports ) {
345
464
346
- module . exports = require ( "mout/array/map " ) ;
465
+ module . exports = require ( "js-data " ) ;
347
466
348
467
/***/ } ,
349
468
/* 4 */
350
469
/***/ function ( module , exports ) {
351
470
352
- module . exports = require ( "mout/string/underscore " ) ;
471
+ module . exports = require ( "mout/array/map " ) ;
353
472
354
473
/***/ } ,
355
474
/* 5 */
475
+ /***/ function ( module , exports ) {
476
+
477
+ module . exports = require ( "mout/array/unique" ) ;
478
+
479
+ /***/ } ,
480
+ /* 6 */
356
481
/***/ function ( module , exports ) {
357
482
358
483
module . exports = require ( "mout/lang/toString" ) ;
0 commit comments