Interface for both db and tx

I am trying to create some utility functions that would accept both DB and transaction. I tried the following
export type DBRelation = ExtractTablesWithRelations<typeof schema>;
export type DBTransaction = PgTransaction<PostgresJsQueryResultHKT, typeof schema, DbRelation>;
export type DBRelation = ExtractTablesWithRelations<typeof schema>;
export type DBTransaction = PgTransaction<PostgresJsQueryResultHKT, typeof schema, DbRelation>;
trying to pass my db type to it results in error (passing tx works) here is how my query function looks like
type TokenUser = Token & { user: Omit<User, "passwordHash"> };
export async function getToken<DB extends DBTransaction>(tx: DB, token: string, tokenType: TokenType): Promise<TokenUser | undefined> {
return await tx.query.tokens.findFirst({
where: and(
eq(tokens.token, token),
isNull(tokens.disabledAt),
gt(tokens.expiresAt, new Date()),
eq(tokens.tokenType, tokenType)
),
with: {
user: {
columns: {
passwordHash: false,
}
}
}
});
}
type TokenUser = Token & { user: Omit<User, "passwordHash"> };
export async function getToken<DB extends DBTransaction>(tx: DB, token: string, tokenType: TokenType): Promise<TokenUser | undefined> {
return await tx.query.tokens.findFirst({
where: and(
eq(tokens.token, token),
isNull(tokens.disabledAt),
gt(tokens.expiresAt, new Date()),
eq(tokens.tokenType, tokenType)
),
with: {
user: {
columns: {
passwordHash: false,
}
}
}
});
}
how can i fix this
1 Reply
Foxikira
FoxikiraOP11mo ago
I found the solution I also created a type PostgresJsDatabase<typeof schema> and made the extends blok or block
Want results from more Discord servers?
Add your server