drizzle-team / drizzle-team/drizzle-orm

[FEATURE]:custom (narrower) type for a table

Open
#4,846 4 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [x] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

I have a table like this :
```ts
const someTable = pgTable({
id:uuid().notNull().primaryKey(),
userId:uuid(),
categoryId:uuid(),
})
```
it should contain on and only one of userId and categoryId based on the business logic,
we can make the code work ( and type safe by putting checks everywhere or ( using ! operator for typescript in order to guarantee the value is there ) but that's not ideal.

something like this would solve the issue :
```ts
const someTable = pgTable<{id:string} &({userId:string} | {categoryId:string})>({
id:uuid().notNull().primaryKey(),
userId:uuid(),
categoryId:uuid(),
})
```
this could also be useful for different applications as well ( for example narrowing type of one column based on another column's type )

it is possible to pass a type for both select and insert if necessary but the idea of being able to narrow down the type is useful IMO.

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.