drizzle-orm
drizzle-orm copied to clipboard
Add `drizzle-orm/node-sqlite` for node:sqlite support
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
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 },
);
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.
upvote (sorry for offtopic, but i hope this will be merged soon)
Progress on this? like there is conflicts need to be resolved
Would love to see this happen.
For some reason, Claude thinks that Drizzle ORM supports Node.js SQLite:
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?