drizzlebetter-sqlite3

how to create a query and then exec it using drizzle query builder feature


I'm working on a project using Drizzle ORM and Better-SQLite3 with TypeScript. I need to execute a query after a specific event occurs. Here's the code I have for constructing the query:

const query = qb.select().from(users).where(eq(users.id, 1));

I understand that toSQL can give me the raw query and parameters, but I'm not sure how to actually execute it using Better-SQLite3. I tried the following, but I'm not sure if it's correct or how to proceed:

const { sql, params } = query.toSQL();
// What do I do next to run this query?

Solution

  • the following code works perfect to me

      const query = database.prepare(sql);
    
      const output = query.reader ? query.all(...params) : query.run(...params);