Skip to content

Commit fc81037

Browse files
Fix merge
1 parent 494f8c4 commit fc81037

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

packages/firestore/src/api/database.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,15 @@ export class Transaction implements firestore.Transaction {
726726
});
727727
}
728728

729+
set<T>(
730+
documentRef: DocumentReference<T>,
731+
data: Partial<T>,
732+
options: firestore.SetOptions
733+
): Transaction;
734+
set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
729735
set<T>(
730736
documentRef: firestore.DocumentReference<T>,
731-
value: T,
737+
value: T | Partial<T>,
732738
options?: firestore.SetOptions
733739
): Transaction {
734740
validateBetweenNumberOfArgs('Transaction.set', arguments, 2, 3);
@@ -738,15 +744,16 @@ export class Transaction implements firestore.Transaction {
738744
this._firestore
739745
);
740746
options = validateSetOptions('Transaction.set', options);
741-
const [convertedValue, functionName] = applyFirestoreDataConverter(
747+
const convertedValue = applyFirestoreDataConverter(
742748
ref._converter,
743749
value,
744-
'Transaction.set'
750+
options
745751
);
746752
const parsed = this._firestore._dataReader.parseSetData(
747-
functionName,
753+
'Transaction.set',
748754
ref._key,
749755
convertedValue,
756+
ref._converter !== null,
750757
options
751758
);
752759
this._transaction.set(ref._key, parsed);
@@ -825,9 +832,15 @@ export class WriteBatch implements firestore.WriteBatch {
825832

826833
constructor(private _firestore: Firestore) {}
827834

835+
set<T>(
836+
documentRef: DocumentReference<T>,
837+
data: Partial<T>,
838+
options: firestore.SetOptions
839+
): WriteBatch;
840+
set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
828841
set<T>(
829842
documentRef: firestore.DocumentReference<T>,
830-
value: T,
843+
value: T | Partial<T>,
831844
options?: firestore.SetOptions
832845
): WriteBatch {
833846
validateBetweenNumberOfArgs('WriteBatch.set', arguments, 2, 3);
@@ -838,15 +851,16 @@ export class WriteBatch implements firestore.WriteBatch {
838851
this._firestore
839852
);
840853
options = validateSetOptions('WriteBatch.set', options);
841-
const [convertedValue, functionName] = applyFirestoreDataConverter(
854+
const convertedValue = applyFirestoreDataConverter(
842855
ref._converter,
843856
value,
844-
'WriteBatch.set'
857+
options
845858
);
846859
const parsed = this._firestore._dataReader.parseSetData(
847-
functionName,
860+
'WriteBatch.set',
848861
ref._key,
849862
convertedValue,
863+
ref._converter !== null,
850864
options
851865
);
852866
this._mutations = this._mutations.concat(
@@ -1032,22 +1046,21 @@ export class DocumentReference<T = firestore.DocumentData>
10321046
);
10331047
}
10341048

1035-
set(
1036-
value: firestore.DocumentData,
1037-
options?: firestore.SetOptions
1038-
): Promise<void>;
1039-
set(value: T, options?: firestore.SetOptions): Promise<void> {
1049+
set(value: Partial<T>, options: firestore.SetOptions): Promise<void>;
1050+
set(value: T): Promise<void>;
1051+
set(value: T | Partial<T>, options?: firestore.SetOptions): Promise<void> {
10401052
validateBetweenNumberOfArgs('DocumentReference.set', arguments, 1, 2);
10411053
options = validateSetOptions('DocumentReference.set', options);
1042-
const [convertedValue, functionName] = applyFirestoreDataConverter(
1054+
const convertedValue = applyFirestoreDataConverter(
10431055
this._converter,
10441056
value,
1045-
'DocumentReference.set'
1057+
options
10461058
);
10471059
const parsed = this.firestore._dataReader.parseSetData(
1048-
functionName,
1060+
'DocumentReference.set',
10491061
this._key,
10501062
convertedValue,
1063+
this._converter !== null,
10511064
options
10521065
);
10531066
return this._firestoreClient.write(
@@ -2622,16 +2635,22 @@ function resultChangeType(type: ChangeType): firestore.DocumentChangeType {
26222635
export function applyFirestoreDataConverter<T>(
26232636
converter: UntypedFirestoreDataConverter<T> | null,
26242637
value: T,
2625-
functionName: string
2626-
): [firestore.DocumentData, string] {
2638+
options?: firestore.SetOptions
2639+
): firestore.DocumentData {
26272640
let convertedValue;
26282641
if (converter) {
2629-
convertedValue = converter.toFirestore(value);
2630-
functionName = 'toFirestore() in ' + functionName;
2642+
if (options && (options.merge || options.mergeFields)) {
2643+
// Cast to `any` in order to satisfy the union type constraint on
2644+
// toFirestore().
2645+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2646+
convertedValue = (converter as any).toFirestore(value, options);
2647+
} else {
2648+
convertedValue = converter.toFirestore(value);
2649+
}
26312650
} else {
26322651
convertedValue = value as firestore.DocumentData;
26332652
}
2634-
return [convertedValue, functionName];
2653+
return convertedValue;
26352654
}
26362655

26372656
function contains(obj: object, key: string): obj is { key: unknown } {

0 commit comments

Comments
 (0)