drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: native CASE WHEN support
- 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
Assessment
This issue has not been assessed yet.