Skip to content

Commit a1897c9

Browse files
Merge 7003f22 into ad44c19
2 parents ad44c19 + 7003f22 commit a1897c9

File tree

7 files changed

+74
-161
lines changed

7 files changed

+74
-161
lines changed

.changeset/blue-geese-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Internal changes for the upcoming modular API.

packages/firestore/exp/src/api/write_batch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { FirebaseFirestore } from './database';
2020
import { executeWrite } from './reference';
2121
import { ensureFirestoreConfigured } from '../../../src/api/database';
2222

23+
export { WriteBatch };
24+
2325
/**
2426
* Creates a write batch, used for performing multiple writes as a single
2527
* atomic operation. The maximum number of writes allowed in a single WriteBatch

packages/firestore/exp/test/shim.ts

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -128,63 +128,6 @@ export class Transaction
128128
}
129129
}
130130

131-
export class WriteBatch
132-
extends Compat<exp.WriteBatch>
133-
implements legacy.WriteBatch {
134-
set<T>(
135-
documentRef: DocumentReference<T>,
136-
data: T,
137-
options?: legacy.SetOptions
138-
): WriteBatch {
139-
if (options) {
140-
validateSetOptions('WriteBatch.set', options);
141-
this._delegate.set(documentRef._delegate, unwrap(data), options);
142-
} else {
143-
this._delegate.set(documentRef._delegate, unwrap(data));
144-
}
145-
return this;
146-
}
147-
148-
update(
149-
documentRef: DocumentReference<any>,
150-
data: legacy.UpdateData
151-
): WriteBatch;
152-
update(
153-
documentRef: DocumentReference<any>,
154-
field: string | FieldPath,
155-
value: any,
156-
...moreFieldsAndValues: any[]
157-
): WriteBatch;
158-
update(
159-
documentRef: DocumentReference<any>,
160-
dataOrField: any,
161-
value?: any,
162-
...moreFieldsAndValues: any[]
163-
): WriteBatch {
164-
if (arguments.length === 2) {
165-
this._delegate.update(documentRef._delegate, unwrap(dataOrField));
166-
} else {
167-
this._delegate.update(
168-
documentRef._delegate,
169-
unwrap(dataOrField),
170-
unwrap(value),
171-
...unwrap(moreFieldsAndValues)
172-
);
173-
}
174-
175-
return this;
176-
}
177-
178-
delete(documentRef: DocumentReference<any>): WriteBatch {
179-
this._delegate.delete(documentRef._delegate);
180-
return this;
181-
}
182-
183-
commit(): Promise<void> {
184-
return this._delegate.commit();
185-
}
186-
}
187-
188131
export class Query<T = legacy.DocumentData>
189132
extends Compat<exp.Query<T>>
190133
implements legacy.Query<T> {

packages/firestore/lite/src/api/reference.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import { getDatastore } from './components';
8181
import { ByteString } from '../../../src/util/byte_string';
8282
import { Bytes } from './bytes';
8383
import { AbstractUserDataWriter } from '../../../src/api/user_data_writer';
84+
import { Compat } from '../../../src/compat/compat';
8485

8586
/**
8687
* Document data (for use with {@link setDoc()}) consists of fields mapped to
@@ -1109,6 +1110,12 @@ export function updateDoc(
11091110
): Promise<void> {
11101111
const dataReader = newUserDataReader(reference.firestore);
11111112

1113+
// For Compat types, we have to "extract" the underlying types before
1114+
// performing validation.
1115+
if (fieldOrUpdateData instanceof Compat) {
1116+
fieldOrUpdateData = fieldOrUpdateData._delegate;
1117+
}
1118+
11121119
let parsed: ParsedUpdateData;
11131120
if (
11141121
typeof fieldOrUpdateData === 'string' ||

packages/firestore/lite/src/api/transaction.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
} from './reference';
4545
import { FieldPath } from './field_path';
4646
import { getDatastore } from './components';
47+
import { Compat } from '../../../src/compat/compat';
4748

4849
// TODO(mrschmidt) Consider using `BaseTransaction` as the base class in the
4950
// legacy SDK.
@@ -198,6 +199,12 @@ export class Transaction {
198199
): this {
199200
const ref = validateReference(documentRef, this._firestore);
200201

202+
// For Compat types, we have to "extract" the underlying types before
203+
// performing validation.
204+
if (fieldOrUpdateData instanceof Compat) {
205+
fieldOrUpdateData = fieldOrUpdateData._delegate;
206+
}
207+
201208
let parsed;
202209
if (
203210
typeof fieldOrUpdateData === 'string' ||

packages/firestore/lite/src/api/write_batch.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { FirebaseFirestore } from './database';
3838
import { invokeCommitRpc } from '../../../src/remote/datastore';
3939
import { FieldPath } from './field_path';
4040
import { getDatastore } from './components';
41+
import { Compat } from '../../../src/compat/compat';
4142

4243
/**
4344
* A write batch, used to perform multiple writes as a single atomic unit.
@@ -155,8 +156,13 @@ export class WriteBatch {
155156
this.verifyNotCommitted();
156157
const ref = validateReference(documentRef, this._firestore);
157158

158-
let parsed;
159+
// For Compat types, we have to "extract" the underlying types before
160+
// performing validation.
161+
if (fieldOrUpdateData instanceof Compat) {
162+
fieldOrUpdateData = fieldOrUpdateData._delegate;
163+
}
159164

165+
let parsed;
160166
if (
161167
typeof fieldOrUpdateData === 'string' ||
162168
fieldOrUpdateData instanceof FieldPath
@@ -233,9 +239,12 @@ export class WriteBatch {
233239
}
234240

235241
export function validateReference<T>(
236-
documentRef: DocumentReference<T>,
242+
documentRef: DocumentReference<T> | Compat<DocumentReference<T>>,
237243
firestore: FirebaseFirestore
238244
): DocumentReference<T> {
245+
if (documentRef instanceof Compat) {
246+
documentRef = documentRef._delegate;
247+
}
239248
if (documentRef.firestore !== firestore) {
240249
throw new FirestoreError(
241250
Code.INVALID_ARGUMENT,

packages/firestore/src/api/database.ts

Lines changed: 42 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ import {
2828
firestoreClientGetNamedQuery,
2929
firestoreClientListen,
3030
firestoreClientLoadBundle,
31-
firestoreClientTransaction,
32-
firestoreClientWrite
31+
firestoreClientTransaction
3332
} from '../core/firestore_client';
3433
import {
3534
Bound,
@@ -59,7 +58,6 @@ import { Transaction as InternalTransaction } from '../core/transaction';
5958
import { ViewSnapshot } from '../core/view_snapshot';
6059
import { Document, MaybeDocument, NoDocument } from '../model/document';
6160
import { DocumentKey } from '../model/document_key';
62-
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
6361
import { FieldPath, ResourcePath } from '../model/path';
6462
import { isServerTimestamp } from '../model/server_timestamps';
6563
import { refValue } from '../model/values';
@@ -121,10 +119,12 @@ import {
121119
getDoc,
122120
onSnapshot,
123121
DocumentReference as ExpDocumentReference,
124-
Query as ExpQuery
122+
Query as ExpQuery,
123+
executeWrite
125124
} from '../../exp/src/api/reference';
126125
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector';
127126
import { Compat } from '../compat/compat';
127+
import { WriteBatch as ExpWriteBatch } from '../../exp/src/api/write_batch';
128128

129129
import {
130130
CollectionReference as PublicCollectionReference,
@@ -403,7 +403,11 @@ export class Firestore
403403

404404
batch(): PublicWriteBatch {
405405
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+
);
407411
}
408412
}
409413

@@ -637,15 +641,9 @@ export class Transaction implements PublicTransaction {
637641
}
638642
}
639643

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 {
649647
set<T>(
650648
documentRef: DocumentReference<T>,
651649
data: Partial<T>,
@@ -654,38 +652,22 @@ export class WriteBatch implements PublicWriteBatch {
654652
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
655653
set<T>(
656654
documentRef: PublicDocumentReference<T>,
657-
value: T | Partial<T>,
655+
data: T | Partial<T>,
658656
options?: PublicSetOptions
659657
): 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+
}
683665
return this;
684666
}
685667

686668
update(
687669
documentRef: PublicDocumentReference<unknown>,
688-
value: PublicUpdateData
670+
data: PublicUpdateData
689671
): WriteBatch;
690672
update(
691673
documentRef: PublicDocumentReference<unknown>,
@@ -695,83 +677,32 @@ export class WriteBatch implements PublicWriteBatch {
695677
): WriteBatch;
696678
update(
697679
documentRef: PublicDocumentReference<unknown>,
698-
fieldOrUpdateData: string | PublicFieldPath | PublicUpdateData,
680+
dataOrField: string | PublicFieldPath | PublicUpdateData,
699681
value?: unknown,
700682
...moreFieldsAndValues: unknown[]
701683
): 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);
728687
} 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
734693
);
735694
}
736-
737-
this._mutations = this._mutations.concat(
738-
parsed.toMutations(ref._key, Precondition.exists(true))
739-
);
740695
return this;
741696
}
742697

743698
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);
753701
return this;
754702
}
755703

756704
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();
775706
}
776707
}
777708

@@ -2016,6 +1947,15 @@ export class CollectionReference<T = PublicDocumentData>
20161947
}
20171948
}
20181949

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+
20191959
function validateReference<T>(
20201960
methodName: string,
20211961
documentRef: PublicDocumentReference<T>,

0 commit comments

Comments
 (0)