RobinBlomberg / kysely-codegen

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

Incorrect timestamp types #209

Closed nisuxyz closed 1 month ago

nisuxyz commented 1 month ago

Based on the documentation, I would expect a database that I migrated and seeded with:

// user_migration.ts
import { sql, type Kysely } from 'kysely'

export async function up(db: Kysely<any>): Promise<void> {
    // up migration code goes here...
    // note: up migrations are mandatory. you must implement this function.
    // For more info, see: https://kysely.dev/docs/migrations
    await db.schema.createTable('users')
        .addColumn('id', 'integer', (cb) => cb.primaryKey().autoIncrement().notNull())
        .addColumn('created_at', 'timestamp', (cb) => cb.notNull().defaultTo(sql`current_timestamp`))
        .addColumn('email', 'varchar(255)', (cb) => cb.notNull())
        .addColumn('username', 'varchar(255)', (cb) => cb.notNull())
        .addColumn('password', 'varchar(255)', (cb) => cb.notNull())
        .execute();
}

export async function down(db: Kysely<any>): Promise<void> {
    // down migration code goes here...
    // note: down migrations are optional. you can safely delete this function.
    // For more info, see: https://kysely.dev/docs/migrations
    await db.schema.dropTable('users').execute();
}
// user_seed.ts
import type { Kysely } from 'kysely';

const users = [
    {
        email: 'test@test.com',
        username: 'test',
        password: Bun.password.hashSync('asdfasdf')
    }
];

export async function seed(db: Kysely<any>): Promise<void> {
    // seed code goes here...
    // note: this function is mandatory. you must implement this function.
    await db.insertInto('users').values(users).execute();
}

To generate a type of Generated<Timestamp> for the created_at column. However I get Generated<string>:

export interface Users {
  created_at: Generated<string>;
  email: string;
  id: Generated<number>;
  password: string;
  username: string;
}

This is preventing me from using the generated types with the serialize plugin, which would allow me to do comparisons on "timestamp" tables by passing date objects like so:

await db.selectFrom('users')
  .where('created_at', '<=', new Date())
  .selectAll().execute();

I'm using the worker-bun-sqlite dialect when generating (this is what I have configured as my dialect for kysely: (kysely-bun-worker)[https://github.com/subframe7536/kysely-sqlite-tools/tree/master/packages/dialect-bun-worker])

Thank you for creating this utility!

Upvote & Fund

Fund with Polar

michael-land commented 1 month ago

@RobinBlomberg this seems already addressed in https://github.com/RobinBlomberg/kysely-codegen/commit/ad03d2ef67f40c7d22df27e30a16bcf6083da463

can we get a new release?

RobinBlomberg commented 1 month ago

Should be fixed now in kysely-codegen@0.17.0!