Skip to content

Commit d7b4db0

Browse files
authored
refactor: strict types (#179)
1 parent 74b4ae9 commit d7b4db0

26 files changed

+147
-117
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ jobs:
2121
- run: pnpm install
2222
- run: pnpm lint
2323
- run: pnpm build
24+
- run: pnpm test:types
2425
- run: pnpm vitest --coverage
2526
- uses: codecov/codecov-action@v5

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"repository": "unjs/db0",
66
"license": "MIT",
77
"sideEffects": false,
8+
"type": "module",
89
"exports": {
910
".": {
1011
"types": "./dist/index.d.mts",
@@ -36,8 +37,9 @@
3637
"lint:fix": "eslint . --fix && prettier -w src test",
3738
"prepack": "pnpm build",
3839
"release": "pnpm test && changelogen --release --push && pnpm publish",
39-
"test": "pnpm lint && vitest run --coverage && pnpm test:bun",
40-
"test:bun": "bun test ./test/connectors/bun-test.ts"
40+
"test": "pnpm lint && pnpm test:types && vitest run --coverage && pnpm test:bun",
41+
"test:bun": "bun test ./test/connectors/bun-test.ts",
42+
"test:types": "tsc --noEmit"
4143
},
4244
"devDependencies": {
4345
"@cloudflare/workers-types": "^4.20251001.0",
@@ -51,7 +53,7 @@
5153
"automd": "^0.4.2",
5254
"better-sqlite3": "^12.4.1",
5355
"changelogen": "^0.6.2",
54-
"db0": "^0.3.2",
56+
"db0": "link:.",
5557
"dotenv": "^17.2.3",
5658
"drizzle-orm": "^0.44.5",
5759
"eslint": "^9.36.0",

pnpm-lock.yaml

Lines changed: 2 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/gen-connectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export type ConnectorOptions = {
105105
.join("\n ")}
106106
};
107107
108-
export const connectors = Object.freeze({
108+
export const connectors: Record<ConnectorName, string> = Object.freeze({
109109
${connectors.flatMap((d) => d.names.map((name, i) => `${i === 0 ? "" : `/** alias of ${d.name} */\n `}"${name}": "${d.subpath}"`)).join(",\n ")},
110110
} as const);
111111
`;

src/_connectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type ConnectorOptions = {
3838
"sqlite3": SQLite3Options;
3939
};
4040

41-
export const connectors = Object.freeze({
41+
export const connectors: Record<ConnectorName, string> = Object.freeze({
4242
"better-sqlite3": "db0/connectors/better-sqlite3",
4343
"bun-sqlite": "db0/connectors/bun-sqlite",
4444
/** alias of bun-sqlite */

src/connectors/better-sqlite3.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { resolve, dirname } from "node:path";
22
import { mkdirSync } from "node:fs";
33
import Database from "better-sqlite3";
4-
import type { Connector } from "db0";
4+
import type { Connector, Primitive } from "db0";
55
import type { Statement as RawStatement } from "better-sqlite3";
6-
import { BoundableStatement } from "./_internal/statement";
6+
import { BoundableStatement } from "./_internal/statement.ts";
77

88
export interface ConnectorOptions {
99
cwd?: string;
@@ -42,16 +42,16 @@ export default function sqliteConnector(
4242
}
4343

4444
class StatementWrapper extends BoundableStatement<() => RawStatement> {
45-
async all(...params) {
45+
async all(...params: Primitive[]) {
4646
return this._statement().all(...params);
4747
}
4848

49-
async run(...params) {
49+
async run(...params: Primitive[]) {
5050
const res = this._statement().run(...params);
5151
return { success: res.changes > 0, ...res };
5252
}
5353

54-
async get(...params) {
54+
async get(...params: Primitive[]) {
5555
return this._statement().get(...params);
5656
}
5757
}

src/connectors/bun-sqlite.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { resolve, dirname } from "node:path";
22
import { mkdirSync } from "node:fs";
33
import { Database, Statement as RawStatement } from "bun:sqlite";
4-
import type { Connector } from "db0";
5-
import { BoundableStatement } from "./_internal/statement";
4+
import type { Connector, Primitive } from "db0";
5+
import { BoundableStatement } from "./_internal/statement.ts";
66

77
export interface ConnectorOptions {
88
cwd?: string;
@@ -41,16 +41,16 @@ export default function bunSqliteConnector(
4141
}
4242

4343
class StatementWrapper extends BoundableStatement<RawStatement> {
44-
all(...params) {
44+
all(...params: Primitive[]) {
4545
return Promise.resolve(this._statement.all(...params));
4646
}
4747

48-
run(...params) {
48+
run(...params: Primitive[]) {
4949
const res = this._statement.run(...params);
5050
return Promise.resolve({ success: true, ...res });
5151
}
5252

53-
get(...params) {
53+
get(...params: Primitive[]) {
5454
return Promise.resolve(this._statement.get(...params));
5555
}
5656
}

src/connectors/cloudflare-d1.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type {
22
D1Database,
33
D1PreparedStatement as RawStatement,
44
} from "@cloudflare/workers-types";
5-
import type { Connector } from "db0";
6-
import { BoundableStatement } from "./_internal/statement";
5+
import type { Connector, Primitive } from "db0";
6+
import { BoundableStatement } from "./_internal/statement.ts";
77

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

3737
class StatementWrapper extends BoundableStatement<RawStatement> {
38-
async all(...params) {
38+
async all(...params: Primitive[]) {
3939
const res = await this._statement.bind(...params).all();
4040
return res.results;
4141
}
4242

43-
async run(...params) {
43+
async run(...params: Primitive[]) {
4444
const res = await this._statement.bind(...params).run();
4545
return res;
4646
}
4747

48-
async get(...params) {
48+
async get(...params: Primitive[]) {
4949
const res = await this._statement.bind(...params).first();
5050
return res;
5151
}

src/connectors/libsql/core.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Client, InStatement } from "@libsql/client";
2-
import type { Connector } from "db0";
3-
import { BoundableStatement } from "../_internal/statement";
2+
import type { Connector, Primitive } from "db0";
3+
import { BoundableStatement } from "../_internal/statement.ts";
44

55
export type ConnectorOptions = {
66
getClient: () => Client;
@@ -33,20 +33,29 @@ class StatementWrapper extends BoundableStatement<void> {
3333
this.#query = query;
3434
}
3535

36-
async all(...params) {
37-
const res = await this.#query({ sql: this.#sql, args: params });
36+
async all(...params: Primitive[]) {
37+
const res = await this.#query({
38+
sql: this.#sql,
39+
args: params as Exclude<Primitive, undefined>[],
40+
});
3841
return res.rows;
3942
}
4043

41-
async run(...params) {
42-
const res = await this.#query({ sql: this.#sql, args: params });
44+
async run(...params: Primitive[]) {
45+
const res = await this.#query({
46+
sql: this.#sql,
47+
args: params as Exclude<Primitive, undefined>[],
48+
});
4349
return {
4450
...res,
4551
};
4652
}
4753

48-
async get(...params) {
49-
const res = await this.#query({ sql: this.#sql, args: params });
54+
async get(...params: Primitive[]) {
55+
const res = await this.#query({
56+
sql: this.#sql,
57+
args: params as Exclude<Primitive, undefined>[],
58+
});
5059
return res.rows[0];
5160
}
5261
}

src/connectors/libsql/http.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { Config, Client } from "@libsql/client";
2-
import type { Connector } from "db0";
2+
import type { Connector, Primitive } from "db0";
33
import { createClient } from "@libsql/client/http";
4-
import libSqlCore from "./core";
4+
import libSqlCore from "./core.ts";
55

66
export type ConnectorOptions = Config;
77

88
export default function libSqlConnector(
99
opts: ConnectorOptions,
1010
): Connector<Client> {
11-
let _client;
11+
let _client: Client | undefined;
1212
const getClient = () => {
1313
if (!_client) {
1414
_client = createClient(opts);

0 commit comments

Comments
 (0)