-
Notifications
You must be signed in to change notification settings - Fork 946
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
Add WriteBatch #3152
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 30bfc3f
Review
schmidt-sebastian f49096f
add one more tests
schmidt-sebastian da3403f
Fix lint
schmidt-sebastian bd3596e
Add lite to build
schmidt-sebastian 3aac6f4
Add private
schmidt-sebastian 6f05e2f
Fix Path
schmidt-sebastian 634145a
Remove unused import
schmidt-sebastian 8187cb7
Update package.json
schmidt-sebastian 5760b02
Simplify
schmidt-sebastian 4ba0f4e
Merge branch 'mrschmidt/initializefirestore' of github.com:firebase/f…
schmidt-sebastian e40faf4
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian f243a6c
Add DocumentReference
schmidt-sebastian d48405b
Feedback
schmidt-sebastian 804fe94
Typo
schmidt-sebastian d244bcd
Lint fix
schmidt-sebastian bc342a3
Update integration.test.ts
schmidt-sebastian 5297d47
Update rollup.config.lite.js
schmidt-sebastian 867d9c0
One more test
schmidt-sebastian 084fdcf
Update reference.ts
schmidt-sebastian ca44064
Add CollectionReference
schmidt-sebastian 920c0e2
Merge branch 'master' into mrschmidt/initializefirestore
schmidt-sebastian d5745f0
Merge
schmidt-sebastian b636d27
Merge
schmidt-sebastian 31ba3ba
Add DocumentSnapshot
schmidt-sebastian 505792f
Add exports
schmidt-sebastian 3750a00
Add getDoc()
schmidt-sebastian 5d3e2aa
Fix build
schmidt-sebastian 58a84d1
More integration test fixes
schmidt-sebastian 6f91171
Fix Integration tests
schmidt-sebastian c63977f
Add DocumentReference
schmidt-sebastian 6352026
Add deleteDoc()
schmidt-sebastian be02305
Cleanup
schmidt-sebastian 518631d
Merge
schmidt-sebastian 6318fa4
Update datastore.ts
schmidt-sebastian a4ff5fd
Add setDoc()
schmidt-sebastian 72b0b8b
Add setDoc()
schmidt-sebastian f3a4316
Add addDoc()
schmidt-sebastian efa58c4
Add converter
schmidt-sebastian 7649b67
Throw when user data parsing fails
schmidt-sebastian 3aa5d2f
Merge
schmidt-sebastian 66d72d0
Throw when user data parsing fails
schmidt-sebastian e5b5d8f
Add test
schmidt-sebastian ca9ab39
Update test names
schmidt-sebastian 97cf094
Merge branch 'mrschmidt/setdoc' into mrschmidt/adddoc
schmidt-sebastian 1a81864
Simplify
schmidt-sebastian 068d5a5
Simplify
schmidt-sebastian 94e22df
Merge branch 'mrschmidt/newgetdocument' into mrschmidt/deletedoc
schmidt-sebastian 555a16c
Merge branch 'mrschmidt/deletedoc' of github.com:firebase/firebase-js…
schmidt-sebastian 61b7c4c
Merge branch 'mrschmidt/deletedoc' into mrschmidt/setdoc
schmidt-sebastian d8abac1
Simplify
schmidt-sebastian ad1a038
Merge branch 'mrschmidt/setdoc' into mrschmidt/adddoc
schmidt-sebastian b2fd485
Simplify
schmidt-sebastian df8186b
addDoc
schmidt-sebastian 8a7fc93
Add documentID() (#3137)
schmidt-sebastian 8bb9829
Add updateDoc()
schmidt-sebastian 3242bc0
Add WriteBatch
schmidt-sebastian f3bfcb2
Use validateReference
schmidt-sebastian 637b4bd
Update types
schmidt-sebastian 7736e3d
Lint
schmidt-sebastian c3e3375
Merge
schmidt-sebastian 835ad93
Merge
schmidt-sebastian bd5a998
Merge
schmidt-sebastian f6553cb
Merge branch 'mrschmidt/snapshot' of github.com:firebase/firebase-js-…
schmidt-sebastian 25f01e8
Merge
schmidt-sebastian 0174149
Add terminate()
schmidt-sebastian 7c4e78a
Lint
schmidt-sebastian 5e62710
lint
schmidt-sebastian 9aa0263
Add test
schmidt-sebastian 2685912
Simplify
schmidt-sebastian 6a34bbb
Merge
schmidt-sebastian 3361a63
Feedback
schmidt-sebastian 27bed90
Merge
schmidt-sebastian 6f8521a
Merge branch 'master' into mrschmidt/newgetdocument
schmidt-sebastian 4142278
Merge
schmidt-sebastian a005ed5
Merge
schmidt-sebastian d7200bd
Merge
schmidt-sebastian 6af39e9
Merge
schmidt-sebastian b327858
Merge
schmidt-sebastian 403fe89
Merge
schmidt-sebastian 93d89cd
Lint
schmidt-sebastian 7b6e899
Merge
schmidt-sebastian 03f8223
Merge
schmidt-sebastian 2216e4a
Merge
schmidt-sebastian 29b9fa9
Merge branch 'master' into mrschmidt/terminate
schmidt-sebastian 07005ec
fix build
schmidt-sebastian cba92df
Merge
schmidt-sebastian 863a6d8
Fix
schmidt-sebastian 8d14d84
Merge
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.