Skip to content

Commit e2d8f93

Browse files
committed
Add addDoc function overloading to accept DocumentRef
1 parent ffbf5a6 commit e2d8f93

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

.changeset/silly-seahorses-jump.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/firestore': minor
3+
---
4+
5+
Add addDoc function overloading to accept DocumentRef

common/api-review/firestore.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { FirebaseApp } from '@firebase/app';
99
import { FirebaseError } from '@firebase/util';
1010
import { LogLevelString as LogLevel } from '@firebase/logger';
1111

12+
// @public
13+
export function addDoc<AppModelType, DbModelType extends DocumentData>(reference: DocumentReference<AppModelType, DbModelType>, data: WithFieldValue<AppModelType>): Promise<DocumentReference<AppModelType, DbModelType>>;
14+
1215
// @public
1316
export function addDoc<AppModelType, DbModelType extends DocumentData>(reference: CollectionReference<AppModelType, DbModelType>, data: WithFieldValue<AppModelType>): Promise<DocumentReference<AppModelType, DbModelType>>;
1417

packages/firestore/src/api/reference_impl.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,20 @@ export function deleteDoc<AppModelType, DbModelType extends DocumentData>(
438438
return executeWrite(firestore, mutations);
439439
}
440440

441+
/**
442+
* Add a new document to specified `DocReference` with the given data.
443+
*
444+
* @param reference - A reference to the document to add.
445+
* @param data - An Object containing the data for the new document.
446+
* @returns A `Promise` resolved with a `DocumentReference` pointing to the
447+
* newly created document after it has been written to the backend (Note that it
448+
* won't resolve while you're offline).
449+
* @throws FirestoreError if the document already exists.
450+
*/
451+
export function addDoc<AppModelType, DbModelType extends DocumentData>(
452+
reference: DocumentReference<AppModelType, DbModelType>,
453+
data: WithFieldValue<AppModelType>
454+
): Promise<DocumentReference<AppModelType, DbModelType>>;
441455
/**
442456
* Add a new document to specified `CollectionReference` with the given data,
443457
* assigning it a document ID automatically.
@@ -451,10 +465,17 @@ export function deleteDoc<AppModelType, DbModelType extends DocumentData>(
451465
export function addDoc<AppModelType, DbModelType extends DocumentData>(
452466
reference: CollectionReference<AppModelType, DbModelType>,
453467
data: WithFieldValue<AppModelType>
468+
): Promise<DocumentReference<AppModelType, DbModelType>>;
469+
export function addDoc<AppModelType, DbModelType extends DocumentData>(
470+
reference:
471+
| CollectionReference<AppModelType, DbModelType>
472+
| DocumentReference<AppModelType, DbModelType>,
473+
data: WithFieldValue<AppModelType>
454474
): Promise<DocumentReference<AppModelType, DbModelType>> {
455475
const firestore = cast(reference.firestore, Firestore);
456476

457-
const docRef = doc(reference);
477+
const docRef =
478+
reference instanceof DocumentReference ? reference : doc(reference);
458479
const convertedValue = applyFirestoreDataConverter(reference.converter, data);
459480

460481
const dataReader = newUserDataReader(reference.firestore);

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { deleteApp } from '@firebase/app';
19-
import { Deferred } from '@firebase/util';
19+
import { Deferred, FirebaseError } from '@firebase/util';
2020
import { expect, use } from 'chai';
2121
import chaiAsPromised from 'chai-as-promised';
2222

@@ -106,6 +106,33 @@ apiDescribe('Database', persistence => {
106106
});
107107
});
108108

109+
it('can add a document with DocRef', () => {
110+
return withTestCollection(persistence, {}, async coll => {
111+
const docRef = doc(coll, 'foo');
112+
await addDoc(docRef, { a: 'a' });
113+
const docSnapshot = await getDoc(docRef);
114+
expect(docSnapshot.data()).to.be.deep.equal({ a: 'a' });
115+
});
116+
});
117+
118+
it('can add a document with CollectionRef', () => {
119+
return withTestCollection(persistence, {}, async coll => {
120+
const docRef = await addDoc(coll, { a: 'a' });
121+
const docSnapshot = await getDoc(docRef);
122+
expect(docSnapshot.data()).to.be.deep.equal({ a: 'a' });
123+
});
124+
});
125+
126+
it("can't add a document with duplicated id", () => {
127+
return withTestDoc(persistence, async docRef => {
128+
await addDoc(docRef, { a: 'a' });
129+
await expect(addDoc(docRef, { a: 'a' })).to.be.rejectedWith(
130+
FirebaseError,
131+
'Document already exists:'
132+
);
133+
});
134+
});
135+
109136
it('can delete a document', () => {
110137
// TODO(#1865): This test fails with node:persistence against Prod
111138
return withTestDoc(persistence, docRef => {

0 commit comments

Comments
 (0)