함수: createTransaction()
function createTransaction<T>(config): Transaction<T>;
정의 위치: packages/db/src/transactions.ts:283
여러 컬렉션 작업을 그룹화하기 위한 새 트랜잭션을 생성합니다
타입 매개변수
T
T 확장 object = Record<string, unknown>
매개변수
config
TransactionConfig<T>
뮤테이션 함수를 포함하는 트랜잭션 구성입니다
반환값
Transaction<T>
새 Transaction 인스턴스입니다
예제
// Basic transaction usage
const tx = createTransaction({
mutationFn: async ({ transaction }) => {
// Send all mutations to API
await api.saveChanges(transaction.mutations)
}
})
tx.mutate(() => {
collection.insert({ id: "1", text: "Buy milk" })
collection.update("2", draft => { draft.completed = true })
})
await tx.isPersisted.promise
// Handle transaction errors
try {
const tx = createTransaction({
mutationFn: async () => { throw new Error("API failed") }
})
tx.mutate(() => {
collection.insert({ id: "1", text: "New item" })
})
await tx.isPersisted.promise
} catch (error) {
console.log('Transaction failed:', error)
}
// Manual commit control
const tx = createTransaction({
autoCommit: false,
mutationFn: async () => {
// API call
}
})
tx.mutate(() => {
collection.insert({ id: "1", text: "Item" })
})
// Commit later
await tx.commit()