-
Notifications
You must be signed in to change notification settings - Fork 944
Add Transaction #3153
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 Transaction #3153
Changes from all commits
Commits
Show all changes
95 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 1da4a6f
Add Transaction
schmidt-sebastian 7736e3d
Lint
schmidt-sebastian 5dc9471
Merge
schmidt-sebastian 0230550
Typo
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 581a42c
Merge
schmidt-sebastian f77c7a9
Merge
schmidt-sebastian 3d6bb9c
Feedback
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/** | ||
* @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 '../../'; | ||
|
||
import { UserDataReader } from '../../../src/api/user_data_reader'; | ||
import { Transaction as InternalTransaction } from '../../../src/core/transaction'; | ||
import { | ||
Document, | ||
MaybeDocument, | ||
NoDocument | ||
} from '../../../src/model/document'; | ||
import { fail } from '../../../src/util/assert'; | ||
import { applyFirestoreDataConverter } from '../../../src/api/database'; | ||
import { DocumentSnapshot } from './snapshot'; | ||
import { Firestore } from './database'; | ||
import { TransactionRunner } from '../../../src/core/transaction_runner'; | ||
import { AsyncQueue } from '../../../src/util/async_queue'; | ||
import { Deferred } from '../../../src/util/promise'; | ||
import { FieldPath as ExternalFieldPath } from '../../../src/api/field_path'; | ||
import { validateReference } from './write_batch'; | ||
import { newUserDataReader } from './reference'; | ||
import { FieldPath } from './field_path'; | ||
import { cast } from './util'; | ||
|
||
export class Transaction implements firestore.Transaction { | ||
// This is the lite version of the Transaction API used in the legacy SDK. The | ||
// class is a close copy but takes different input types. | ||
|
||
private readonly _dataReader: UserDataReader; | ||
|
||
constructor( | ||
private readonly _firestore: Firestore, | ||
private readonly _transaction: InternalTransaction | ||
) { | ||
// 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! | ||
); | ||
} | ||
|
||
get<T>( | ||
documentRef: firestore.DocumentReference<T> | ||
): Promise<firestore.DocumentSnapshot<T>> { | ||
const ref = validateReference(documentRef, this._firestore); | ||
return this._transaction | ||
.lookup([ref._key]) | ||
.then((docs: MaybeDocument[]) => { | ||
if (!docs || docs.length !== 1) { | ||
return fail('Mismatch in docs returned from document lookup.'); | ||
} | ||
const doc = docs[0]; | ||
if (doc instanceof NoDocument) { | ||
return new DocumentSnapshot<T>( | ||
this._firestore, | ||
ref._key, | ||
null, | ||
ref._converter | ||
); | ||
} else if (doc instanceof Document) { | ||
return new DocumentSnapshot<T>( | ||
this._firestore, | ||
doc.key, | ||
doc, | ||
ref._converter | ||
); | ||
} else { | ||
throw fail( | ||
`BatchGetDocumentsRequest returned unexpected document type: ${doc.constructor.name}` | ||
); | ||
} | ||
}); | ||
} | ||
|
||
set<T>(documentRef: firestore.DocumentReference<T>, value: T): Transaction; | ||
set<T>( | ||
documentRef: firestore.DocumentReference<T>, | ||
value: Partial<T>, | ||
options: firestore.SetOptions | ||
): Transaction; | ||
set<T>( | ||
documentRef: firestore.DocumentReference<T>, | ||
value: T, | ||
options?: firestore.SetOptions | ||
): Transaction { | ||
const ref = validateReference(documentRef, this._firestore); | ||
const [convertedValue] = applyFirestoreDataConverter( | ||
ref._converter, | ||
value, | ||
'Transaction.set' | ||
); | ||
const parsed = this._dataReader.parseSetData( | ||
'Transaction.set', | ||
convertedValue, | ||
options | ||
); | ||
this._transaction.set(ref._key, parsed); | ||
return this; | ||
} | ||
|
||
update( | ||
documentRef: firestore.DocumentReference<unknown>, | ||
value: firestore.UpdateData | ||
): Transaction; | ||
update( | ||
documentRef: firestore.DocumentReference<unknown>, | ||
field: string | ExternalFieldPath, | ||
value: unknown, | ||
...moreFieldsAndValues: unknown[] | ||
): Transaction; | ||
update( | ||
documentRef: firestore.DocumentReference<unknown>, | ||
fieldOrUpdateData: string | ExternalFieldPath | firestore.UpdateData, | ||
value?: unknown, | ||
...moreFieldsAndValues: unknown[] | ||
): Transaction { | ||
const ref = validateReference(documentRef, this._firestore); | ||
|
||
let parsed; | ||
if ( | ||
typeof fieldOrUpdateData === 'string' || | ||
fieldOrUpdateData instanceof FieldPath | ||
) { | ||
parsed = this._dataReader.parseUpdateVarargs( | ||
'Transaction.update', | ||
fieldOrUpdateData, | ||
value, | ||
moreFieldsAndValues | ||
); | ||
} else { | ||
parsed = this._dataReader.parseUpdateData( | ||
'Transaction.update', | ||
fieldOrUpdateData | ||
); | ||
} | ||
|
||
this._transaction.update(ref._key, parsed); | ||
return this; | ||
} | ||
|
||
delete(documentRef: firestore.DocumentReference<unknown>): Transaction { | ||
const ref = validateReference(documentRef, this._firestore); | ||
this._transaction.delete(ref._key); | ||
return this; | ||
} | ||
} | ||
|
||
export function runTransaction<T>( | ||
firestore: firestore.FirebaseFirestore, | ||
updateFunction: (transaction: firestore.Transaction) => Promise<T> | ||
): Promise<T> { | ||
const firestoreClient = cast(firestore, Firestore); | ||
return firestoreClient._ensureClientConfigured().then(async datastore => { | ||
const deferred = new Deferred<T>(); | ||
new TransactionRunner<T>( | ||
new AsyncQueue(), | ||
datastore, | ||
internalTransaction => | ||
updateFunction(new Transaction(firestoreClient, internalTransaction)), | ||
deferred | ||
).run(); | ||
return deferred.promise; | ||
}); | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of adding
void
to the generic? It seems to compile and run fine even when I removed it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without it
new Deferred()
isDeferred<unknown>
. I would like it to default it toDeferred<void>
.