Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

transaction.md

419 lines7.8 KB

1---
2id: Transaction
3title: Transaction
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8# Class: Transaction\<T\>
9​
10Defined in: [packages/db/src/transactions.ts:116](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L116)
11​
12## Type Parameters
13​
14• **T** *extends* `object` = `Record`\<`string`, `unknown`\>
15​
16## Constructors
17​
18### new Transaction()
19​
20```ts
21new Transaction<T>(config): Transaction<T>
22```
23​
24Defined in: [packages/db/src/transactions.ts:131](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L131)
25​
26#### Parameters
27​
28##### config
29​
30[`TransactionConfig`](../../interfaces/transactionconfig.md)\<`T`\>
31​
32#### Returns
33​
34[`Transaction`](../transaction.md)\<`T`\>
35​
36## Properties
37​
38### autoCommit
39​
40```ts
41autoCommit: boolean;
42```
43​
44Defined in: [packages/db/src/transactions.ts:122](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L122)
45​
46***
47​
48### createdAt
49​
50```ts
51createdAt: Date;
52```
53​
54Defined in: [packages/db/src/transactions.ts:123](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L123)
55​
56***
57​
58### error?
59​
60```ts
61optional error: object;
62```
63​
64Defined in: [packages/db/src/transactions.ts:126](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L126)
65​
66#### error
67​
68```ts
69error: Error;
70```
71​
72#### message
73​
74```ts
75message: string;
76```
77​
78***
79​
80### id
81​
82```ts
83id: string;
84```
85​
86Defined in: [packages/db/src/transactions.ts:117](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L117)
87​
88***
89​
90### isPersisted
91​
92```ts
93isPersisted: Deferred<Transaction<T>>;
94```
95​
96Defined in: [packages/db/src/transactions.ts:121](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L121)
97​
98***
99​
100### metadata
101​
102```ts
103metadata: Record<string, unknown>;
104```
105​
106Defined in: [packages/db/src/transactions.ts:125](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L125)
107​
108***
109​
110### mutationFn
111​
112```ts
113mutationFn: MutationFn<T>;
114```
115​
116Defined in: [packages/db/src/transactions.ts:119](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L119)
117​
118***
119​
120### mutations
121​
122```ts
123mutations: PendingMutation<T, OperationType, Collection<T, any, any, any, any>>[];
124```
125​
126Defined in: [packages/db/src/transactions.ts:120](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L120)
127​
128***
129​
130### sequenceNumber
131​
132```ts
133sequenceNumber: number;
134```
135​
136Defined in: [packages/db/src/transactions.ts:124](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L124)
137​
138***
139​
140### state
141​
142```ts
143state: TransactionState;
144```
145​
146Defined in: [packages/db/src/transactions.ts:118](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L118)
147​
148## Methods
149​
150### applyMutations()
151​
152```ts
153applyMutations(mutations): void
154```
155​
156Defined in: [packages/db/src/transactions.ts:212](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L212)
157​
158#### Parameters
159​
160##### mutations
161​
162[`PendingMutation`](../../interfaces/pendingmutation.md)\<`any`, [`OperationType`](../../type-aliases/operationtype.md), [`Collection`](../../interfaces/collection.md)\<`any`, `any`, `any`, `any`, `any`\>\>[]
163​
164#### Returns
165​
166`void`
167​
168***
169​
170### commit()
171​
172```ts
173commit(): Promise<Transaction<T>>
174```
175​
176Defined in: [packages/db/src/transactions.ts:349](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L349)
177​
178Commit the transaction and execute the mutation function
179​
180#### Returns
181​
182`Promise`\<[`Transaction`](../transaction.md)\<`T`\>\>
183​
184Promise that resolves to this transaction when complete
185​
186#### Examples
187​
188```ts
189// Manual commit (when autoCommit is false)
190const tx = createTransaction({
191 autoCommit: false,
192 mutationFn: async ({ transaction }) => {
193 await api.saveChanges(transaction.mutations)
194 }
195})
196​
197tx.mutate(() => {
198 collection.insert({ id: "1", text: "Buy milk" })
199})
200​
201await tx.commit() // Manually commit
202```
203​
204```ts
205// Handle commit errors
206try {
207 const tx = createTransaction({
208 mutationFn: async () => { throw new Error("API failed") }
209 })
210​
211 tx.mutate(() => {
212 collection.insert({ id: "1", text: "Item" })
213 })
214​
215 await tx.commit()
216} catch (error) {
217 console.log('Commit failed, transaction rolled back:', error)
219```
220​
221```ts
222// Check transaction state after commit
223await tx.commit()
224console.log(tx.state) // "completed" or "failed"
225```
226​
227***
228​
229### compareCreatedAt()
230​
231```ts
232compareCreatedAt(other): number
233```
234​
235Defined in: [packages/db/src/transactions.ts:395](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L395)
236​
237Compare two transactions by their createdAt time and sequence number in order
238to sort them in the order they were created.
239​
240#### Parameters
241​
242##### other
243​
244[`Transaction`](../transaction.md)\<`any`\>
245​
246The other transaction to compare to
247​
248#### Returns
249​
250`number`
251​
252-1 if this transaction was created before the other, 1 if it was created after, 0 if they were created at the same time
253​
254***
255​
256### mutate()
257​
258```ts
259mutate(callback): Transaction<T>
260```
261​
262Defined in: [packages/db/src/transactions.ts:193](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L193)
263​
264Execute collection operations within this transaction
265​
266#### Parameters
267​
268##### callback
269​
270() => `void`
271​
272Function containing collection operations to group together
273​
274#### Returns
275​
276[`Transaction`](../transaction.md)\<`T`\>
277​
278This transaction for chaining
279​
280#### Examples
281​
282```ts
283// Group multiple operations
284const tx = createTransaction({ mutationFn: async () => {
285 // Send to API
286}})
287​
288tx.mutate(() => {
289 collection.insert({ id: "1", text: "Buy milk" })
290 collection.update("2", draft => { draft.completed = true })
291 collection.delete("3")
292})
293​
294await tx.isPersisted.promise
295```
296​
297```ts
298// Handle mutate errors
299try {
300 tx.mutate(() => {
301 collection.insert({ id: "invalid" }) // This might throw
302 })
303} catch (error) {
304 console.log('Mutation failed:', error)
306```
307​
308```ts
309// Manual commit control
310const tx = createTransaction({ autoCommit: false, mutationFn: async () => {} })
311​
312tx.mutate(() => {
313 collection.insert({ id: "1", text: "Item" })
314})
315​
316// Commit later when ready
317await tx.commit()
318```
319​
320***
321​
322### rollback()
323​
324```ts
325rollback(config?): Transaction<T>
326```
327​
328Defined in: [packages/db/src/transactions.ts:266](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L266)
329​
330Rollback the transaction and any conflicting transactions
331​
332#### Parameters
333​
334##### config?
335​
336Configuration for rollback behavior
337​
338###### isSecondaryRollback?
339​
340`boolean`
341​
342#### Returns
343​
344[`Transaction`](../transaction.md)\<`T`\>
345​
346This transaction for chaining
347​
348#### Examples
349​
350```ts
351// Manual rollback
352const tx = createTransaction({ mutationFn: async () => {
353 // Send to API
354}})
355​
356tx.mutate(() => {
357 collection.insert({ id: "1", text: "Buy milk" })
358})
359​
360// Rollback if needed
361if (shouldCancel) {
362 tx.rollback()
364```
365​
366```ts
367// Handle rollback cascade (automatic)
368const tx1 = createTransaction({ mutationFn: async () => {} })
369const tx2 = createTransaction({ mutationFn: async () => {} })
370​
371tx1.mutate(() => collection.update("1", draft => { draft.value = "A" }))
372tx2.mutate(() => collection.update("1", draft => { draft.value = "B" })) // Same item
373​
374tx1.rollback() // This will also rollback tx2 due to conflict
375```
376​
377```ts
378// Handle rollback in error scenarios
379try {
380 await tx.isPersisted.promise
381} catch (error) {
382 console.log('Transaction was rolled back:', error)
383 // Transaction automatically rolled back on mutation function failure
385```
386​
387***
388​
389### setState()
390​
391```ts
392setState(newState): void
393```
394​
395Defined in: [packages/db/src/transactions.ts:146](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L146)
396​
397#### Parameters
398​
399##### newState
400​
401[`TransactionState`](../../type-aliases/transactionstate.md)
402​
403#### Returns
404​
405`void`
406​
407***
408​
409### touchCollection()
410​
411```ts
412touchCollection(): void
413```
414​
415Defined in: [packages/db/src/transactions.ts:294](https://github.com/TanStack/db/blob/main/packages/db/src/transactions.ts#L294)
416​
417#### Returns
418​
419`void`