BEGIN ... COMMIT inserted wrong

I'm trying to run this code:
await (db).transaction(async (tx: any) => {
for (let i = 21; i <= 25; i++) {
const r = await tx
.insert(books)
.values({ id: i, title: `tx ${i}`, price: i })
.run()
}
})
await (db).transaction(async (tx: any) => {
for (let i = 21; i <= 25; i++) {
const r = await tx
.insert(books)
.values({ id: i, title: `tx ${i}`, price: i })
.run()
}
})
And it prints the following line to console
BEGIN DEFERRED
insert into "books" ("id", "title", "price") values (21.0, 'tx 21', 21.0)
COMMIT
insert into "books" ("id", "title", "price") values (22.0, 'tx 22', 22.0)
insert into "books" ("id", "title", "price") values (23.0, 'tx 23', 23.0)
insert into "books" ("id", "title", "price") values (24.0, 'tx 24', 24.0)
insert into "books" ("id", "title", "price") values (25.0, 'tx 25', 25.0)
BEGIN DEFERRED
insert into "books" ("id", "title", "price") values (21.0, 'tx 21', 21.0)
COMMIT
insert into "books" ("id", "title", "price") values (22.0, 'tx 22', 22.0)
insert into "books" ("id", "title", "price") values (23.0, 'tx 23', 23.0)
insert into "books" ("id", "title", "price") values (24.0, 'tx 24', 24.0)
insert into "books" ("id", "title", "price") values (25.0, 'tx 25', 25.0)
Why is this? Is it a serious bug with transactions (using better-sqlite3) or just a weirdness of the console log? DB setup is as simple as
const sqlite = new BetterSQLite(dbPath, {
verbose: console.log,
})
sqlite.pragma('journal_mode = WAL')

const db = drizzle({ client: sqlite })
const sqlite = new BetterSQLite(dbPath, {
verbose: console.log,
})
sqlite.pragma('journal_mode = WAL')

const db = drizzle({ client: sqlite })
2 Replies
hyperknot
hyperknotOP4mo ago
Ah, I think I get it, adding the async/await when using a sync driver, like better-sqlite3 is totally corrupting the transaction logic.
hyperknot
hyperknotOP4mo ago
GitHub
Universal transactions for both sync and async drivers · drizzle-t...
I&#39;m trying to write a transaction snippet which works both with sync and async drivers. It took me a while to realize, that: I can write async code and it&#39;ll seem to work with all drivers, ...

Did you find this page helpful?