Skip to content

Commit

Permalink
feat: node-sqlite driver with native node:sqlite (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Feb 20, 2025
1 parent 41706e1 commit ad0e62c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/_connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import type { ConnectorOptions as LibSQLHttpOptions } from "db0/connectors/libsq
import type { ConnectorOptions as LibSQLNodeOptions } from "db0/connectors/libsql/node";
import type { ConnectorOptions as LibSQLWebOptions } from "db0/connectors/libsql/web";
import type { ConnectorOptions as MySQL2Options } from "db0/connectors/mysql2";
import type { ConnectorOptions as NodeSQLiteOptions } from "db0/connectors/node-sqlite";
import type { ConnectorOptions as NodeSQLite3Options } from "db0/connectors/node-sqlite3";
import type { ConnectorOptions as PgliteOptions } from "db0/connectors/pglite";
import type { ConnectorOptions as PlanetscaleOptions } from "db0/connectors/planetscale";
import type { ConnectorOptions as PostgreSQLOptions } from "db0/connectors/postgresql";

export type ConnectorName = "better-sqlite3" | "sqlite" | "bun-sqlite" | "bun" | "cloudflare-d1" | "libsql-core" | "libsql-http" | "libsql-node" | "libsql" | "libsql-web" | "mysql2" | "node-sqlite3" | "pglite" | "planetscale" | "postgresql";
export type ConnectorName = "better-sqlite3" | "sqlite" | "bun-sqlite" | "bun" | "cloudflare-d1" | "libsql-core" | "libsql-http" | "libsql-node" | "libsql" | "libsql-web" | "mysql2" | "node-sqlite" | "node-sqlite3" | "pglite" | "planetscale" | "postgresql";

export type ConnectorOptions = {
"better-sqlite3": BetterSQLite3Options;
Expand All @@ -30,6 +31,7 @@ export type ConnectorOptions = {
"libsql": LibSQLNodeOptions;
"libsql-web": LibSQLWebOptions;
"mysql2": MySQL2Options;
"node-sqlite": NodeSQLiteOptions;
"node-sqlite3": NodeSQLite3Options;
"pglite": PgliteOptions;
"planetscale": PlanetscaleOptions;
Expand All @@ -51,6 +53,7 @@ export const connectors = Object.freeze({
"libsql": "db0/connectors/libsql/node",
"libsql-web": "db0/connectors/libsql/web",
"mysql2": "db0/connectors/mysql2",
"node-sqlite": "db0/connectors/node-sqlite",
"node-sqlite3": "db0/connectors/node-sqlite3",
"pglite": "db0/connectors/pglite",
"planetscale": "db0/connectors/planetscale",
Expand Down
62 changes: 62 additions & 0 deletions src/connectors/node-sqlite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { resolve, dirname } from 'node:path'
import { mkdirSync } from 'node:fs'
import type { Connector } from "../types";
import type { DatabaseSync, StatementSync } from "node:sqlite";
import { BoundableStatement } from './_internal/statement';

export interface ConnectorOptions {
cwd?: string
path?: string
name?: string
}

export default function nodeSqlite3Connector(opts: ConnectorOptions) {
let _db: DatabaseSync | undefined

const getDB = () => {
if (_db) {
return _db
}
const nodeSqlite = globalThis.process?.getBuiltinModule?.('node:sqlite')
if (!nodeSqlite) {
throw new Error('`node:sqlite` module is not available. Please ensure you are running in Node.js >= 22.5 or Deno >= 2.2.')
}
if (opts.name === ':memory:') {
_db = new nodeSqlite.DatabaseSync(':memory:')
return _db
}
const filePath = resolve(
opts.cwd || '.',
opts.path || `.data/${opts.name || 'db'}.sqlite`,
)
mkdirSync(dirname(filePath), { recursive: true })
_db = new nodeSqlite.DatabaseSync(filePath)
return _db
}

return <Connector<DatabaseSync>>{
name: 'node-sqlite',
dialect: 'sqlite',
getInstance: () => getDB(),
exec(sql: string) {
getDB().exec(sql)
return { success: true }
},
prepare: sql => new StatementWrapper(getDB().prepare(sql)),
}
}

class StatementWrapper extends BoundableStatement<StatementSync> {
async all(...params) {
const raws = this._statement.all(...params)
return raws
}
async run(...params) {
const res = this._statement.run(...params)
return { success: true, ...res }
}
async get(...params) {
const raw = this._statement.get(...params)
return raw
}
}
12 changes: 12 additions & 0 deletions test/connectors/node-sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe } from "vitest";
import connector from "../../src/connectors/node-sqlite";
import { testConnector } from "./_tests";

describe("connectors: node-sqlite (native)", () => {
testConnector({
dialect: "sqlite",
connector: connector({
name: ":memory:",
}),
});
});

0 comments on commit ad0e62c

Please sign in to comment.