Use schema definition as normal types

I want to use my schema definition as a normal type. Basically what .select().from() returns - plain object with the type. I need it for something like this: const result = (await fetch("http://127.0.0.1:8000")).json() as Promise<typeof my_schema[]>; I could of course define a type with the same schema as my db schema but then I have to update that whenever I update my schema, is there a better way?
2 Replies
Kasper
Kasper4mo ago
Do you mean like this?
// schema.ts

export const usersTable = mysqlTable('users', {
id: bigint('id', { mode: 'number', unsigned: true }).primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
verified: boolean('verified').notNull().default(false),
});

export type User = typeof usersTable.$inferSelect;
export type UserInsert = typeof usersTable.$inferInsert;
// schema.ts

export const usersTable = mysqlTable('users', {
id: bigint('id', { mode: 'number', unsigned: true }).primaryKey(),
name: varchar('name', { length: 256 }).notNull(),
verified: boolean('verified').notNull().default(false),
});

export type User = typeof usersTable.$inferSelect;
export type UserInsert = typeof usersTable.$inferInsert;
TAINCER
TAINCER3mo ago
Yes, thanks alot!