drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

Add `drizzle-orm/node-sqlite` for node:sqlite support

Open mizchi opened this issue 8 months ago • 3 comments

I added drizzle-orm/node-sqlite driver for node:sqlite

import { drizzle } from "drizzle-orm/node-sqlite";
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
import { eq } from "drizzle-orm";

const users = sqliteTable("users", {
  id: integer("id").primaryKey(),
  name: text("name").notNull(),
});
const db = drizzle(":memory:");

https://nodejs.org/api/sqlite.html

node:sqlite requires node >v22.5

It's mostly a port of better-sqlite3 version.

Test

My local tests.

https://gist.github.com/mizchi/4859a5134e92bea1ab41259fed328f9e

with deno+sqlite+drizzle

https://gist.github.com/mizchi/9c69c920ac12e6cb7e77b1fd6be8adbe

integration-tests is mostly a stub yet, as it didn't port well as is.

Motivation

for deno + drizzle + sqlite https://github.com/drizzle-team/drizzle-orm/issues/2648#issuecomment-2766058213

related https://github.com/drizzle-team/drizzle-orm/pull/3868

mizchi avatar Mar 31 '25 15:03 mizchi

How can I test it locally? "drizzle-orm": "git+https://github.com/mizchi/drizzle-orm.git#node-sqlite" results in npm error Unsupported URL Type "workspace:": workspace:./drizzle-orm/dist.

UPD: I tried using https://gitpkg.vercel.app, but building Drizzle locally is another level of complexity. Would have to stick to a drizzle-orm/sqlite-proxy wrapper until this is merged!

Proxy implementation reference
import { drizzle } from "drizzle-orm/sqlite-proxy";
import { DatabaseSync } from "node:sqlite";

const sqlite = new DatabaseSync(":memory:");

const db = drizzle<typeof schema>(
  async (sql, params, method) => {
    // console.debug({ sql, params, method });
    let stmt = sqlite.prepare(sql);

    switch (method) {
      case "all": {
        const rows = stmt.all(...params);
        // console.debug({ rows });
        return {
          rows: rows.map((row) => Object.values(row as any)),
        };
      }

      case "get": {
        const row = stmt.get(...params);
        // console.debug({ row });
        return { rows: Object.values(row as any) };
      }

      case "run":
      case "values":
        stmt.run(...params);
        return { rows: [] };
    }
  },

  // Pass the schema to the drizzle instance
  { schema },
);

vladfaust avatar Mar 31 '25 16:03 vladfaust

I would also like to know how it is designed in this project.

I mainly wanted to use it with deno, so I released my own scoped package (@mizchi/drizzle-orm) for debugging and checked the operation.

import { drizzle } from "npm:@mizchi/drizzle-orm/dist/node-sqlite/index.js";
import {
  integer,
  sqliteTable,
  text,
} from "npm:@mizchi/drizzle-orm/dist/sqlite-core/index.js";
import { eq } from "npm:@mizchi/drizzle-orm/dist/index.js";

Note: This package is for checking the operation and will not be maintained. Do not use it.

mizchi avatar Apr 01 '25 06:04 mizchi

upvote (sorry for offtopic, but i hope this will be merged soon)

shevernitskiy avatar Apr 24 '25 14:04 shevernitskiy

Progress on this? like there is conflicts need to be resolved

CyanChanges avatar May 06 '25 08:05 CyanChanges

Would love to see this happen.

For some reason, Claude thinks that Drizzle ORM supports Node.js SQLite:

image

hyunbinseo avatar Jun 09 '25 03:06 hyunbinseo

Copilot also thinks so... In the meantime, is there any workaround to test Drizzle on node:sqlite, for example by manually creating a connection and passing it to one of the other drivers or something? Because that is basically what Copilot is suggesting me to do (pass created connection to better-sqlite3 driver) but I don't trust its advice. Any one can share some insight in this?

Download avatar Jun 15 '25 11:06 Download