/
transaction.ts
282 lines (267 loc) · 9.37 KB
/
transaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @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 { getModularInstance } from '@firebase/util';
import { Transaction as InternalTransaction } from '../core/transaction';
import { TransactionRunner } from '../core/transaction_runner';
import { fail } from '../util/assert';
import { newAsyncQueue } from '../util/async_queue_impl';
import { cast } from '../util/input_validation';
import { Deferred } from '../util/promise';
import { getDatastore } from './components';
import { Firestore } from './database';
import { FieldPath } from './field_path';
import {
DocumentReference,
PartialWithFieldValue,
SetOptions,
UpdateData,
WithFieldValue
} from './reference';
import {
applyFirestoreDataConverter,
LiteUserDataWriter
} from './reference_impl';
import { DocumentSnapshot } from './snapshot';
import {
newUserDataReader,
parseSetData,
parseUpdateData,
parseUpdateVarargs,
UserDataReader
} from './user_data_reader';
import { validateReference } from './write_batch';
// TODO(mrschmidt) Consider using `BaseTransaction` as the base class in the
// legacy SDK.
/**
* A reference to a transaction.
*
* The `Transaction` object passed to a transaction's `updateFunction` provides
* the methods to read and write data within the transaction context. See
* {@link runTransaction}.
*/
export class Transaction {
// This is the tree-shakeable version of the Transaction class used in the
// legacy SDK. The class is a close copy but takes different input and output
// types. The firestore-exp SDK further extends this class to return its API
// type.
private readonly _dataReader: UserDataReader;
/** @hideconstructor */
constructor(
protected readonly _firestore: Firestore,
private readonly _transaction: InternalTransaction
) {
this._dataReader = newUserDataReader(_firestore);
}
/**
* Reads the document referenced by the provided {@link DocumentReference}.
*
* @param documentRef - A reference to the document to be read.
* @returns A `DocumentSnapshot` with the read data.
*/
get<T>(documentRef: DocumentReference<T>): Promise<DocumentSnapshot<T>> {
const ref = validateReference(documentRef, this._firestore);
const userDataWriter = new LiteUserDataWriter(this._firestore);
return this._transaction.lookup([ref._key]).then(docs => {
if (!docs || docs.length !== 1) {
return fail('Mismatch in docs returned from document lookup.');
}
const doc = docs[0];
if (doc.isFoundDocument()) {
return new DocumentSnapshot(
this._firestore,
userDataWriter,
doc.key,
doc,
ref.converter
);
} else if (doc.isNoDocument()) {
return new DocumentSnapshot(
this._firestore,
userDataWriter,
ref._key,
null,
ref.converter
);
} else {
throw fail(
`BatchGetDocumentsRequest returned unexpected document: ${doc}`
);
}
});
}
/**
* Writes to the document referred to by the provided {@link
* DocumentReference}. If the document does not exist yet, it will be created.
*
* @param documentRef - A reference to the document to be set.
* @param data - An object of the fields and values for the document.
* @throws Error - If the provided input is not a valid Firestore document.
* @returns This `Transaction` instance. Used for chaining method calls.
*/
set<T>(documentRef: DocumentReference<T>, data: WithFieldValue<T>): this;
/**
* Writes to the document referred to by the provided {@link
* DocumentReference}. If the document does not exist yet, it will be created.
* If you provide `merge` or `mergeFields`, the provided data can be merged
* into an existing document.
*
* @param documentRef - A reference to the document to be set.
* @param data - An object of the fields and values for the document.
* @param options - An object to configure the set behavior.
* @throws Error - If the provided input is not a valid Firestore document.
* @returns This `Transaction` instance. Used for chaining method calls.
*/
set<T>(
documentRef: DocumentReference<T>,
data: PartialWithFieldValue<T>,
options: SetOptions
): this;
set<T>(
documentRef: DocumentReference<T>,
value: PartialWithFieldValue<T>,
options?: SetOptions
): this {
const ref = validateReference(documentRef, this._firestore);
const convertedValue = applyFirestoreDataConverter(
ref.converter,
value,
options
);
const parsed = parseSetData(
this._dataReader,
'Transaction.set',
ref._key,
convertedValue,
ref.converter !== null,
options
);
this._transaction.set(ref._key, parsed);
return this;
}
/**
* Updates fields in the document referred to by the provided {@link
* DocumentReference}. The update will fail if applied to a document that does
* not exist.
*
* @param documentRef - A reference to the document to be updated.
* @param data - An object containing the fields and values with which to
* update the document. Fields can contain dots to reference nested fields
* within the document.
* @throws Error - If the provided input is not valid Firestore data.
* @returns This `Transaction` instance. Used for chaining method calls.
*/
update<T>(documentRef: DocumentReference<T>, data: UpdateData<T>): this;
/**
* Updates fields in the document referred to by the provided {@link
* DocumentReference}. The update will fail if applied to a document that does
* not exist.
*
* Nested fields can be updated by providing dot-separated field path
* strings or by providing `FieldPath` objects.
*
* @param documentRef - A reference to the document to be updated.
* @param field - The first field to update.
* @param value - The first value.
* @param moreFieldsAndValues - Additional key/value pairs.
* @throws Error - If the provided input is not valid Firestore data.
* @returns This `Transaction` instance. Used for chaining method calls.
*/
update(
documentRef: DocumentReference<unknown>,
field: string | FieldPath,
value: unknown,
...moreFieldsAndValues: unknown[]
): this;
update<T>(
documentRef: DocumentReference<T>,
fieldOrUpdateData: string | FieldPath | UpdateData<T>,
value?: unknown,
...moreFieldsAndValues: unknown[]
): this {
const ref = validateReference(documentRef, this._firestore);
// For Compat types, we have to "extract" the underlying types before
// performing validation.
fieldOrUpdateData = getModularInstance(fieldOrUpdateData);
let parsed;
if (
typeof fieldOrUpdateData === 'string' ||
fieldOrUpdateData instanceof FieldPath
) {
parsed = parseUpdateVarargs(
this._dataReader,
'Transaction.update',
ref._key,
fieldOrUpdateData,
value,
moreFieldsAndValues
);
} else {
parsed = parseUpdateData(
this._dataReader,
'Transaction.update',
ref._key,
fieldOrUpdateData
);
}
this._transaction.update(ref._key, parsed);
return this;
}
/**
* Deletes the document referred to by the provided {@link DocumentReference}.
*
* @param documentRef - A reference to the document to be deleted.
* @returns This `Transaction` instance. Used for chaining method calls.
*/
delete(documentRef: DocumentReference<unknown>): this {
const ref = validateReference(documentRef, this._firestore);
this._transaction.delete(ref._key);
return this;
}
}
/**
* Executes the given `updateFunction` and then attempts to commit the changes
* applied within the transaction. If any document read within the transaction
* has changed, Cloud Firestore retries the `updateFunction`. If it fails to
* commit after 5 attempts, the transaction fails.
*
* The maximum number of writes allowed in a single transaction is 500.
*
* @param firestore - A reference to the Firestore database to run this
* transaction against.
* @param updateFunction - The function to execute within the transaction
* context.
* @returns If the transaction completed successfully or was explicitly aborted
* (the `updateFunction` returned a failed promise), the promise returned by the
* `updateFunction `is returned here. Otherwise, if the transaction failed, a
* rejected promise with the corresponding failure error is returned.
*/
export function runTransaction<T>(
firestore: Firestore,
updateFunction: (transaction: Transaction) => Promise<T>
): Promise<T> {
firestore = cast(firestore, Firestore);
const datastore = getDatastore(firestore);
const deferred = new Deferred<T>();
new TransactionRunner<T>(
newAsyncQueue(),
datastore,
internalTransaction =>
updateFunction(new Transaction(firestore, internalTransaction)),
deferred
).run();
return deferred.promise;
}