How to type a table with common column names ?

Hey, I have a couple tables that have some common column names, ex.
export const appointments = coreSchema.table(
"appointments",
{
id: primaryKey(),
accountId: foreignKey("account_id")
.references(() => accounts.id)
.notNull(),
}
);

export const clients = coreSchema.table(
"clients",
{
id: primaryKey(),
accountId: foreignKey("account_id")
.references(() => accounts.id)
.notNull(),
}
)
export const appointments = coreSchema.table(
"appointments",
{
id: primaryKey(),
accountId: foreignKey("account_id")
.references(() => accounts.id)
.notNull(),
}
);

export const clients = coreSchema.table(
"clients",
{
id: primaryKey(),
accountId: foreignKey("account_id")
.references(() => accounts.id)
.notNull(),
}
)
I want to be able to write a shared function that can be used to build queries with the account_id where condition already added to it, something like this
export const getQuery = (table: Table, accountId: string) => {
const qb = new QueryBuilder();
return qb.select().from(table).where(eq(table.accountId, accountId));
};
export const getQuery = (table: Table, accountId: string) => {
const qb = new QueryBuilder();
return qb.select().from(table).where(eq(table.accountId, accountId));
};
How should the type Table be defined here? I've tried something like this
interface Table extends PgTable {
accountId: PgColumn;
}
interface Table extends PgTable {
accountId: PgColumn;
}
Is this a good/acceptable way to do it, or is there a more built-in drizzle type that can cover such scenarios?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?