定义于: packages/db/src/transactions.ts:116
• T extends object = Record<string, unknown>
new Transaction<T>(config): Transaction<T>
new Transaction<T>(config): Transaction<T>
定义于: packages/db/src/transactions.ts:131
Transaction<T>
autoCommit: boolean;
autoCommit: boolean;
定义于: packages/db/src/transactions.ts:122
createdAt: Date;
createdAt: Date;
定义于: packages/db/src/transactions.ts:123
optional error: object;
optional error: object;
定义于: packages/db/src/transactions.ts:126
error: Error;
error: Error;
message: string;
message: string;
id: string;
id: string;
定义于: packages/db/src/transactions.ts:117
isPersisted: Deferred<Transaction<T>>;
isPersisted: Deferred<Transaction<T>>;
定义于: packages/db/src/transactions.ts:121
metadata: Record<string, unknown>;
metadata: Record<string, unknown>;
定义于: packages/db/src/transactions.ts:125
mutationFn: MutationFn<T>;
mutationFn: MutationFn<T>;
定义于: packages/db/src/transactions.ts:119
mutations: PendingMutation<T, OperationType, Collection<T, any, any, any, any>>[];
mutations: PendingMutation<T, OperationType, Collection<T, any, any, any, any>>[];
定义于: packages/db/src/transactions.ts:120
sequenceNumber: number;
sequenceNumber: number;
定义于: packages/db/src/transactions.ts:124
state: TransactionState;
state: TransactionState;
定义于: packages/db/src/transactions.ts:118
applyMutations(mutations): void
applyMutations(mutations): void
定义于: packages/db/src/transactions.ts:212
PendingMutation<any, OperationType, Collection<any, any, any, any, any>>[]
void
commit(): Promise<Transaction<T>>
commit(): Promise<Transaction<T>>
定义于: packages/db/src/transactions.ts:349
提交事务并执行变异函数
Promise<Transaction<T>>
完成时解析为此事务的 Promise
// Manual commit (when autoCommit is false)
const tx = createTransaction({
autoCommit: false,
mutationFn: async ({ transaction }) => {
await api.saveChanges(transaction.mutations)
}
})
tx.mutate(() => {
collection.insert({ id: "1", text: "Buy milk" })
})
await tx.commit() // Manually commit
// Manual commit (when autoCommit is false)
const tx = createTransaction({
autoCommit: false,
mutationFn: async ({ transaction }) => {
await api.saveChanges(transaction.mutations)
}
})
tx.mutate(() => {
collection.insert({ id: "1", text: "Buy milk" })
})
await tx.commit() // Manually commit
// Handle commit errors
try {
const tx = createTransaction({
mutationFn: async () => { throw new Error("API failed") }
})
tx.mutate(() => {
collection.insert({ id: "1", text: "Item" })
})
await tx.commit()
} catch (error) {
console.log('Commit failed, transaction rolled back:', error)
}
// Handle commit errors
try {
const tx = createTransaction({
mutationFn: async () => { throw new Error("API failed") }
})
tx.mutate(() => {
collection.insert({ id: "1", text: "Item" })
})
await tx.commit()
} catch (error) {
console.log('Commit failed, transaction rolled back:', error)
}
// Check transaction state after commit
await tx.commit()
console.log(tx.state) // "completed" or "failed"
// Check transaction state after commit
await tx.commit()
console.log(tx.state) // "completed" or "failed"
compareCreatedAt(other): number
compareCreatedAt(other): number
定义于: packages/db/src/transactions.ts:395
按创建时间对其进行排序,以实现它们在创建时的顺序
Transaction<any>
要与之比较的另一个事务
number
-1 如果此事务在其他事务之前创建,1 如果在此事务之后创建,0 如果它们在同一时间创建
mutate(callback): Transaction<T>
mutate(callback): Transaction<T>
定义于: packages/db/src/transactions.ts:193
在此事务内执行集合操作
() => void
包含要分组的集合操作的函数
Transaction<T>
此事务用于链接
// Group multiple operations
const tx = createTransaction({ mutationFn: async () => {
// Send to API
}})
tx.mutate(() => {
collection.insert({ id: "1", text: "Buy milk" })
collection.update("2", draft => { draft.completed = true })
collection.delete("3")
})
await tx.isPersisted.promise
// Group multiple operations
const tx = createTransaction({ mutationFn: async () => {
// Send to API
}})
tx.mutate(() => {
collection.insert({ id: "1", text: "Buy milk" })
collection.update("2", draft => { draft.completed = true })
collection.delete("3")
})
await tx.isPersisted.promise
// Handle mutate errors
try {
tx.mutate(() => {
collection.insert({ id: "invalid" }) // This might throw
})
} catch (error) {
console.log('Mutation failed:', error)
}
// Handle mutate errors
try {
tx.mutate(() => {
collection.insert({ id: "invalid" }) // This might throw
})
} catch (error) {
console.log('Mutation failed:', error)
}
// Manual commit control
const tx = createTransaction({ autoCommit: false, mutationFn: async () => {} })
tx.mutate(() => {
collection.insert({ id: "1", text: "Item" })
})
// Commit later when ready
await tx.commit()
// Manual commit control
const tx = createTransaction({ autoCommit: false, mutationFn: async () => {} })
tx.mutate(() => {
collection.insert({ id: "1", text: "Item" })
})
// Commit later when ready
await tx.commit()
rollback(config?): Transaction<T>
rollback(config?): Transaction<T>
定义于: packages/db/src/transactions.ts:266
回滚事务和任何冲突的事务
回滚行为的配置
boolean
Transaction<T>
此事务用于链接
// Manual rollback
const tx = createTransaction({ mutationFn: async () => {
// Send to API
}})
tx.mutate(() => {
collection.insert({ id: "1", text: "Buy milk" })
})
// Rollback if needed
if (shouldCancel) {
tx.rollback()
}
// Manual rollback
const tx = createTransaction({ mutationFn: async () => {
// Send to API
}})
tx.mutate(() => {
collection.insert({ id: "1", text: "Buy milk" })
})
// Rollback if needed
if (shouldCancel) {
tx.rollback()
}
// Handle rollback cascade (automatic)
const tx1 = createTransaction({ mutationFn: async () => {} })
const tx2 = createTransaction({ mutationFn: async () => {} })
tx1.mutate(() => collection.update("1", draft => { draft.value = "A" }))
tx2.mutate(() => collection.update("1", draft => { draft.value = "B" })) // Same item
tx1.rollback() // This will also rollback tx2 due to conflict
// Handle rollback cascade (automatic)
const tx1 = createTransaction({ mutationFn: async () => {} })
const tx2 = createTransaction({ mutationFn: async () => {} })
tx1.mutate(() => collection.update("1", draft => { draft.value = "A" }))
tx2.mutate(() => collection.update("1", draft => { draft.value = "B" })) // Same item
tx1.rollback() // This will also rollback tx2 due to conflict
// Handle rollback in error scenarios
try {
await tx.isPersisted.promise
} catch (error) {
console.log('Transaction was rolled back:', error)
// Transaction automatically rolled back on mutation function failure
}
// Handle rollback in error scenarios
try {
await tx.isPersisted.promise
} catch (error) {
console.log('Transaction was rolled back:', error)
// Transaction automatically rolled back on mutation function failure
}
setState(newState): void
setState(newState): void
定义于: packages/db/src/transactions.ts:146
void
touchCollection(): void
touchCollection(): void
定义于: packages/db/src/transactions.ts:294
void
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。