drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Add `isUnique` to Column Config types
- 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
Currently, there is no way for TypeScript to know if a column is unique, in the same way it is possible to infer if a column is a primary key, or not null, or has default etc.
I am trying to create some Types in my project that will allow me to pass the schema for some tables into a type, and infer the columns that are primary keys or are unique.
These are the types I have currently:
```ts
/** ExtractTableConfig
*
* Extract the type of the `TableConfig` of a given table.
*/
export type ExtractTableConfig = Table extends PgTableWithColumns<
infer TableConfig
> ? TableConfig : never
/** ExtractColumnConfig
*
* Extract the type of the:
*
* - `ColumnBaseConfig`
* - `RuntimeConfig`
* - `TypeConfig`
*
* of a given column.
*/
export type ExtractColumnConfig = Column extends PgColumn<
infer ColumnBaseConfig,
infer RuntimeConfig,
infer TypeConfig
> ? {
"ColumnBaseConfig": ColumnBaseConfig,
"RuntimeConfig": RuntimeConfig,
"TypeConfig": TypeConfig
} : never
/** ExtractTableColumnConfigs
*
* Extract the `ColumnBaseConfig` of each column in a given table.
*/
export type ExtractTableColumnConfigs> = {
[Key in keyof ExtractTableConfig["columns"]]:
ExtractColumnConfig<
ExtractTableConfig["columns"][Key]
>["ColumnBaseConfig"]
}
/** ColumnsAreUnique
*
* Get which columns are unique in a given table.
*
* Includes columns created with `.primaryKey()` or `.unique()`.
*/
export type ColumnsAreUnique> = {
[Key in keyof ExtractTableColumnConfigs]:
ExtractTableColumnConfigs[Key]["isPrimaryKey"] extends true
? true
: ExtractTableColumnConfigs[Key]["isUnique"] extends true
? true
: false
}
import tables from "./schemas/index"
type UCs = ColumnsAreUnique
```
The type of `UCs` comes back as:
```ts
type UCs = {
id: true;
userId: false;
address: false;
verified: false;
verificationCode: false;
codeSentAt: false;
}
```
instead of:
```ts
type UCs = {
id: true;
userId: true;
address: true;
verified: false;
verificationCode: true;
codeSentAt: false;
}
```
Because this is what is really being evaluated:
```ts
export type ColumnsAreUnique> = {
[Key in keyof ExtractTableColumnConfigs]:
ExtractTableColumnConfigs[Key]["isPrimaryKey"] extends true
? true
// : ExtractTableColumnConfigs[Key]["isUnique"] extends true
// ? true
// WARNING: "isUnique" is not a property yet, so only PKs are included
: false
}
```
As far as I am aware, there is currently no way to reproduce the behaviour I am looking for, adding an `isUnique` property would allow these types to work as intended, and allow proper type safety for some complex queries I have developed.
I have tracked down the class that similar properties have been implemented to [here](https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/column-builder.ts#L55), for anyone attempting to add this feature, this is where I would start, but I am not ashamed to say, this is beyond my ability.
Contributor guide
Assessment
This issue has not been assessed yet.