DT
Drizzle Teamโ€ข7mo 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();
// 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" })
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๐Ÿ™
No description
2 Replies
n00ki
n00kiOPโ€ข7mo ago
anyone?๐Ÿ™Š
Artamiel
Artamielโ€ข7mo ago
Hi, newbie here just trying to help based on the docs, I believe the prepared statement should be executed with .execute() instead of .all() may I know why did you use .execute on the getAccount but .all on the .getAllAccounts ?
Want results from more Discord servers?
Add your server