Skip to content

Commit 372a3ee

Browse files
author
Brian Chen
committed
Call toFirestore() once in add()
1 parent 0119d66 commit 372a3ee

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import { Transaction as InternalTransaction } from '../../../src/core/transactio
2929
import { validateReference } from '../../../lite/src/api/write_batch';
3030
import { getDatastore } from '../../../lite/src/api/components';
3131

32-
export class Transaction extends LiteTransaction
32+
export class Transaction
33+
extends LiteTransaction
3334
implements firestore.Transaction {
3435
// This class implements the same logic as the Transaction API in the Lite SDK
3536
// but is subclassed in order to return its own DocumentSnapshot types.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import { ParseContext } from '../../../src/api/user_data_reader';
3030
import { FieldTransform } from '../../../src/model/mutation';
3131

3232
/** The public FieldValue class of the lite API. */
33-
export abstract class FieldValue extends SerializableFieldValue
33+
export abstract class FieldValue
34+
extends SerializableFieldValue
3435
implements firestore.FieldValue {}
3536

3637
/**

packages/firestore/src/api/database.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2379,8 +2379,24 @@ export class CollectionReference<T = DocumentData>
23792379
? this._converter.toFirestore(value)
23802380
: value;
23812381
validateArgType('CollectionReference.add', 'object', 1, convertedValue);
2382+
2383+
// Call set() with the converted value directly to avoid calling toFirestore() a second time.
23822384
const docRef = this.doc();
2383-
return docRef.set(value).then(() => docRef);
2385+
const defaultConverter: FirestoreDataConverter<DocumentData> = {
2386+
toFirestore(modelObject: DocumentData): DocumentData {
2387+
return modelObject;
2388+
},
2389+
fromFirestore(
2390+
snapshot: QueryDocumentSnapshot,
2391+
options: SnapshotOptions
2392+
): DocumentData {
2393+
return snapshot.data(options)!;
2394+
}
2395+
};
2396+
return docRef
2397+
.withConverter(defaultConverter)
2398+
.set(convertedValue)
2399+
.then(() => docRef);
23842400
}
23852401

23862402
withConverter<U>(

packages/firestore/test/integration/api/validation.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ apiDescribe('Validation:', (persistence: boolean) => {
589589
});
590590
});
591591

592+
validationIt(
593+
persistence,
594+
'validates CollectionReference.add with correct function name',
595+
db => {
596+
const coll = db.collection('foo');
597+
598+
expect(() => coll.add(new TestClass('foo'))).to.throw(
599+
'Function CollectionReference.add() called with invalid data.'
600+
);
601+
}
602+
);
603+
592604
validationIt(persistence, 'must not contain undefined.', db => {
593605
// Note: This test uses the default setting for `ignoreUndefinedProperties`.
594606
return expectWriteToFail(

0 commit comments

Comments
 (0)