how to get generic table type

I tried to use AnyPgTable type to make dynamic function
async findById(id: number, table: AnyPgTable) {
const res = await this.db.select().from(table).where(eq(table.id, id));

return res.at(0);
}
async findById(id: number, table: AnyPgTable) {
const res = await this.db.select().from(table).where(eq(table.id, id));

return res.at(0);
}
typescript: Property 'id' does not exist on type 'AnyPgTable'. [2339]
typescript: Property 'id' does not exist on type 'AnyPgTable'. [2339]
but i need to have id type all tables have serial id column
id: serial('id').primaryKey(),
id: serial('id').primaryKey(),
1 Reply
TJBlackman
TJBlackman10mo ago
This might help you https://orm.drizzle.team/docs/goodies Also, you might have to pass in the expected return type as a generic: const result = await findById<UserTableType[]>(id, usersTable); imo, your function is too generic you should just make getById functions for each of your tables. getUserById getPostById etc..
Drizzle ORM - Goodies
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Want results from more Discord servers?
Add your server