Drizzle TeamDT
Drizzle Teamโ€ข2y ago
n00ki

Properly inferring Prepared Statement Return Type when executing a query

Hi everyone ๐Ÿ™‚

I'm working on a SvelteKit project with Drizzle ORM and i'm trying to extract some of the queries to prepared statements.
There's probably something i'm doing wrong, yet when i execute the prepared statement in my codebase - i'm loosing the inferred model return type.
Meaning, const data = await ps.execute() | ps.all() | ps.execute({ ph: 'ph'}) return type is unknown.

I'm declaring and exporting the prepared statements like this:
// SELECT * FROM accounts
export const getAccounts: SQLitePreparedQuery<PreparedQueryConfig> = db.select().from(Account).prepare();
// OR
// SELECT * FROM accounts WHERE id = ?
export const getAccountById: SQLitePreparedQuery<PreparedQueryConfig> = db.query.Account.findFirst({
  where: eq(Account.id, sql.placeholder('id'))
}).prepare();


then importing and using them in my application like this:
const getAllAccounts = await getAccounts.all();
const getAccount = await getAccountById.execute({ id: "1" })


and i get 'getAllAccounts' is of type 'unknown' | 'getAccount' is of type 'unknown' when trying to access the data.
trying to set the var name to the inferred model type (like const getaAllAccounts: Account[] didn't work either...Type 'unknown' is not assignable to...).

Any ideas?
Thanks in adance๐Ÿ™
ps.png
Was this page helpful?