Skip to content

Add WriteBatch #3152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 89 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
da82b5d
Add Firestore, initializeFirestore, getFirestore to Firestore lite
schmidt-sebastian May 26, 2020
30bfc3f
Review
schmidt-sebastian May 27, 2020
f49096f
add one more tests
schmidt-sebastian May 27, 2020
da3403f
Fix lint
schmidt-sebastian May 27, 2020
bd3596e
Add lite to build
schmidt-sebastian May 27, 2020
3aac6f4
Add private
schmidt-sebastian May 27, 2020
6f05e2f
Fix Path
schmidt-sebastian May 27, 2020
634145a
Remove unused import
schmidt-sebastian May 27, 2020
8187cb7
Update package.json
schmidt-sebastian May 27, 2020
5760b02
Simplify
schmidt-sebastian May 27, 2020
4ba0f4e
Merge branch 'mrschmidt/initializefirestore' of github.com:firebase/f…
schmidt-sebastian May 27, 2020
e40faf4
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian May 27, 2020
f243a6c
Add DocumentReference
schmidt-sebastian May 27, 2020
d48405b
Feedback
schmidt-sebastian May 28, 2020
804fe94
Typo
schmidt-sebastian May 28, 2020
d244bcd
Lint fix
schmidt-sebastian May 28, 2020
bc342a3
Update integration.test.ts
schmidt-sebastian May 28, 2020
5297d47
Update rollup.config.lite.js
schmidt-sebastian May 28, 2020
867d9c0
One more test
schmidt-sebastian May 28, 2020
084fdcf
Update reference.ts
schmidt-sebastian May 28, 2020
ca44064
Add CollectionReference
schmidt-sebastian May 28, 2020
920c0e2
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian May 28, 2020
d5745f0
Merge
schmidt-sebastian May 28, 2020
b636d27
Merge
schmidt-sebastian May 28, 2020
31ba3ba
Add DocumentSnapshot
schmidt-sebastian May 28, 2020
505792f
Add exports
schmidt-sebastian May 28, 2020
3750a00
Add getDoc()
schmidt-sebastian May 28, 2020
5d3e2aa
Fix build
schmidt-sebastian May 28, 2020
58a84d1
More integration test fixes
schmidt-sebastian May 29, 2020
6f91171
Fix Integration tests
schmidt-sebastian May 29, 2020
c63977f
Add DocumentReference
schmidt-sebastian May 29, 2020
6352026
Add deleteDoc()
schmidt-sebastian May 29, 2020
be02305
Cleanup
schmidt-sebastian May 29, 2020
518631d
Merge
schmidt-sebastian May 29, 2020
6318fa4
Update datastore.ts
schmidt-sebastian May 29, 2020
a4ff5fd
Add setDoc()
schmidt-sebastian May 29, 2020
72b0b8b
Add setDoc()
schmidt-sebastian May 29, 2020
f3a4316
Add addDoc()
schmidt-sebastian May 30, 2020
efa58c4
Add converter
schmidt-sebastian May 30, 2020
7649b67
Throw when user data parsing fails
schmidt-sebastian May 30, 2020
3aa5d2f
Merge
schmidt-sebastian May 30, 2020
66d72d0
Throw when user data parsing fails
schmidt-sebastian May 30, 2020
e5b5d8f
Add test
schmidt-sebastian May 30, 2020
ca9ab39
Update test names
schmidt-sebastian May 30, 2020
97cf094
Merge branch 'mrschmidt/setdoc' into mrschmidt/adddoc
schmidt-sebastian May 30, 2020
1a81864
Simplify
schmidt-sebastian May 30, 2020
068d5a5
Simplify
schmidt-sebastian May 30, 2020
94e22df
Merge branch 'mrschmidt/newgetdocument' into mrschmidt/deletedoc
schmidt-sebastian May 30, 2020
555a16c
Merge branch 'mrschmidt/deletedoc' of github.com:firebase/firebase-js…
schmidt-sebastian May 30, 2020
61b7c4c
Merge branch 'mrschmidt/deletedoc' into mrschmidt/setdoc
schmidt-sebastian May 30, 2020
d8abac1
Simplify
schmidt-sebastian May 30, 2020
ad1a038
Merge branch 'mrschmidt/setdoc' into mrschmidt/adddoc
schmidt-sebastian May 30, 2020
b2fd485
Simplify
schmidt-sebastian May 30, 2020
df8186b
addDoc
schmidt-sebastian May 30, 2020
8a7fc93
Add documentID() (#3137)
schmidt-sebastian Jun 1, 2020
8bb9829
Add updateDoc()
schmidt-sebastian Jun 2, 2020
3242bc0
Add WriteBatch
schmidt-sebastian Jun 2, 2020
f3bfcb2
Use validateReference
schmidt-sebastian Jun 2, 2020
637b4bd
Update types
schmidt-sebastian Jun 2, 2020
7736e3d
Lint
schmidt-sebastian Jun 2, 2020
c3e3375
Merge
schmidt-sebastian Jun 3, 2020
835ad93
Merge
schmidt-sebastian Jun 3, 2020
bd5a998
Merge
schmidt-sebastian Jun 3, 2020
f6553cb
Merge branch 'mrschmidt/snapshot' of github.com:firebase/firebase-js-…
schmidt-sebastian Jun 3, 2020
25f01e8
Merge
schmidt-sebastian Jun 3, 2020
0174149
Add terminate()
schmidt-sebastian Jun 3, 2020
7c4e78a
Lint
schmidt-sebastian Jun 3, 2020
5e62710
lint
schmidt-sebastian Jun 3, 2020
9aa0263
Add test
schmidt-sebastian Jun 3, 2020
2685912
Simplify
schmidt-sebastian Jun 4, 2020
6a34bbb
Merge
schmidt-sebastian Jun 4, 2020
3361a63
Feedback
schmidt-sebastian Jun 4, 2020
27bed90
Merge
schmidt-sebastian Jun 4, 2020
6f8521a
Merge branch 'master' into mrschmidt/newgetdocument
schmidt-sebastian Jun 4, 2020
4142278
Merge
schmidt-sebastian Jun 4, 2020
a005ed5
Merge
schmidt-sebastian Jun 4, 2020
d7200bd
Merge
schmidt-sebastian Jun 5, 2020
6af39e9
Merge
schmidt-sebastian Jun 5, 2020
b327858
Merge
schmidt-sebastian Jun 5, 2020
403fe89
Merge
schmidt-sebastian Jun 5, 2020
93d89cd
Lint
schmidt-sebastian Jun 5, 2020
7b6e899
Merge
schmidt-sebastian Jun 5, 2020
03f8223
Merge
schmidt-sebastian Jun 5, 2020
2216e4a
Merge
schmidt-sebastian Jun 5, 2020
29b9fa9
Merge branch 'master' into mrschmidt/terminate
schmidt-sebastian Jun 5, 2020
07005ec
fix build
schmidt-sebastian Jun 5, 2020
cba92df
Merge
schmidt-sebastian Jun 5, 2020
863a6d8
Fix
schmidt-sebastian Jun 5, 2020
8d14d84
Merge
schmidt-sebastian Jun 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/firestore/lite/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ export class Transaction {

get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>>;

set<T>(documentRef: DocumentReference<T>, data: T): Transaction;
set<T>(
documentRef: DocumentReference<T>,
data: T,
options?: SetOptions
data: Partial<T>,
options: SetOptions
): Transaction;

update(documentRef: DocumentReference<any>, data: UpdateData): Transaction;
Expand All @@ -160,10 +161,11 @@ export class Transaction {
export class WriteBatch {
private constructor();

set<T>(documentRef: DocumentReference<T>, data: T): WriteBatch;
set<T>(
documentRef: DocumentReference<T>,
data: T,
options?: SetOptions
data: Partial<T>,
options: SetOptions
): WriteBatch;

update(documentRef: DocumentReference<any>, data: UpdateData): WriteBatch;
Expand Down
2 changes: 2 additions & 0 deletions packages/firestore/lite/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export {

export { DocumentSnapshot, QueryDocumentSnapshot } from './src/api/snapshot';

export { WriteBatch, writeBatch } from './src/api/write_batch';

export { setLogLevel } from '../src/util/log';

export function registerFirestore(): void {
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/lite/src/api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,17 @@ export function setDoc<T>(
}

export function updateDoc(
reference: firestore.DocumentReference,
reference: firestore.DocumentReference<unknown>,
data: firestore.UpdateData
): Promise<void>;
export function updateDoc(
reference: firestore.DocumentReference,
reference: firestore.DocumentReference<unknown>,
field: string | firestore.FieldPath,
value: unknown,
...moreFieldsAndValues: unknown[]
): Promise<void>;
export function updateDoc(
reference: firestore.DocumentReference,
reference: firestore.DocumentReference<unknown>,
fieldOrUpdateData: string | firestore.FieldPath | firestore.UpdateData,
value?: unknown,
...moreFieldsAndValues: unknown[]
Expand Down Expand Up @@ -418,7 +418,7 @@ export function addDoc<T>(
.then(() => docRef);
}

function newUserDataReader(
export function newUserDataReader(
databaseId: DatabaseId,
settings: firestore.Settings
): UserDataReader {
Expand Down
179 changes: 179 additions & 0 deletions packages/firestore/lite/src/api/write_batch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as firestore from '../../index';
import {
DeleteMutation,
Mutation,
Precondition
} from '../../../src/model/mutation';
import { Code, FirestoreError } from '../../../src/util/error';
import { applyFirestoreDataConverter } from '../../../src/api/database';
import {
DocumentKeyReference,
UserDataReader
} from '../../../src/api/user_data_reader';
import { cast } from './util';
import { DocumentReference, newUserDataReader } from './reference';
import { Firestore } from './database';
import { invokeCommitRpc } from '../../../src/remote/datastore';
import { FieldPath } from './field_path';

export class WriteBatch implements firestore.WriteBatch {
// This is the lite version of the WriteBatch API used in the legacy SDK. The
// class is a close copy but takes different input types.

private _mutations = [] as Mutation[];
private _committed = false;
private _dataReader: UserDataReader;

constructor(private _firestore: Firestore) {
// Kick off configuring the client, which freezes the settings.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
_firestore._ensureClientConfigured();
this._dataReader = newUserDataReader(
_firestore._databaseId,
_firestore._settings!
);
}

set<T>(documentRef: firestore.DocumentReference<T>, value: T): WriteBatch;
set<T>(
documentRef: firestore.DocumentReference<T>,
value: Partial<T>,
options: firestore.SetOptions
): WriteBatch;
set<T>(
documentRef: firestore.DocumentReference<T>,
value: T,
options?: firestore.SetOptions
): WriteBatch {
this.verifyNotCommitted();
const ref = validateReference(documentRef, this._firestore);

const [convertedValue] = applyFirestoreDataConverter(
ref._converter,
value,
'WriteBatch.set'
);

const parsed = this._dataReader.parseSetData(
'WriteBatch.set',
convertedValue,
options
);
this._mutations = this._mutations.concat(
parsed.toMutations(ref._key, Precondition.none())
);
return this;
}

update(
documentRef: firestore.DocumentReference<unknown>,
value: firestore.UpdateData
): WriteBatch;
update(
documentRef: firestore.DocumentReference<unknown>,
field: string | firestore.FieldPath,
value: unknown,
...moreFieldsAndValues: unknown[]
): WriteBatch;
update(
documentRef: firestore.DocumentReference<unknown>,
fieldOrUpdateData: string | firestore.FieldPath | firestore.UpdateData,
value?: unknown,
...moreFieldsAndValues: unknown[]
): WriteBatch {
this.verifyNotCommitted();
const ref = validateReference(documentRef, this._firestore);

let parsed;

if (
typeof fieldOrUpdateData === 'string' ||
fieldOrUpdateData instanceof FieldPath
) {
parsed = this._dataReader.parseUpdateVarargs(
'WriteBatch.update',
fieldOrUpdateData,
value,
moreFieldsAndValues
);
} else {
parsed = this._dataReader.parseUpdateData(
'WriteBatch.update',
fieldOrUpdateData
);
}

this._mutations = this._mutations.concat(
parsed.toMutations(ref._key, Precondition.exists(true))
);
return this;
}

delete(documentRef: firestore.DocumentReference<unknown>): WriteBatch {
this.verifyNotCommitted();
const ref = validateReference(documentRef, this._firestore);
this._mutations = this._mutations.concat(
new DeleteMutation(ref._key, Precondition.none())
);
return this;
}

commit(): Promise<void> {
this.verifyNotCommitted();
this._committed = true;
if (this._mutations.length > 0) {
return this._firestore
._ensureClientConfigured()
.then(datastore => invokeCommitRpc(datastore, this._mutations));
}

return Promise.resolve();
}

private verifyNotCommitted(): void {
if (this._committed) {
throw new FirestoreError(
Code.FAILED_PRECONDITION,
'A write batch can no longer be used after commit() ' +
'has been called.'
);
}
}
}

export function validateReference<T>(
documentRef: firestore.DocumentReference<T>,
firestore: Firestore
): DocumentKeyReference<T> {
if (documentRef.firestore !== firestore) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Provided document reference is from a different Firestore instance.'
);
} else {
return cast(documentRef, DocumentReference) as DocumentReference<T>;
}
}

export function writeBatch(
firestore: firestore.FirebaseFirestore
): firestore.WriteBatch {
return new WriteBatch(cast(firestore, Firestore));
}
Loading