drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.54k stars 643 forks source link

[DOCUMENTATION ISSUE] : Documentation for raw sql queries incredibly unclear #1953

Open ben-hearn-sb opened 8 months ago

ben-hearn-sb commented 8 months ago

Hello Drizzle team,

Thought I would report this to you as a heads up as I spent some hours on this.

I am running drizzle 0.29.4 and drizzle kit 0.20.14 and using better-sqlite3 as the engine.

I was trying to execute a raw SQL query using "sql.raw()" and spent many hours trying to figure out how to actually execute it properly.

The doc states you can use "db.execute", however the db does not have that method available so after many hours of searching and trying different things to get my results I found the actual execution hidden away inside "goodies" where i was able to run "db.all()" on my query.

The current webpage for using "sql.raw()" is fine for actually building the string, the problem is the execution is not listed anywhere near the actual "magic sql operator" section.

To speed up getting into the ORM I would suggest you move the execution of sql raw into the actual "magic sql" section.

Docs around SQL raw: https://orm.drizzle.team/docs/sql Doc where actual execution lives: https://orm.drizzle.team/docs/goodies#raw-sql-queries-execution

Cheers,

Ben

khanakia commented 4 months ago

@ben-hearn-sb did you find the solution how to run raw SQL ?

I am getting this error when I trying to run according to docs Property 'execute' does not exist on type 'BetterSQLite3Database<Record<string, never>>'.ts(2339)

import { sql } from 'drizzle-orm' 
const id = 69;

import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';
import { text, integer, sqliteTable } from "drizzle-orm/sqlite-core";

const sqlite = new Database('content.db');

export const db = drizzle(sqlite);

await db.execute(sql`select * from ${usersTable} where ${usersTable.id} = ${id}`)
ben-hearn-sb commented 4 months ago

Hey @khanakia yes I did in the end. I had to use "db.run()" or "db.all()" instead of execute. The docs dont reflect this at all so I stumbled upon it

khanakia commented 4 months ago

Thanks