Skip to content

Commit

Permalink
fix(builder): fix transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
subframe7536 committed Nov 8, 2023
1 parent b671c24 commit 7f1a675
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
9 changes: 4 additions & 5 deletions packages/sqlite-builder/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
}

private getDB() {
return this.trxCount ? this.trx! : this.kysely
return this.trx || this.kysely
}

private logError(e: unknown, errorMsg?: string) {
Expand Down Expand Up @@ -161,12 +161,11 @@ export class SqliteBuilder<DB extends Record<string, any>> {
}
}
this.trxCount++
const _db = this.getDB()
const sp = await savePoint(_db, `sp_${this.trxCount}`)
this.logger?.debug(`run in savepoint:${this.trxCount}`)
const sp = await savePoint(this.trx, `sp_${this.trxCount}`)
this.logger?.debug(`run in savepoint: sp_${this.trxCount}`)

try {
const result = await fn(_db as Transaction<DB>)
const result = await fn(this.trx)
await sp.release()
this.trxCount--
return result
Expand Down
6 changes: 5 additions & 1 deletion packages/sqlite-utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ SQLite utils for [kysely](https://github.com/kysely-org/kysely)

## features

- nest transaction support (using `savepoint`)
- nest transaction support (using `savePoint`)
- check integrity (`integrity_check` pragma)
- precompile querys for performance
- get or set db version (`user_version` pragma)
- optimize pragma (typesafe `journal_mode`, `synchoronous`...)
- create kysely logger

## credit

Expand Down
8 changes: 5 additions & 3 deletions playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ function testWaSqliteWorker() {
useWaSqliteWorker()
}
async function deleteDatabase() {
const dbs = await window.indexedDB.databases()
dbs.forEach((db) => { window.indexedDB.deleteDatabase(db.name!) })
try {
const dbs = await window.indexedDB.databases()
dbs.forEach(db => window.indexedDB.deleteDatabase(db.name!))
} catch { }
}
async function clear() {
// console.clear()
await deleteFile('sqljs')
await deleteFile('sqlijsWorker')
await deleteFile('sqljsWorker')
await deleteDatabase()
const root = await navigator.storage?.getDirectory()
try {
Expand Down
8 changes: 4 additions & 4 deletions playground/src/modules/sqljsWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ const dialect = new SqlJsDialect({
// locateFile: file => `https://sql.js.org/dist/${file}`,
locateFile: () => WasmUrl,
})
return new SQL.Database(await loadFile('sqlijsWorker'))
return new SQL.Database(await loadFile('sqljsWorker'))
},
onWrite: {
func(data) {
console.log(`[sqljs worker] write to indexeddb, length: ${data.length}`)
writeFile('sqlijsWorker', data)
writeFile('sqljsWorker', data)
},
},
})
onmessage = () => {
console.log('start sqljs test')
console.log('start sqljs worker test')
testDB(dialect).then((data) => {
data?.forEach(e => console.log('[sqljs]', e))
data?.forEach(e => console.log('[sqljs Worker]', e))
})
}
22 changes: 13 additions & 9 deletions playground/src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ export async function testDB(dialect: Dialect) {

for (let i = 0; i < 10; i++) {
await db.transaction(async () => {
// await db.transaction(async (trx) => {
await db.execute(d => d
.insertInto('test')
.values({
name: `test at ${Date.now()}`,
blobtest: Uint8Array.from([2, 3, 4, 5, 6, 7, 8]),
}),
)
// })
await db.transaction(async () => {
if (i > 8) {
console.log('test rollback')
throw new Error('test rollback')
}
await db.execute(d => d
.insertInto('test')
.values({
name: `test at ${Date.now()}`,
blobtest: Uint8Array.from([2, 3, 4, 5, 6, 7, 8]),
}),
)
})
})
}

Expand Down
11 changes: 9 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f1a675

Please sign in to comment.