drizzle-team / drizzle-team/drizzle-orm

[BUG]: sqlite transactions can't be async for 4 of 5 implementations

Open
#2,275 16 comments 38 reactions 1 assignee Claimed by @AndriiSherman View on GitHub
bug db/sqlite priority qb/transactions
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### What version of `drizzle-orm` are you using?

0.30.9

### What version of `drizzle-kit` are you using?

0.20.17

### Describe the Bug

The following async transaction doesn't work for any of the sqlite implementation (except sqlite-proxy)

```ts
await db.transaction(async tx => {
await tx.delete(schema.items)

if (items.length >= 1) {
await tx.insert(schema.items)
.values(items)
.onConflictDoNothing()
}
})
```

Logger will print something like this:

```
begin
delete from "items"
commit
insert into "items" values (...)
```

### Expected behavior

Async transactions (like in the [sqlite documentation](https://orm.drizzle.team/docs/transactions))

```ts
await db.transaction(async tx => {
await tx.delete(schema.items)

if (items.length >= 1) {
await tx.insert(schema.items)
.values(items)
.onConflictDoNothing()
}
})
```

should work, like sync transactions

```ts
db.transaction(tx => {
tx.delete(schema.items).run()

if (items.length >= 1) {
tx.insert(schema.items)
.values(items)
.onConflictDoNothing()
.run()
}
})
```

, but the transaction callbacks are not `await`ed, except for sqlite-proxy.

- wrong: https://github.com/drizzle-team/drizzle-orm/blob/10d22305b062dd0f4b0ff1594a6ae722487b7886/drizzle-orm/src/expo-sqlite/session.ts#L69
- wrong: https://github.com/drizzle-team/drizzle-orm/blob/10d22305b062dd0f4b0ff1594a6ae722487b7886/drizzle-orm/src/better-sqlite3/session.ts#L83
- wrong: https://github.com/drizzle-team/drizzle-orm/blob/10d22305b062dd0f4b0ff1594a6ae722487b7886/drizzle-orm/src/bun-sqlite/session.ts#L93
- wrong: https://github.com/drizzle-team/drizzle-orm/blob/10d22305b062dd0f4b0ff1594a6ae722487b7886/drizzle-orm/src/op-sqlite/session.ts#L68
- good: https://github.com/drizzle-team/drizzle-orm/blob/10d22305b062dd0f4b0ff1594a6ae722487b7886/drizzle-orm/src/sqlite-proxy/session.ts#L85

### Environment & setup

- expo-sqlite
- better-sqlite3
- bun-sqlite
- op-sqlite

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.