@@ -305,6 +305,79 @@ addHiddenPropsToTarget(LocalStorageAdapter.prototype, {
305
305
*/
306
306
beforeUpdateMany : noop ,
307
307
308
+ _create ( mapper , props , opts ) {
309
+ const self = this
310
+ const id = get ( props , mapper . idAttribute ) || guid ( )
311
+ set ( props , mapper . idAttribute , id )
312
+ const key = self . getIdPath ( mapper , opts , id )
313
+
314
+ // Create the record
315
+ // TODO: Create related records when the "with" option is provided
316
+ self . storage . setItem ( key , toJson ( props ) )
317
+ self . ensureId ( id , mapper , opts )
318
+ return fromJson ( self . storage . getItem ( key ) )
319
+ } ,
320
+
321
+ _createWithRelations ( mapper , record , opts ) {
322
+ const self = this
323
+ const fields = { }
324
+ opts . with || ( opts . with = [ ] )
325
+ const idAttribute = mapper . idAttribute
326
+ forEachRelation ( mapper , opts , function ( def , __opts ) {
327
+ const localField = def . localField
328
+ const relationData = get ( record , localField )
329
+ const relatedMapper = def . getRelation ( )
330
+ if ( ! relationData ) {
331
+ return
332
+ }
333
+ if ( def . type === 'hasMany' ) {
334
+ fields [ localField ] = relationData
335
+ } else if ( def . type === 'hasOne' ) {
336
+ fields [ localField ] = relationData
337
+ } else if ( def . type === 'belongsTo' ) {
338
+ fields [ localField ] = self . _createWithRelations ( relatedMapper , relationData , __opts )
339
+ set ( record , def . foreignKey , get ( fields [ localField ] , relatedMapper . idAttribute ) )
340
+ }
341
+ } )
342
+ const _props = { }
343
+ forOwn ( record , function ( value , key ) {
344
+ if ( ! ( key in fields ) ) {
345
+ _props [ key ] = value
346
+ }
347
+ } )
348
+
349
+ const id = get ( _props , idAttribute ) || guid ( )
350
+ set ( _props , idAttribute , id )
351
+ const key = self . getIdPath ( mapper , opts , id )
352
+
353
+ // Create the record
354
+ // TODO: Create related records when the "with" option is provided
355
+ self . storage . setItem ( key , toJson ( _props ) )
356
+ self . ensureId ( id , mapper , opts )
357
+ record = fromJson ( self . storage . getItem ( key ) )
358
+
359
+ forEachRelation ( mapper , opts , function ( def , __opts ) {
360
+ const localField = def . localField
361
+ const relationData = fields [ localField ]
362
+ const relatedMapper = def . getRelation ( )
363
+ if ( ! relationData ) {
364
+ return
365
+ }
366
+ if ( def . type === 'hasMany' ) {
367
+ fields [ localField ] = relationData . map ( function ( relatedDataItem ) {
368
+ set ( relatedDataItem , def . foreignKey , id )
369
+ return self . _createWithRelations ( relatedMapper , relatedDataItem , __opts )
370
+ } )
371
+ } else if ( def . type === 'hasOne' ) {
372
+ set ( relationData , def . foreignKey , id )
373
+ fields [ localField ] = self . _createWithRelations ( relatedMapper , relationData , __opts )
374
+ }
375
+ } )
376
+
377
+ fillIn ( record , fields )
378
+ return record
379
+ } ,
380
+
308
381
/**
309
382
* Create a new record.
310
383
*
@@ -329,14 +402,7 @@ addHiddenPropsToTarget(LocalStorageAdapter.prototype, {
329
402
return resolve ( self [ op ] ( mapper , props , opts ) ) . then ( function ( _props ) {
330
403
// Allow for re-assignment from lifecycle hook
331
404
let record = isUndefined ( _props ) ? props : _props
332
- const id = get ( record , mapper . idAttribute ) || guid ( )
333
- set ( record , mapper . idAttribute , id )
334
- const key = self . getIdPath ( mapper , opts , id )
335
-
336
- // Create the record
337
- // TODO: Create related records when the "with" option is provided
338
- self . storage . setItem ( key , toJson ( record ) )
339
- self . ensureId ( id , mapper , opts )
405
+ record = self . _createWithRelations ( mapper , record , opts )
340
406
341
407
// afterCreate lifecycle hook
342
408
op = opts . op = 'afterCreate'
@@ -377,16 +443,8 @@ addHiddenPropsToTarget(LocalStorageAdapter.prototype, {
377
443
return resolve ( self [ op ] ( mapper , props , opts ) ) . then ( function ( _props ) {
378
444
// Allow for re-assignment from lifecycle hook
379
445
let records = isUndefined ( _props ) ? props : _props
380
- const idAttribute = mapper . idAttribute
381
-
382
- // Create the record
383
- // TODO: Create related records when the "with" option is provided
384
- records . forEach ( function ( record ) {
385
- const id = get ( record , idAttribute ) || guid ( )
386
- set ( record , idAttribute , id )
387
- const key = self . getIdPath ( mapper , opts , id )
388
- self . storage . setItem ( key , toJson ( record ) )
389
- self . ensureId ( id , mapper , opts )
446
+ records = records . map ( function ( record ) {
447
+ return self . _createWithRelations ( mapper , record , opts )
390
448
} )
391
449
392
450
// afterCreateMany lifecycle hook
0 commit comments