@@ -27,8 +27,7 @@ import {
27
27
firestoreClientGetDocumentsViaSnapshotListener ,
28
28
firestoreClientGetNamedQuery ,
29
29
firestoreClientListen ,
30
- firestoreClientLoadBundle ,
31
- firestoreClientTransaction
30
+ firestoreClientLoadBundle
32
31
} from '../core/firestore_client' ;
33
32
import {
34
33
Bound ,
@@ -54,14 +53,13 @@ import {
54
53
queryWithLimit ,
55
54
queryWithStartAt
56
55
} from '../core/query' ;
57
- import { Transaction as InternalTransaction } from '../core/transaction' ;
58
56
import { ViewSnapshot } from '../core/view_snapshot' ;
59
- import { Document , MaybeDocument , NoDocument } from '../model/document' ;
57
+ import { Document } from '../model/document' ;
60
58
import { DocumentKey } from '../model/document_key' ;
61
59
import { FieldPath , ResourcePath } from '../model/path' ;
62
60
import { isServerTimestamp } from '../model/server_timestamps' ;
63
61
import { refValue } from '../model/values' ;
64
- import { debugAssert , fail } from '../util/assert' ;
62
+ import { debugAssert } from '../util/assert' ;
65
63
import { Code , FirestoreError } from '../util/error' ;
66
64
import {
67
65
cast ,
@@ -85,9 +83,6 @@ import {
85
83
import {
86
84
fieldPathFromArgument ,
87
85
parseQueryValue ,
88
- parseSetData ,
89
- parseUpdateData ,
90
- parseUpdateVarargs ,
91
86
UntypedFirestoreDataConverter ,
92
87
UserDataReader
93
88
} from './user_data_reader' ;
@@ -125,6 +120,10 @@ import {
125
120
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector' ;
126
121
import { Compat } from '../compat/compat' ;
127
122
import { WriteBatch as ExpWriteBatch } from '../../exp/src/api/write_batch' ;
123
+ import {
124
+ runTransaction ,
125
+ Transaction as ExpTransaction
126
+ } from '../../exp/src/api/transaction' ;
128
127
129
128
import {
130
129
CollectionReference as PublicCollectionReference ,
@@ -392,12 +391,8 @@ export class Firestore
392
391
runTransaction < T > (
393
392
updateFunction : ( transaction : PublicTransaction ) => Promise < T >
394
393
) : Promise < T > {
395
- const client = ensureFirestoreConfigured ( this . _delegate ) ;
396
- return firestoreClientTransaction (
397
- client ,
398
- ( transaction : InternalTransaction ) => {
399
- return updateFunction ( new Transaction ( this , transaction ) ) ;
400
- }
394
+ return runTransaction ( this . _delegate , transaction =>
395
+ updateFunction ( new Transaction ( this , transaction ) )
401
396
) ;
402
397
}
403
398
@@ -478,68 +473,23 @@ export function namedQuery(
478
473
/**
479
474
* A reference to a transaction.
480
475
*/
481
- export class Transaction implements PublicTransaction {
482
- private _dataReader : UserDataReader ;
483
-
476
+ export class Transaction
477
+ extends Compat < ExpTransaction >
478
+ implements PublicTransaction {
484
479
constructor (
485
- private _firestore : Firestore ,
486
- private _transaction : InternalTransaction
480
+ private readonly _firestore : Firestore ,
481
+ delegate : ExpTransaction
487
482
) {
488
- this . _dataReader = newUserDataReader ( this . _firestore . _delegate ) ;
483
+ super ( delegate ) ;
489
484
}
490
485
491
486
get < T > (
492
487
documentRef : PublicDocumentReference < T >
493
488
) : Promise < PublicDocumentSnapshot < T > > {
494
- const ref = validateReference (
495
- 'Transaction.get' ,
496
- documentRef ,
497
- this . _firestore
498
- ) ;
499
- const userDataWriter = new UserDataWriter ( this . _firestore ) ;
500
- return this . _transaction
501
- . lookup ( [ ref . _key ] )
502
- . then ( ( docs : MaybeDocument [ ] ) => {
503
- if ( ! docs || docs . length !== 1 ) {
504
- return fail ( 'Mismatch in docs returned from document lookup.' ) ;
505
- }
506
- const doc = docs [ 0 ] ;
507
- if ( doc instanceof NoDocument ) {
508
- return new DocumentSnapshot < T > (
509
- this . _firestore ,
510
- new ExpDocumentSnapshot (
511
- this . _firestore . _delegate ,
512
- userDataWriter ,
513
- ref . _key ,
514
- null ,
515
- new SnapshotMetadata (
516
- /*hasPendingWrites= */ false ,
517
- /* fromCache= */ false
518
- ) ,
519
- ref . _converter
520
- )
521
- ) ;
522
- } else if ( doc instanceof Document ) {
523
- return new DocumentSnapshot < T > (
524
- this . _firestore ,
525
- new ExpDocumentSnapshot (
526
- this . _firestore . _delegate ,
527
- userDataWriter ,
528
- ref . _key ,
529
- doc ,
530
- new SnapshotMetadata (
531
- /*hasPendingWrites= */ false ,
532
- /* fromCache= */ false
533
- ) ,
534
- ref . _converter
535
- )
536
- ) ;
537
- } else {
538
- throw fail (
539
- `BatchGetDocumentsRequest returned unexpected document type: ${ doc . constructor . name } `
540
- ) ;
541
- }
542
- } ) ;
489
+ const ref = castReference ( documentRef ) ;
490
+ return this . _delegate
491
+ . get ( ref )
492
+ . then ( result => new DocumentSnapshot ( this . _firestore , result ) ) ;
543
493
}
544
494
545
495
set < T > (
@@ -550,35 +500,22 @@ export class Transaction implements PublicTransaction {
550
500
set < T > ( documentRef : DocumentReference < T > , data : T ) : Transaction ;
551
501
set < T > (
552
502
documentRef : PublicDocumentReference < T > ,
553
- value : T | Partial < T > ,
503
+ data : T | Partial < T > ,
554
504
options ?: PublicSetOptions
555
505
) : Transaction {
556
- const ref = validateReference (
557
- 'Transaction.set' ,
558
- documentRef ,
559
- this . _firestore
560
- ) ;
561
- options = validateSetOptions ( 'Transaction.set' , options ) ;
562
- const convertedValue = applyFirestoreDataConverter (
563
- ref . _converter ,
564
- value ,
565
- options
566
- ) ;
567
- const parsed = parseSetData (
568
- this . _dataReader ,
569
- 'Transaction.set' ,
570
- ref . _key ,
571
- convertedValue ,
572
- ref . _converter !== null ,
573
- options
574
- ) ;
575
- this . _transaction . set ( ref . _key , parsed ) ;
506
+ const ref = castReference ( documentRef ) ;
507
+ if ( options ) {
508
+ validateSetOptions ( 'Transaction.set' , options ) ;
509
+ this . _delegate . set ( ref , data , options ) ;
510
+ } else {
511
+ this . _delegate . set ( ref , data ) ;
512
+ }
576
513
return this ;
577
514
}
578
515
579
516
update (
580
517
documentRef : PublicDocumentReference < unknown > ,
581
- value : PublicUpdateData
518
+ data : PublicUpdateData
582
519
) : Transaction ;
583
520
update (
584
521
documentRef : PublicDocumentReference < unknown > ,
@@ -588,55 +525,28 @@ export class Transaction implements PublicTransaction {
588
525
) : Transaction ;
589
526
update (
590
527
documentRef : PublicDocumentReference < unknown > ,
591
- fieldOrUpdateData : string | PublicFieldPath | PublicUpdateData ,
528
+ dataOrField : unknown ,
592
529
value ?: unknown ,
593
530
...moreFieldsAndValues : unknown [ ]
594
531
) : Transaction {
595
- const ref = validateReference (
596
- 'Transaction.update' ,
597
- documentRef ,
598
- this . _firestore
599
- ) ;
600
-
601
- // For Compat types, we have to "extract" the underlying types before
602
- // performing validation.
603
- if ( fieldOrUpdateData instanceof Compat ) {
604
- fieldOrUpdateData = ( fieldOrUpdateData as Compat < ExpFieldPath > ) . _delegate ;
605
- }
606
-
607
- let parsed ;
608
- if (
609
- typeof fieldOrUpdateData === 'string' ||
610
- fieldOrUpdateData instanceof ExpFieldPath
611
- ) {
612
- parsed = parseUpdateVarargs (
613
- this . _dataReader ,
614
- 'Transaction.update' ,
615
- ref . _key ,
616
- fieldOrUpdateData ,
617
- value ,
618
- moreFieldsAndValues
619
- ) ;
532
+ const ref = castReference ( documentRef ) ;
533
+ if ( arguments . length === 2 ) {
534
+ this . _delegate . update ( ref , dataOrField as PublicUpdateData ) ;
620
535
} else {
621
- parsed = parseUpdateData (
622
- this . _dataReader ,
623
- 'Transaction.update' ,
624
- ref . _key ,
625
- fieldOrUpdateData
536
+ this . _delegate . update (
537
+ ref ,
538
+ dataOrField as string ,
539
+ value ,
540
+ ... moreFieldsAndValues
626
541
) ;
627
542
}
628
543
629
- this . _transaction . update ( ref . _key , parsed ) ;
630
544
return this ;
631
545
}
632
546
633
547
delete ( documentRef : PublicDocumentReference < unknown > ) : Transaction {
634
- const ref = validateReference (
635
- 'Transaction.delete' ,
636
- documentRef ,
637
- this . _firestore
638
- ) ;
639
- this . _transaction . delete ( ref . _key ) ;
548
+ const ref = castReference ( documentRef ) ;
549
+ this . _delegate . delete ( ref ) ;
640
550
return this ;
641
551
}
642
552
}
@@ -1956,25 +1866,6 @@ function castReference<T>(
1956
1866
return cast < ExpDocumentReference < T > > ( documentRef , ExpDocumentReference ) ;
1957
1867
}
1958
1868
1959
- function validateReference < T > (
1960
- methodName : string ,
1961
- documentRef : PublicDocumentReference < T > ,
1962
- firestore : Firestore
1963
- ) : ExpDocumentReference < T > {
1964
- const reference = cast < ExpDocumentReference < T > > (
1965
- documentRef ,
1966
- ExpDocumentReference
1967
- ) ;
1968
- if ( reference . firestore !== firestore . _delegate ) {
1969
- throw new FirestoreError (
1970
- Code . INVALID_ARGUMENT ,
1971
- 'Provided document reference is from a different Firestore instance.'
1972
- ) ;
1973
- } else {
1974
- return reference ;
1975
- }
1976
- }
1977
-
1978
1869
/**
1979
1870
* Converts custom model object of type T into DocumentData by applying the
1980
1871
* converter if it exists.
0 commit comments