@@ -7,6 +7,7 @@ import { bind } from '@ember/runloop';
7
7
import { on } from '@ember/object/evented' ;
8
8
import { classify , camelize } from '@ember/string' ;
9
9
import { pluralize } from 'ember-inflector' ;
10
+ import { v4 } from 'uuid' ;
10
11
//import BelongsToRelationship from 'ember-data/-private/system/relationships/state/belongs-to';
11
12
12
13
import {
@@ -15,6 +16,22 @@ import {
15
16
configFlagDisabled ,
16
17
} from '../utils' ;
17
18
19
+ function getRevFromSaveResult ( records ) {
20
+ let rev = null ;
21
+ try {
22
+ rev = records [ Object . keys ( records ) [ 0 ] ] [ 0 ] . rev ;
23
+ if ( ! rev || Object . keys ( records ) . length > 1 ) {
24
+ // eslint-disable-next-line no-console
25
+ console . warn (
26
+ `getRevFromSaveResult going to return ${ rev } , but that may not be correct`
27
+ ) ;
28
+ }
29
+ } catch ( e ) {
30
+ throw Error ( `Could not determine rev` ) ;
31
+ }
32
+ return rev ;
33
+ }
34
+
18
35
//BelongsToRelationship.reopen({
19
36
// findRecord() {
20
37
// return this._super().catch(() => {
@@ -516,29 +533,60 @@ export default class PouchAdapter extends RESTAdapter.extend({
516
533
} ) ;
517
534
} ,
518
535
536
+ generateIdForRecord : function ( /* store, type, inputProperties */ ) {
537
+ return v4 ( ) ;
538
+ } ,
539
+
519
540
createdRecords : null ,
520
- createRecord : async function ( store , type , record ) {
521
- await this . _init ( store , type ) ;
522
- var data = this . _recordToData ( store , type , record ) ;
541
+ createRecord : async function ( store , type , snapshot ) {
542
+ const record = snapshot . record ;
543
+ if ( record . _emberPouchSavePromise ) {
544
+ console . log ( 'found save promise' ) ;
545
+ const changes = record . changedAttributes ( ) ;
546
+ record . _emberPouchSavePromise = record . _emberPouchSavePromise . then (
547
+ ( records ) => {
548
+ // If there have been changes since the document was created then we should update the record now
549
+ if ( Object . keys ( changes ) . length > 0 ) {
550
+ // Include latest rev to indicate that we're aware that data has changed since original request
551
+ // (otherwise a document update conflict error would be thrown by the DB)
552
+ snapshot . _attributes . rev = getRevFromSaveResult ( records ) ;
553
+ return this . updateRecord ( store , type , snapshot ) ;
554
+ }
555
+ return records ;
556
+ }
557
+ ) ;
558
+ return record . _emberPouchSavePromise ;
559
+ }
560
+
561
+ this . _init ( store , type ) ;
562
+ var data = this . _recordToData ( store , type , snapshot ) ;
523
563
let rel = this . db . rel ;
524
564
525
565
let id = data . id ;
526
- if ( ! id ) {
527
- id = data . id = rel . uuid ( ) ;
528
- }
529
566
this . createdRecords [ id ] = true ;
530
567
531
568
let typeName = this . getRecordTypeName ( type ) ;
532
- try {
533
- let saved = await rel . save ( typeName , data ) ;
534
- Object . assign ( data , saved ) ;
535
- let result = { } ;
536
- result [ pluralize ( typeName ) ] = [ data ] ;
537
- return result ;
538
- } catch ( e ) {
539
- delete this . createdRecords [ id ] ;
540
- throw e ;
541
- }
569
+
570
+ Object . defineProperty ( record , '_emberPouchSavePromise' , {
571
+ enumerable : false ,
572
+ writable : true ,
573
+ value : rel
574
+ . save ( typeName , data )
575
+ . then ( ( saved ) => {
576
+ Object . assign ( data , saved ) ;
577
+ let result = { } ;
578
+ result [ pluralize ( typeName ) ] = [ data ] ;
579
+ console . log ( 'saved, result' , saved , result ) ;
580
+ return result ;
581
+ } )
582
+ . catch ( ( e ) => {
583
+ console . log ( 'catch' , e ) ;
584
+ delete this . createdRecords [ id ] ;
585
+ throw e ;
586
+ } ) ,
587
+ } ) ;
588
+
589
+ return record . _emberPouchSavePromise ;
542
590
} ,
543
591
544
592
updateRecord : async function ( store , type , record ) {
0 commit comments