@@ -28,8 +28,7 @@ import {
28
28
firestoreClientGetNamedQuery ,
29
29
firestoreClientListen ,
30
30
firestoreClientLoadBundle ,
31
- firestoreClientTransaction ,
32
- firestoreClientWrite
31
+ firestoreClientTransaction
33
32
} from '../core/firestore_client' ;
34
33
import {
35
34
Bound ,
@@ -59,7 +58,6 @@ import { Transaction as InternalTransaction } from '../core/transaction';
59
58
import { ViewSnapshot } from '../core/view_snapshot' ;
60
59
import { Document , MaybeDocument , NoDocument } from '../model/document' ;
61
60
import { DocumentKey } from '../model/document_key' ;
62
- import { DeleteMutation , Mutation , Precondition } from '../model/mutation' ;
63
61
import { FieldPath , ResourcePath } from '../model/path' ;
64
62
import { isServerTimestamp } from '../model/server_timestamps' ;
65
63
import { refValue } from '../model/values' ;
@@ -121,10 +119,12 @@ import {
121
119
getDoc ,
122
120
onSnapshot ,
123
121
DocumentReference as ExpDocumentReference ,
124
- Query as ExpQuery
122
+ Query as ExpQuery ,
123
+ executeWrite
125
124
} from '../../exp/src/api/reference' ;
126
125
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector' ;
127
126
import { Compat } from '../compat/compat' ;
127
+ import { WriteBatch as ExpWriteBatch } from '../../exp/src/api/write_batch' ;
128
128
129
129
import {
130
130
CollectionReference as PublicCollectionReference ,
@@ -403,7 +403,11 @@ export class Firestore
403
403
404
404
batch ( ) : PublicWriteBatch {
405
405
ensureFirestoreConfigured ( this . _delegate ) ;
406
- return new WriteBatch ( this ) ;
406
+ return new WriteBatch (
407
+ new ExpWriteBatch ( this . _delegate , mutations =>
408
+ executeWrite ( this . _delegate , mutations )
409
+ )
410
+ ) ;
407
411
}
408
412
}
409
413
@@ -637,15 +641,9 @@ export class Transaction implements PublicTransaction {
637
641
}
638
642
}
639
643
640
- export class WriteBatch implements PublicWriteBatch {
641
- private _mutations = [ ] as Mutation [ ] ;
642
- private _committed = false ;
643
- private _dataReader : UserDataReader ;
644
-
645
- constructor ( private _firestore : Firestore ) {
646
- this . _dataReader = newUserDataReader ( this . _firestore . _delegate ) ;
647
- }
648
-
644
+ export class WriteBatch
645
+ extends Compat < ExpWriteBatch >
646
+ implements PublicWriteBatch {
649
647
set < T > (
650
648
documentRef : DocumentReference < T > ,
651
649
data : Partial < T > ,
@@ -654,38 +652,22 @@ export class WriteBatch implements PublicWriteBatch {
654
652
set < T > ( documentRef : DocumentReference < T > , data : T ) : WriteBatch ;
655
653
set < T > (
656
654
documentRef : PublicDocumentReference < T > ,
657
- value : T | Partial < T > ,
655
+ data : T | Partial < T > ,
658
656
options ?: PublicSetOptions
659
657
) : WriteBatch {
660
- this . verifyNotCommitted ( ) ;
661
- const ref = validateReference (
662
- 'WriteBatch.set' ,
663
- documentRef ,
664
- this . _firestore
665
- ) ;
666
- options = validateSetOptions ( 'WriteBatch.set' , options ) ;
667
- const convertedValue = applyFirestoreDataConverter (
668
- ref . _converter ,
669
- value ,
670
- options
671
- ) ;
672
- const parsed = parseSetData (
673
- this . _dataReader ,
674
- 'WriteBatch.set' ,
675
- ref . _key ,
676
- convertedValue ,
677
- ref . _converter !== null ,
678
- options
679
- ) ;
680
- this . _mutations = this . _mutations . concat (
681
- parsed . toMutations ( ref . _key , Precondition . none ( ) )
682
- ) ;
658
+ const ref = castReference ( documentRef ) ;
659
+ if ( options ) {
660
+ validateSetOptions ( 'WriteBatch.set' , options ) ;
661
+ this . _delegate . set ( ref , data , options ) ;
662
+ } else {
663
+ this . _delegate . set ( ref , data ) ;
664
+ }
683
665
return this ;
684
666
}
685
667
686
668
update (
687
669
documentRef : PublicDocumentReference < unknown > ,
688
- value : PublicUpdateData
670
+ data : PublicUpdateData
689
671
) : WriteBatch ;
690
672
update (
691
673
documentRef : PublicDocumentReference < unknown > ,
@@ -695,83 +677,32 @@ export class WriteBatch implements PublicWriteBatch {
695
677
) : WriteBatch ;
696
678
update (
697
679
documentRef : PublicDocumentReference < unknown > ,
698
- fieldOrUpdateData : string | PublicFieldPath | PublicUpdateData ,
680
+ dataOrField : string | PublicFieldPath | PublicUpdateData ,
699
681
value ?: unknown ,
700
682
...moreFieldsAndValues : unknown [ ]
701
683
) : WriteBatch {
702
- this . verifyNotCommitted ( ) ;
703
- const ref = validateReference (
704
- 'WriteBatch.update' ,
705
- documentRef ,
706
- this . _firestore
707
- ) ;
708
-
709
- // For Compat types, we have to "extract" the underlying types before
710
- // performing validation.
711
- if ( fieldOrUpdateData instanceof Compat ) {
712
- fieldOrUpdateData = ( fieldOrUpdateData as Compat < ExpFieldPath > ) . _delegate ;
713
- }
714
-
715
- let parsed ;
716
- if (
717
- typeof fieldOrUpdateData === 'string' ||
718
- fieldOrUpdateData instanceof ExpFieldPath
719
- ) {
720
- parsed = parseUpdateVarargs (
721
- this . _dataReader ,
722
- 'WriteBatch.update' ,
723
- ref . _key ,
724
- fieldOrUpdateData ,
725
- value ,
726
- moreFieldsAndValues
727
- ) ;
684
+ const ref = castReference ( documentRef ) ;
685
+ if ( arguments . length === 2 ) {
686
+ this . _delegate . update ( ref , dataOrField as PublicUpdateData ) ;
728
687
} else {
729
- parsed = parseUpdateData (
730
- this . _dataReader ,
731
- 'WriteBatch.update' ,
732
- ref . _key ,
733
- fieldOrUpdateData
688
+ this . _delegate . update (
689
+ ref ,
690
+ dataOrField as string | ExpFieldPath ,
691
+ value ,
692
+ ... moreFieldsAndValues
734
693
) ;
735
694
}
736
-
737
- this . _mutations = this . _mutations . concat (
738
- parsed . toMutations ( ref . _key , Precondition . exists ( true ) )
739
- ) ;
740
695
return this ;
741
696
}
742
697
743
698
delete ( documentRef : PublicDocumentReference < unknown > ) : WriteBatch {
744
- this . verifyNotCommitted ( ) ;
745
- const ref = validateReference (
746
- 'WriteBatch.delete' ,
747
- documentRef ,
748
- this . _firestore
749
- ) ;
750
- this . _mutations = this . _mutations . concat (
751
- new DeleteMutation ( ref . _key , Precondition . none ( ) )
752
- ) ;
699
+ const ref = castReference ( documentRef ) ;
700
+ this . _delegate . delete ( ref ) ;
753
701
return this ;
754
702
}
755
703
756
704
commit ( ) : Promise < void > {
757
- this . verifyNotCommitted ( ) ;
758
- this . _committed = true ;
759
- if ( this . _mutations . length > 0 ) {
760
- const client = ensureFirestoreConfigured ( this . _firestore . _delegate ) ;
761
- return firestoreClientWrite ( client , this . _mutations ) ;
762
- }
763
-
764
- return Promise . resolve ( ) ;
765
- }
766
-
767
- private verifyNotCommitted ( ) : void {
768
- if ( this . _committed ) {
769
- throw new FirestoreError (
770
- Code . FAILED_PRECONDITION ,
771
- 'A write batch can no longer be used after commit() ' +
772
- 'has been called.'
773
- ) ;
774
- }
705
+ return this . _delegate . commit ( ) ;
775
706
}
776
707
}
777
708
@@ -2016,6 +1947,15 @@ export class CollectionReference<T = PublicDocumentData>
2016
1947
}
2017
1948
}
2018
1949
1950
+ function castReference < T > (
1951
+ documentRef : PublicDocumentReference < T >
1952
+ ) : ExpDocumentReference < T > {
1953
+ if ( documentRef instanceof Compat ) {
1954
+ documentRef = documentRef . _delegate ;
1955
+ }
1956
+ return cast < ExpDocumentReference < T > > ( documentRef , ExpDocumentReference ) ;
1957
+ }
1958
+
2019
1959
function validateReference < T > (
2020
1960
methodName : string ,
2021
1961
documentRef : PublicDocumentReference < T > ,
0 commit comments