Skip to content

Commit 2636202

Browse files
committed
ensure that all transactions are given an id
1 parent 836c46e commit 2636202

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

.changeset/open-foxes-say.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/db": patch
3+
---
4+
5+
Ensure that all transactions are given an id, fixes a potential bug with direct mutations

packages/db/src/transactions.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,8 @@ let transactionStack: Array<Transaction<any>> = []
1515
export function createTransaction<
1616
TData extends object = Record<string, unknown>,
1717
>(config: TransactionConfig<TData>): Transaction<TData> {
18-
if (typeof config.mutationFn === `undefined`) {
19-
throw `mutationFn is required when creating a transaction`
20-
}
21-
22-
let transactionId = config.id
23-
if (!transactionId) {
24-
transactionId = crypto.randomUUID()
25-
}
26-
const newTransaction = new Transaction<TData>({
27-
...config,
28-
id: transactionId,
29-
})
30-
18+
const newTransaction = new Transaction<TData>(config)
3119
transactions.push(newTransaction)
32-
3320
return newTransaction
3421
}
3522

@@ -74,7 +61,10 @@ export class Transaction<
7461
}
7562

7663
constructor(config: TransactionConfig<T>) {
77-
this.id = config.id!
64+
if (typeof config.mutationFn === `undefined`) {
65+
throw `mutationFn is required when creating a transaction`
66+
}
67+
this.id = config.id ?? crypto.randomUUID()
7868
this.mutationFn = config.mutationFn
7969
this.state = `pending`
8070
this.mutations = []

0 commit comments

Comments
 (0)