drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Include a way to change the table type for a discriminated union

Open
#5,863 0 comments 0 reactions 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 often do something like this:

```ts
export const accountProviders = p.pgTable(
'account_providers',
{
id: p.integer().primaryKey().generatedAlwaysAsIdentity(),
type: p.text({ enum: AccountProviderTypeValues }).notNull().unique(),
config: p.json().$type(),
},
(t) => [
p.check(
'type_check',
sql`(${t.type} = ${AccountProviderType.local} AND ${t.config} IS NULL)
OR
(${t.type} = ${AccountProviderType.oidc} AND ${t.config} IS NOT NULL)`,
),
],
);

export type AccountProvider =
| Override<
typeof accountProviders.$inferSelect,
{
type: typeof AccountProviderType.local;
config: null;
}
>
| Override<
typeof accountProviders.$inferSelect,
{
type: typeof AccountProviderType.oidc;
config: OidcConfigSchema;
}
>;
```
When `type` = `local`, config must be `null`, and when `type` = `oid`, config must exist. This is also confirmed with `CHECK`.

However, this method doesn't work when I run `db.query.accountProviders.findMany()` or `db.query.accountProviders.findFirst()` ; config is always nullable.

It would be nice if drizzle provided a way to change the schema type, perhaps something like `pgTable()`.

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.