drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: discriminator column and custom type
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Describe what you want
Hi,
I have a use case similar to the following one, I have a discriminator column that influence the content of a json field
```typescript
export const Section = mysqlTable(
'BillingDetail',
{
id: varchar('id', 200)
.primaryKey()
.notNull(),
kind: varchar('kind', {
length: DEFAULT_VARCHAR_LENGTH,
enum: [
'creditCard',
'bankAccount'
],
}).notNull(),
data: customType<{
data: BillingData;
}>({
toDriver: value => JSON.stringify(value),
fromDriver: value => value as BillingData,
dataType: () => 'json',
})('data').notNull(),
type BillingData = {
expiryMonth?: number;
expiryYear?: number;
cardType?: string;
bankName?: string;
swift?: string;
}
```
the type `BillingData` contains all the field that can be contained in `data`
I would like to have something like
```typescript
const BillingDetail = { id: string; } &
(
{ kind: 'creditCard'; data: { expiryMonth: number; expiryYear: number; cardType: string} }
|
{ kind: 'bankAccount'; data: { bankName: string; swift: string} }
)
```
as result.
But I can’t use my discriminator column (`kind`) in my `customType` to specify more precisely the data
It would be great to have a way to refine types based on other columns (notably for json columns), to be able define a schema as a union
Contributor guide
Assessment
This issue has not been assessed yet.