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>;

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,
        }
      }
    }
  });
}

how can i fix this
Was this page helpful?