drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: native CASE WHEN support

Open
#1,065 7 comments 52 reactions 0 assignees View on GitHub
db/mysql db/postgres db/sqlite enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

Using `case` in queries can be a really nice way to restructure data, or omit things entirely if unset. https://www.postgresql.org/docs/current/functions-conditional.html

It's also very useful in `orderBy` queries, such as like this:

```javascript
sql`
case
when ${table.scope} = 'a' then 1
when ${table.scope} = 'b' then 2
when ${table.scope} = 'c' then 3
when ${table.scope} = 'd' then 4
end
`,
```

Or when querying from multiple tables but wanting to conditionally change where data is coming from:
```javascript
sql`
case
when ${table.type} = 'a' then ${someTable.name}
when ${table.type} = 'b' then ${otherTable.title}
end
`,
```

This is fine, but could be more ergonomic I think with a native helper.

Psuedo-example without much thought of what this could look like:
```javascript
case(
eq(table.type, 'a'), someTable.name,
eq(table.type, 'b'), otherTable.title,
)
```

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.