drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: enum for integer columns

Open
#4,138 0 comments 5 reactions 0 assignees View on GitHub
db/mysql db/postgres 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

Since enum types don't check runtime values, wouldn't it be possible to narrow `integer` columns as well?

> You can define `{ enum: ["value1", "value2"] }` config to infer `insert` and `select` types, it won’t check runtime values.

```ts
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';

export const table = sqliteTable('table', {
textEnum: text({ enum: ['1', '2'] }).notNull(),
intEnum: integer({ enum: [1, 2] }).notNull(), // suggestion - does not work
jsonEnum: text().$type<1 | 2>().notNull(), // workaround - but it's stored as a string
});

// {
// textEnum: '1' | '2';
// intEnum: number;
// jsonEnum: 2 | 1;
// };
type Enums = typeof table.$inferSelect;
```

I personally love Valibot's [`picklist`](https://valibot.dev/api/picklist/) API because it supports both string and number.

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.