RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
819 stars 73 forks source link

Add --singular option, close 32 #162

Closed acro5piano closed 4 months ago

acro5piano commented 5 months 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.

RobinBlomberg commented 4 months ago

Amazing! Thanks! 🙌