Confused by Kysely

I'm using kysely for a project and I'm using it to create tables, however now that I create the tables with the columns and behaviors I want, I need to essentially do the same by writing the typescript type, which seems bad. Am I missing something or is that really how I'm expected to do this? for example here is a users table
const createUserTable = async () => {
await db.schema
.createTable("users")
.addColumn("id", "integer", (cb) =>
cb.primaryKey().autoIncrement().notNull()
)
.addColumn("email", "varchar(255)", (cb) => cb.notNull().unique())
.addColumn("password", "varchar(255)", (cb) => cb.notNull())
.addColumn("is_admin", "boolean", (cb) => cb.notNull().defaultTo(false))
.addColumn("first_name", "varchar(255)", (cb) => cb.notNull())
.addColumn("last_name", "varchar(255)")
.addColumn("created_at", "timestamp", (cb) =>
cb.notNull().defaultTo(sql`current_timestamp`)
)
.addColumn("last_login", "timestamp")
.execute();
};
const createUserTable = async () => {
await db.schema
.createTable("users")
.addColumn("id", "integer", (cb) =>
cb.primaryKey().autoIncrement().notNull()
)
.addColumn("email", "varchar(255)", (cb) => cb.notNull().unique())
.addColumn("password", "varchar(255)", (cb) => cb.notNull())
.addColumn("is_admin", "boolean", (cb) => cb.notNull().defaultTo(false))
.addColumn("first_name", "varchar(255)", (cb) => cb.notNull())
.addColumn("last_name", "varchar(255)")
.addColumn("created_at", "timestamp", (cb) =>
cb.notNull().defaultTo(sql`current_timestamp`)
)
.addColumn("last_login", "timestamp")
.execute();
};
Now if I want this to have typescript types I have to write this as well
export interface UsersTable {

id: Generated<number>;
email: string;
password: string;

is_admin: Generated<0 | 1>;
first_name: string;
last_name: string | null;

created_at: ColumnType<Date, string | undefined, never>;
last_login: Date | null;
}

export type User = Selectable<UsersTable>;
export type NewUser = Insertable<UsersTable>;
export type UserUpdate = Updateable<UsersTable>;
export interface UsersTable {

id: Generated<number>;
email: string;
password: string;

is_admin: Generated<0 | 1>;
first_name: string;
last_name: string | null;

created_at: ColumnType<Date, string | undefined, never>;
last_login: Date | null;
}

export type User = Selectable<UsersTable>;
export type NewUser = Insertable<UsersTable>;
export type UserUpdate = Updateable<UsersTable>;
I would think that the library should be able to auto generate this type from all the information I give it when I create the damn table
4 Replies
ErickO
ErickO•7mo ago
🤢 absolutely vile yes, you still need to define the types typescript types are static, the sucky code from kysely's migration is run when the code runs, therefore it has no previous knowledge of the types you set, you'd need to have a script that can transform that info to actual types but the end result is the same, additional TS files for the db info
Shane
ShaneOP•7mo ago
yikes will switching to drizzle solve this issue?
ErickO
ErickO•7mo ago
I do not know tbh
Shane
ShaneOP•7mo ago
for now I'm going to codegen and just deal with codegening after running migrations every time
Want results from more Discord servers?
Add your server