Skip to content

Commit 7f1a675

Browse files
committed
fix(builder): fix transaction
1 parent b671c24 commit 7f1a675

File tree

6 files changed

+40
-24
lines changed

6 files changed

+40
-24
lines changed

packages/sqlite-builder/src/builder.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class SqliteBuilder<DB extends Record<string, any>> {
130130
}
131131

132132
private getDB() {
133-
return this.trxCount ? this.trx! : this.kysely
133+
return this.trx || this.kysely
134134
}
135135

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

168167
try {
169-
const result = await fn(_db as Transaction<DB>)
168+
const result = await fn(this.trx)
170169
await sp.release()
171170
this.trxCount--
172171
return result

packages/sqlite-utils/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ SQLite utils for [kysely](https://github.com/kysely-org/kysely)
44

55
## features
66

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

1014
## credit
1115

playground/src/App.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ function testWaSqliteWorker() {
2727
useWaSqliteWorker()
2828
}
2929
async function deleteDatabase() {
30-
const dbs = await window.indexedDB.databases()
31-
dbs.forEach((db) => { window.indexedDB.deleteDatabase(db.name!) })
30+
try {
31+
const dbs = await window.indexedDB.databases()
32+
dbs.forEach(db => window.indexedDB.deleteDatabase(db.name!))
33+
} catch { }
3234
}
3335
async function clear() {
3436
// console.clear()
3537
await deleteFile('sqljs')
36-
await deleteFile('sqlijsWorker')
38+
await deleteFile('sqljsWorker')
3739
await deleteDatabase()
3840
const root = await navigator.storage?.getDirectory()
3941
try {

playground/src/modules/sqljsWorker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ const dialect = new SqlJsDialect({
1010
// locateFile: file => `https://sql.js.org/dist/${file}`,
1111
locateFile: () => WasmUrl,
1212
})
13-
return new SQL.Database(await loadFile('sqlijsWorker'))
13+
return new SQL.Database(await loadFile('sqljsWorker'))
1414
},
1515
onWrite: {
1616
func(data) {
1717
console.log(`[sqljs worker] write to indexeddb, length: ${data.length}`)
18-
writeFile('sqlijsWorker', data)
18+
writeFile('sqljsWorker', data)
1919
},
2020
},
2121
})
2222
onmessage = () => {
23-
console.log('start sqljs test')
23+
console.log('start sqljs worker test')
2424
testDB(dialect).then((data) => {
25-
data?.forEach(e => console.log('[sqljs]', e))
25+
data?.forEach(e => console.log('[sqljs Worker]', e))
2626
})
2727
}

playground/src/modules/utils.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ export async function testDB(dialect: Dialect) {
2525

2626
for (let i = 0; i < 10; i++) {
2727
await db.transaction(async () => {
28-
// await db.transaction(async (trx) => {
29-
await db.execute(d => d
30-
.insertInto('test')
31-
.values({
32-
name: `test at ${Date.now()}`,
33-
blobtest: Uint8Array.from([2, 3, 4, 5, 6, 7, 8]),
34-
}),
35-
)
36-
// })
28+
await db.transaction(async () => {
29+
if (i > 8) {
30+
console.log('test rollback')
31+
throw new Error('test rollback')
32+
}
33+
await db.execute(d => d
34+
.insertInto('test')
35+
.values({
36+
name: `test at ${Date.now()}`,
37+
blobtest: Uint8Array.from([2, 3, 4, 5, 6, 7, 8]),
38+
}),
39+
)
40+
})
3741
})
3842
}
3943

pnpm-lock.yaml

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)