Skip to content

Commit 3018fd4

Browse files
Fix merge
1 parent 5458d98 commit 3018fd4

File tree

3 files changed

+48
-156
lines changed

3 files changed

+48
-156
lines changed

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/src/api/database.ts

Lines changed: 41 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ import {
121121
getDoc,
122122
onSnapshot,
123123
DocumentReference as ExpDocumentReference,
124-
Query as ExpQuery
124+
Query as ExpQuery,
125+
executeWrite
125126
} from '../../exp/src/api/reference';
126127
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector';
127128
import { Compat } from '../compat/compat';
129+
import { WriteBatch as ExpWriteBatch } from '../../exp/src/api/write_batch';
128130

129131
import {
130132
CollectionReference as PublicCollectionReference,
@@ -403,7 +405,11 @@ export class Firestore
403405

404406
batch(): PublicWriteBatch {
405407
ensureFirestoreConfigured(this._delegate);
406-
return new WriteBatch(this);
408+
return new WriteBatch(
409+
new ExpWriteBatch(this._delegate, mutations =>
410+
executeWrite(this._delegate, mutations)
411+
)
412+
);
407413
}
408414
}
409415

@@ -637,15 +643,9 @@ export class Transaction implements PublicTransaction {
637643
}
638644
}
639645

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-
646+
export class WriteBatch
647+
extends Compat<ExpWriteBatch>
648+
implements PublicWriteBatch {
649649
set<T>(
650650
documentRef: DocumentReference<T>,
651651
data: Partial<T>,
@@ -654,38 +654,22 @@ export class WriteBatch implements PublicWriteBatch {
654654
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
655655
set<T>(
656656
documentRef: PublicDocumentReference<T>,
657-
value: T | Partial<T>,
657+
data: T | Partial<T>,
658658
options?: PublicSetOptions
659659
): 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-
);
660+
const ref = castReference(documentRef);
661+
if (options) {
662+
validateSetOptions('WriteBatch.set', options);
663+
this._delegate.set(ref, data, options);
664+
} else {
665+
this._delegate.set(ref, data);
666+
}
683667
return this;
684668
}
685669

686670
update(
687671
documentRef: PublicDocumentReference<unknown>,
688-
value: PublicUpdateData
672+
data: PublicUpdateData
689673
): WriteBatch;
690674
update(
691675
documentRef: PublicDocumentReference<unknown>,
@@ -695,83 +679,32 @@ export class WriteBatch implements PublicWriteBatch {
695679
): WriteBatch;
696680
update(
697681
documentRef: PublicDocumentReference<unknown>,
698-
fieldOrUpdateData: string | PublicFieldPath | PublicUpdateData,
682+
dataOrField: string | PublicFieldPath | PublicUpdateData,
699683
value?: unknown,
700684
...moreFieldsAndValues: unknown[]
701685
): 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-
);
686+
const ref = castReference(documentRef);
687+
if (arguments.length === 2) {
688+
this._delegate.update(ref, dataOrField as PublicUpdateData);
728689
} else {
729-
parsed = parseUpdateData(
730-
this._dataReader,
731-
'WriteBatch.update',
732-
ref._key,
733-
fieldOrUpdateData
690+
this._delegate.update(
691+
ref,
692+
dataOrField as string | ExpFieldPath,
693+
value,
694+
...moreFieldsAndValues
734695
);
735696
}
736-
737-
this._mutations = this._mutations.concat(
738-
parsed.toMutations(ref._key, Precondition.exists(true))
739-
);
740697
return this;
741698
}
742699

743700
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-
);
701+
const ref = castReference(documentRef);
702+
this._delegate.delete(ref);
753703
return this;
754704
}
755705

756706
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-
}
707+
return this._delegate.commit();
775708
}
776709
}
777710

@@ -2016,6 +1949,15 @@ export class CollectionReference<T = PublicDocumentData>
20161949
}
20171950
}
20181951

1952+
function castReference<T>(
1953+
documentRef: PublicDocumentReference<T>
1954+
): ExpDocumentReference<T> {
1955+
if (documentRef instanceof Compat) {
1956+
documentRef = documentRef._delegate;
1957+
}
1958+
return cast<ExpDocumentReference<T>>(documentRef, ExpDocumentReference);
1959+
}
1960+
20191961
function validateReference<T>(
20201962
methodName: string,
20211963
documentRef: PublicDocumentReference<T>,

0 commit comments

Comments
 (0)