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
21.47k stars 486 forks source link

[BUG]: TypeError: Cannot read properties of undefined (reading 'findFirst') #2475

Closed jamiehaywood closed 3 weeks ago

jamiehaywood commented 3 weeks ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.5

Describe the Bug

import { drizzle } from "drizzle-orm/bun-sqlite";
import { Database } from "bun:sqlite";
import * as schema from "./schema";
import { eq } from "drizzle-orm/expressions";

const sqlite = new Database("sqlite.db");

const db = drizzle<typeof schema>(sqlite);

const book = {
  id: "1",
  title: "The Great Gatsby",
  description: "A book about a rich guy",
};

await db.query.books.findFirst({ where: (b) => eq(b.id, book.id) });

The above query fails with TypeError: Cannot read properties of undefined (reading 'findFirst').

If I change the driver to better-sqlite3 it's the same.

Expected behavior

To be able to run findFirst queries

Environment & setup

No response

jamiehaywood commented 3 weeks ago

My current workaround is:

const [ first ] = await db
  .select()
  .from(schema.books)
  .where((b) => eq(b.id, book.id));
jamiehaywood commented 3 weeks ago

I reread the docs, and to use the query builder, you need to pass schema in as the second argument to the db constructor:

const db = drizzle<typeof schema>(sqlite, { schema });
                                             ^^^^^