Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ jobs:
- run: pnpm install
- run: pnpm lint
- run: pnpm build
- run: pnpm test:types
- run: pnpm vitest --coverage
- uses: codecov/codecov-action@v5
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"repository": "unjs/db0",
"license": "MIT",
"sideEffects": false,
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.mts",
Expand Down Expand Up @@ -36,8 +37,9 @@
"lint:fix": "eslint . --fix && prettier -w src test",
"prepack": "pnpm build",
"release": "pnpm test && changelogen --release --push && pnpm publish",
"test": "pnpm lint && vitest run --coverage && pnpm test:bun",
"test:bun": "bun test ./test/connectors/bun-test.ts"
"test": "pnpm lint && pnpm test:types && vitest run --coverage && pnpm test:bun",
"test:bun": "bun test ./test/connectors/bun-test.ts",
"test:types": "tsc --noEmit"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20251001.0",
Expand All @@ -51,7 +53,7 @@
"automd": "^0.4.2",
"better-sqlite3": "^12.4.1",
"changelogen": "^0.6.2",
"db0": "^0.3.2",
"db0": "link:.",
"dotenv": "^17.2.3",
"drizzle-orm": "^0.44.5",
"eslint": "^9.36.0",
Expand Down
13 changes: 2 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/gen-connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export type ConnectorOptions = {
.join("\n ")}
};

export const connectors = Object.freeze({
export const connectors: Record<ConnectorName, string> = Object.freeze({
${connectors.flatMap((d) => d.names.map((name, i) => `${i === 0 ? "" : `/** alias of ${d.name} */\n `}"${name}": "${d.subpath}"`)).join(",\n ")},
} as const);
`;
Expand Down
2 changes: 1 addition & 1 deletion src/_connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type ConnectorOptions = {
"sqlite3": SQLite3Options;
};

export const connectors = Object.freeze({
export const connectors: Record<ConnectorName, string> = Object.freeze({
"better-sqlite3": "db0/connectors/better-sqlite3",
"bun-sqlite": "db0/connectors/bun-sqlite",
/** alias of bun-sqlite */
Expand Down
10 changes: 5 additions & 5 deletions src/connectors/better-sqlite3.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { resolve, dirname } from "node:path";
import { mkdirSync } from "node:fs";
import Database from "better-sqlite3";
import type { Connector } from "db0";
import type { Connector, Primitive } from "db0";
import type { Statement as RawStatement } from "better-sqlite3";
import { BoundableStatement } from "./_internal/statement";
import { BoundableStatement } from "./_internal/statement.ts";

export interface ConnectorOptions {
cwd?: string;
Expand Down Expand Up @@ -42,16 +42,16 @@ export default function sqliteConnector(
}

class StatementWrapper extends BoundableStatement<() => RawStatement> {
async all(...params) {
async all(...params: Primitive[]) {
return this._statement().all(...params);
}

async run(...params) {
async run(...params: Primitive[]) {
const res = this._statement().run(...params);
return { success: res.changes > 0, ...res };
}

async get(...params) {
async get(...params: Primitive[]) {
return this._statement().get(...params);
}
}
10 changes: 5 additions & 5 deletions src/connectors/bun-sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { resolve, dirname } from "node:path";
import { mkdirSync } from "node:fs";
import { Database, Statement as RawStatement } from "bun:sqlite";
import type { Connector } from "db0";
import { BoundableStatement } from "./_internal/statement";
import type { Connector, Primitive } from "db0";
import { BoundableStatement } from "./_internal/statement.ts";

export interface ConnectorOptions {
cwd?: string;
Expand Down Expand Up @@ -41,16 +41,16 @@ export default function bunSqliteConnector(
}

class StatementWrapper extends BoundableStatement<RawStatement> {
all(...params) {
all(...params: Primitive[]) {
return Promise.resolve(this._statement.all(...params));
}

run(...params) {
run(...params: Primitive[]) {
const res = this._statement.run(...params);
return Promise.resolve({ success: true, ...res });
}

get(...params) {
get(...params: Primitive[]) {
return Promise.resolve(this._statement.get(...params));
}
}
14 changes: 7 additions & 7 deletions src/connectors/cloudflare-d1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type {
D1Database,
D1PreparedStatement as RawStatement,
} from "@cloudflare/workers-types";
import type { Connector } from "db0";
import { BoundableStatement } from "./_internal/statement";
import type { Connector, Primitive } from "db0";
import { BoundableStatement } from "./_internal/statement.ts";

export interface ConnectorOptions {
bindingName?: string;
Expand All @@ -15,8 +15,8 @@ export default function cloudflareD1Connector(
const getDB = () => {
// TODO: Remove legacy __cf_env__ support in next major version
const binding: D1Database =
globalThis.__env__?.[options.bindingName] ||
globalThis.__cf_env__?.[options.bindingName];
((globalThis as any).__env__ as any)?.[options.bindingName!] ||
((globalThis as any).__cf_env__ as any)?.[options.bindingName!];
if (!binding) {
throw new Error(
`[db0] [d1] binding \`${options.bindingName}\` not found`,
Expand All @@ -35,17 +35,17 @@ export default function cloudflareD1Connector(
}

class StatementWrapper extends BoundableStatement<RawStatement> {
async all(...params) {
async all(...params: Primitive[]) {
const res = await this._statement.bind(...params).all();
return res.results;
}

async run(...params) {
async run(...params: Primitive[]) {
const res = await this._statement.bind(...params).run();
return res;
}

async get(...params) {
async get(...params: Primitive[]) {
const res = await this._statement.bind(...params).first();
return res;
}
Expand Down
25 changes: 17 additions & 8 deletions src/connectors/libsql/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Client, InStatement } from "@libsql/client";
import type { Connector } from "db0";
import { BoundableStatement } from "../_internal/statement";
import type { Connector, Primitive } from "db0";
import { BoundableStatement } from "../_internal/statement.ts";

export type ConnectorOptions = {
getClient: () => Client;
Expand Down Expand Up @@ -33,20 +33,29 @@ class StatementWrapper extends BoundableStatement<void> {
this.#query = query;
}

async all(...params) {
const res = await this.#query({ sql: this.#sql, args: params });
async all(...params: Primitive[]) {
const res = await this.#query({
sql: this.#sql,
args: params as Exclude<Primitive, undefined>[],
});
return res.rows;
}

async run(...params) {
const res = await this.#query({ sql: this.#sql, args: params });
async run(...params: Primitive[]) {
const res = await this.#query({
sql: this.#sql,
args: params as Exclude<Primitive, undefined>[],
});
return {
...res,
};
}

async get(...params) {
const res = await this.#query({ sql: this.#sql, args: params });
async get(...params: Primitive[]) {
const res = await this.#query({
sql: this.#sql,
args: params as Exclude<Primitive, undefined>[],
});
return res.rows[0];
}
}
6 changes: 3 additions & 3 deletions src/connectors/libsql/http.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Config, Client } from "@libsql/client";
import type { Connector } from "db0";
import type { Connector, Primitive } from "db0";
import { createClient } from "@libsql/client/http";
import libSqlCore from "./core";
import libSqlCore from "./core.ts";

export type ConnectorOptions = Config;

export default function libSqlConnector(
opts: ConnectorOptions,
): Connector<Client> {
let _client;
let _client: Client | undefined;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
Expand Down
6 changes: 3 additions & 3 deletions src/connectors/libsql/node.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Config, Client } from "@libsql/client";
import type { Connector } from "db0";
import type { Connector, Primitive } from "db0";
import { createClient } from "@libsql/client";
import libSqlCore from "./core";
import libSqlCore from "./core.ts";

export type ConnectorOptions = Config;

export default function libSqlConnector(
opts: ConnectorOptions,
): Connector<Client> {
let _client;
let _client: Client | undefined;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
Expand Down
6 changes: 3 additions & 3 deletions src/connectors/libsql/web.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Config, Client } from "@libsql/client";
import type { Connector } from "db0";
import type { Connector, Primitive } from "db0";
import { createClient } from "@libsql/client/http";
import libSqlCore from "./core";
import libSqlCore from "./core.ts";

export type ConnectorOptions = Config;

export default function libSqlConnector(
opts: ConnectorOptions,
): Connector<Client> {
let _client;
let _client: Client | undefined;
const getClient = () => {
if (!_client) {
_client = createClient(opts);
Expand Down
10 changes: 5 additions & 5 deletions src/connectors/mysql2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mysql from "mysql2/promise";
import type { Connector } from "db0";
import { BoundableStatement } from "./_internal/statement";
import type { Connector, Primitive } from "db0";
import { BoundableStatement } from "./_internal/statement.ts";

export type ConnectorOptions = mysql.ConnectionOptions;

Expand Down Expand Up @@ -49,20 +49,20 @@ class StatementWrapper extends BoundableStatement<void> {
this.#query = query;
}

async all(...params) {
async all(...params: Primitive[]) {
const res = (await this.#query(this.#sql, params)) as mysql.RowDataPacket[];
return res;
}

async run(...params) {
async run(...params: Primitive[]) {
const res = (await this.#query(this.#sql, params)) as mysql.RowDataPacket[];
return {
success: true,
...res,
};
}

async get(...params) {
async get(...params: Primitive[]) {
const res = (await this.#query(this.#sql, params)) as mysql.RowDataPacket[];
return res[0];
}
Expand Down
22 changes: 14 additions & 8 deletions src/connectors/node-sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { resolve, dirname } from "node:path";
import { mkdirSync } from "node:fs";
import type { Connector } from "db0";
import type { Connector, Primitive } from "db0";
import type { DatabaseSync, StatementSync } from "node:sqlite";
import { BoundableStatement } from "./_internal/statement";
import { BoundableStatement } from "./_internal/statement.ts";

export interface ConnectorOptions {
cwd?: string;
Expand Down Expand Up @@ -51,16 +51,22 @@ export default function nodeSqlite3Connector(
}

class StatementWrapper extends BoundableStatement<() => StatementSync> {
async all(...params) {
const raws = this._statement().all(...params);
async all(...params: Primitive[]) {
const raws = this._statement().all(
...(params as Exclude<Primitive, undefined | boolean>[]),
);
return raws;
}
async run(...params) {
const res = this._statement().run(...params);
async run(...params: Primitive[]) {
const res = this._statement().run(
...(params as Exclude<Primitive, undefined | boolean>[]),
);
return { success: true, ...res };
}
async get(...params) {
const raw = this._statement().get(...params);
async get(...params: Primitive[]) {
const raw = this._statement().get(
...(params as Exclude<Primitive, undefined | boolean>[]),
);
return raw;
}
}
Loading