Skip to content

Commit 27d4286

Browse files
Add addDoc() (#3141)
1 parent 2c66c4e commit 27d4286

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

packages/firestore/lite/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export function getQuery<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
269269
export function addDoc<T>(
270270
reference: CollectionReference<T>,
271271
data: T
272-
): Promise<DocumentSnapshot<T>>;
272+
): Promise<DocumentReference<T>>;
273273
export function setDoc<T>(
274274
reference: DocumentReference<T>,
275275
data: T

packages/firestore/lite/index.node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export {
3737
parent,
3838
getDoc,
3939
deleteDoc,
40-
setDoc
40+
setDoc,
41+
addDoc
4142
} from './src/api/reference';
4243

4344
// TOOD(firestorelite): Add tests when setDoc() is available

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,37 @@ export function deleteDoc(
320320
);
321321
}
322322

323+
export function addDoc<T>(
324+
reference: firestore.CollectionReference<T>,
325+
data: T
326+
): Promise<firestore.DocumentReference<T>> {
327+
const collRef = cast(reference, CollectionReference);
328+
const docRef = doc(collRef);
329+
330+
const [convertedValue] = applyFirestoreDataConverter(
331+
collRef._converter,
332+
data,
333+
'addDoc'
334+
);
335+
336+
// Kick off configuring the client, which freezes the settings.
337+
const configureClient = collRef.firestore._ensureClientConfigured();
338+
const dataReader = newUserDataReader(
339+
collRef.firestore._databaseId,
340+
collRef.firestore._settings!
341+
);
342+
const parsed = dataReader.parseSetData('addDoc', convertedValue);
343+
344+
return configureClient
345+
.then(datastore =>
346+
invokeCommitRpc(
347+
datastore,
348+
parsed.toMutations(docRef._key, Precondition.exists(false))
349+
)
350+
)
351+
.then(() => docRef);
352+
}
353+
323354
function newUserDataReader(
324355
databaseId: DatabaseId,
325356
settings: firestore.Settings

packages/firestore/lite/test/helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
DEFAULT_PROJECT_ID,
2626
DEFAULT_SETTINGS
2727
} from '../../test/integration/util/settings';
28+
import { AutoId } from '../../src/util/misc';
2829

2930
let appCount = 0;
3031

@@ -66,3 +67,11 @@ export function withTestDocAndInitialData(
6667
return fn(ref);
6768
});
6869
}
70+
71+
export function withTestCollection(
72+
fn: (doc: firestore.CollectionReference) => void | Promise<void>
73+
): Promise<void> {
74+
return withTestDb(db => {
75+
return fn(collection(db, AutoId.newId()));
76+
});
77+
}

packages/firestore/lite/test/integration.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
initializeFirestore
2525
} from '../src/api/database';
2626
import {
27+
withTestCollection,
2728
withTestDb,
2829
withTestDbSettings,
2930
withTestDoc,
@@ -37,7 +38,8 @@ import {
3738
DocumentReference,
3839
getDoc,
3940
deleteDoc,
40-
setDoc
41+
setDoc,
42+
addDoc
4143
} from '../src/api/reference';
4244
import { FieldPath } from '../src/api/field_path';
4345
import {
@@ -258,6 +260,24 @@ describe('setDoc()', () => {
258260
});
259261
});
260262

263+
describe('addDoc()', () => {
264+
it('can add a document', () => {
265+
return withTestCollection(async collRef => {
266+
const docRef = await addDoc(collRef, { val: 1 });
267+
const docSnap = await getDoc(docRef);
268+
expect(docSnap.data()).to.deep.equal({ val: 1 });
269+
});
270+
});
271+
272+
it('throws when user input fails validation', () => {
273+
return withTestCollection(async collRef => {
274+
expect(() => addDoc(collRef, { val: undefined })).to.throw(
275+
'Function addDoc() called with invalid data. Unsupported field value: undefined (found in field val)'
276+
);
277+
});
278+
});
279+
});
280+
261281
describe('DocumentSnapshot', () => {
262282
it('can represent missing data', () => {
263283
return withTestDoc(async docRef => {
@@ -303,6 +323,7 @@ describe('DocumentSnapshot', () => {
303323
});
304324
});
305325

326+
// TODO(firestorelite): Add converter tests
306327
describe('deleteDoc()', () => {
307328
it('can delete a non-existing document', () => {
308329
return withTestDoc(docRef => deleteDoc(docRef));

0 commit comments

Comments
 (0)