Skip to content

Commit 08bb538

Browse files
Add setDoc, updateDoc, deleteDoc and addDoc
1 parent 64162a2 commit 08bb538

File tree

5 files changed

+371
-8
lines changed

5 files changed

+371
-8
lines changed

packages/firestore/exp/index.node.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ export {
4444

4545
export { runTransaction, Transaction } from '../lite/src/api/transaction';
4646

47-
export { getDoc } from './src/api/reference';
47+
export {
48+
getDoc,
49+
addDoc,
50+
setDoc,
51+
deleteDoc,
52+
updateDoc
53+
} from './src/api/reference';
4854

4955
export {
5056
FieldValue,

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

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,28 @@
2020
import * as firestore from '../../index';
2121

2222
import { Firestore } from './database';
23-
import { DocumentKeyReference } from '../../../src/api/user_data_reader';
23+
import {
24+
DocumentKeyReference,
25+
ParsedUpdateData
26+
} from '../../../src/api/user_data_reader';
2427
import { debugAssert } from '../../../src/util/assert';
2528
import { cast } from '../../../lite/src/api/util';
2629
import { DocumentSnapshot } from './snapshot';
2730
import {
31+
applyFirestoreDataConverter,
2832
getDocViaSnapshotListener,
2933
SnapshotMetadata
3034
} from '../../../src/api/database';
3135
import { ViewSnapshot } from '../../../src/core/view_snapshot';
32-
import { DocumentReference } from '../../../lite/src/api/reference';
36+
import {
37+
CollectionReference,
38+
doc,
39+
DocumentReference,
40+
newUserDataReader
41+
} from '../../../lite/src/api/reference';
42+
import { invokeCommitRpc } from '../../../src/remote/datastore';
43+
import { DeleteMutation, Precondition } from '../../../src/model/mutation';
44+
import { FieldPath } from '../../../lite/src/api/field_path';
3345

3446
export function getDoc<T>(
3547
reference: firestore.DocumentReference<T>
@@ -42,6 +54,131 @@ export function getDoc<T>(
4254
});
4355
}
4456

57+
export function setDoc<T>(
58+
reference: firestore.DocumentReference<T>,
59+
data: T
60+
): Promise<void>;
61+
export function setDoc<T>(
62+
reference: firestore.DocumentReference<T>,
63+
data: Partial<T>,
64+
options: firestore.SetOptions
65+
): Promise<void>;
66+
export function setDoc<T>(
67+
reference: firestore.DocumentReference<T>,
68+
data: T,
69+
options?: firestore.SetOptions
70+
): Promise<void> {
71+
const ref = cast<DocumentReference<T>>(reference, DocumentReference);
72+
const firestore = cast(ref.firestore, Firestore);
73+
74+
const [convertedValue] = applyFirestoreDataConverter(
75+
ref._converter,
76+
data,
77+
'setDoc'
78+
);
79+
const dataReader = newUserDataReader(firestore);
80+
const parsed = dataReader.parseSetData(
81+
'setDoc',
82+
ref._key,
83+
convertedValue,
84+
options
85+
);
86+
87+
return firestore
88+
._getFirestoreClient()
89+
.then(firestoreClient =>
90+
firestoreClient.write(parsed.toMutations(ref._key, Precondition.none()))
91+
);
92+
}
93+
94+
export function updateDoc(
95+
reference: firestore.DocumentReference<unknown>,
96+
data: firestore.UpdateData
97+
): Promise<void>;
98+
export function updateDoc(
99+
reference: firestore.DocumentReference<unknown>,
100+
field: string | firestore.FieldPath,
101+
value: unknown,
102+
...moreFieldsAndValues: unknown[]
103+
): Promise<void>;
104+
export function updateDoc(
105+
reference: firestore.DocumentReference<unknown>,
106+
fieldOrUpdateData: string | firestore.FieldPath | firestore.UpdateData,
107+
value?: unknown,
108+
...moreFieldsAndValues: unknown[]
109+
): Promise<void> {
110+
const ref = cast<DocumentReference<unknown>>(reference, DocumentReference);
111+
const firestore = cast(ref.firestore, Firestore);
112+
const dataReader = newUserDataReader(firestore);
113+
114+
let parsed: ParsedUpdateData;
115+
if (
116+
typeof fieldOrUpdateData === 'string' ||
117+
fieldOrUpdateData instanceof FieldPath
118+
) {
119+
parsed = dataReader.parseUpdateVarargs(
120+
'updateDoc',
121+
ref._key,
122+
fieldOrUpdateData,
123+
value,
124+
moreFieldsAndValues
125+
);
126+
} else {
127+
parsed = dataReader.parseUpdateData(
128+
'updateDoc',
129+
ref._key,
130+
fieldOrUpdateData
131+
);
132+
}
133+
134+
return firestore
135+
._getFirestoreClient()
136+
.then(firestoreClient =>
137+
firestoreClient.write(
138+
parsed.toMutations(ref._key, Precondition.exists(true))
139+
)
140+
);
141+
}
142+
143+
export function deleteDoc(
144+
reference: firestore.DocumentReference
145+
): Promise<void> {
146+
const ref = cast<DocumentReference<unknown>>(reference, DocumentReference);
147+
const firestore = cast(ref.firestore, Firestore);
148+
return firestore
149+
._getFirestoreClient()
150+
.then(firestoreClient =>
151+
firestoreClient.write([new DeleteMutation(ref._key, Precondition.none())])
152+
);
153+
}
154+
155+
export function addDoc<T>(
156+
reference: firestore.CollectionReference<T>,
157+
data: T
158+
): Promise<firestore.DocumentReference<T>> {
159+
const collRef = cast<CollectionReference<T>>(reference, CollectionReference);
160+
const firestore = cast(collRef, Firestore);
161+
const docRef = doc(collRef);
162+
163+
const [convertedValue] = applyFirestoreDataConverter(
164+
collRef._converter,
165+
data,
166+
'addDoc'
167+
);
168+
169+
const dataReader = newUserDataReader(collRef.firestore);
170+
const parsed = dataReader.parseSetData('addDoc', docRef._key, convertedValue);
171+
172+
return firestore
173+
._getFirestoreClient()
174+
.then(firestoreClient =>
175+
firestoreClient.write(
176+
parsed.toMutations(docRef._key, Precondition.exists(false))
177+
)
178+
)
179+
.then(() => docRef);
180+
}
181+
45182
/**
46183
* Converts a ViewSnapshot that contains the single document specified by `ref`
47184
* to a DocumentSnapshot.

packages/firestore/exp/test/helpers.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
DEFAULT_SETTINGS
2727
} from '../../test/integration/util/settings';
2828
import { collection } from '../../lite/src/api/reference';
29+
import { setDoc } from '../src/api/reference';
30+
import { AutoId } from '../../src/util/misc';
2931

3032
let appCount = 0;
3133

@@ -43,6 +45,14 @@ export async function withTestDbSettings(
4345
return fn(firestore);
4446
}
4547

48+
export function withTestCollection(
49+
fn: (collRef: firestore.CollectionReference) => void | Promise<void>
50+
): Promise<void> {
51+
return withTestDb(db => {
52+
return fn(collection(db, AutoId.newId()));
53+
});
54+
}
55+
4656
export function withTestDb(
4757
fn: (db: firestore.FirebaseFirestore) => void | Promise<void>
4858
): Promise<void> {
@@ -54,3 +64,14 @@ export function withTestDoc(
5464
): Promise<void> {
5565
return withTestDb(db => fn(doc(collection(db, 'test-collection'))));
5666
}
67+
68+
export function withTestDocAndInitialData(
69+
data: firestore.DocumentData,
70+
fn: (doc: firestore.DocumentReference) => void | Promise<void>
71+
): Promise<void> {
72+
return withTestDb(async db => {
73+
const ref = doc(collection(db, 'test-collection'));
74+
await setDoc(ref, data);
75+
return fn(ref);
76+
});
77+
}

0 commit comments

Comments
 (0)