RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
690 stars 62 forks source link

Add --singular option, close 32 #162

Open acro5piano opened 2 weeks ago

acro5piano commented 2 weeks ago

Thank you for the fantastic tool!

As discussed on https://github.com/RobinBlomberg/kysely-codegen/issues/32, I've added option --singular.

Examples

Given the schema:

CREATE TYPE user_status as ENUM ('ACTIVE', 'INACTIVE'); 

CREATE TABLE users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
  status user_status NOT NULL
);

Run with --singular

pnpm kysely-codegen --singular

generates:

import type { ColumnType } from "kysely";

type UserStatus = 'ACTIVE' | 'INACTIVE'

export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
  ? ColumnType<S, I | undefined, U>
  : ColumnType<T, T | undefined, T>;

export interface User {
  id: Generated<string>;
  status: UserStatus;
}

export interface Database {
  users: User;
}

NOTE: It does not touch enum names and values.