Skip to content

Commit

Permalink
refactor: update drizzle integration
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 26, 2024
1 parent 303f138 commit 74c909e
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 120 deletions.
4 changes: 2 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules
coverage
dist
drivers
/server*
/integrations
/connectors
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"extends": ["eslint-config-unjs"],
"rules": {
"unicorn/expiring-todo-comments": 0,
"@typescript-eslint/no-non-null-assertion": 0
"@typescript-eslint/no-non-null-assertion": 0,
"unicorn/no-null": 0,
"@typescript-eslint/no-unused-vars": 0
}
}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ node_modules
coverage
dist
tmp
/connectors
/test.*
__*
.data
.tmp
.env

/connectors
/integrations
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
connectors
integrations
dist
13 changes: 12 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default defineBuildConfig({
},
entries: [
"src/index",
"src/drizzle",
{
input: "src/connectors/",
outDir: "connectors",
Expand All @@ -20,5 +19,17 @@ export default defineBuildConfig({
ext: "cjs",
declaration: false,
},
{
input: "src/integrations/",
outDir: "integrations",
format: "esm",
},
{
input: "src/integrations/",
outDir: "integrations",
format: "cjs",
ext: "cjs",
declaration: false,
},
],
});
2 changes: 1 addition & 1 deletion docs/3.integrations/drizzle.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ icon: simple-icons:drizzle
```ts [index.ts]
import { createDatabase } from "db0";
import sqlite from "db0/connectors/better-sqlite3";
import { drizzle } from "db0/drizzle";
import { drizzle } from "db0/integrations/drizzle";
import { sqliteTable, text, numeric } from "drizzle-orm/sqlite-core";

// Initialize DB instance
Expand Down
7 changes: 2 additions & 5 deletions examples/drizzle/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { sqliteTable, text, numeric } from "drizzle-orm/sqlite-core";

// import { createDatabase } from "db0";
// import sqlite from "db0/connectors/better-sqlite3";
// import { drizzle } from "db0/drizzle";

import { createDatabase } from "../../src";
import { drizzle } from "../../src/integrations/drizzle"

import sqlite from "../../src/connectors/better-sqlite3";
import { drizzle } from "../../src/drizzle";

export const users = sqliteTable("users", {
id: numeric("id"),
Expand Down
3 changes: 2 additions & 1 deletion examples/drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"devDependencies": {
"drizzle-kit": "^0.20.14",
"drizzle-orm": "^0.29.4",
"jiti": "^1.21.0"
"jiti": "^1.21.0",
"db0": "latest"
}
}
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"import": "./connectors/*.mjs",
"require": "./connectors/*.cjs"
},
"./drizzle": {
"types": "./dist/drizzle.d.ts",
"import": "./dist/drizzle.mjs",
"require": "./dist/drizzle.cjs"
"./integrations/*": {
"types": "./integrations/*.d.ts",
"import": "./integrations/*.mjs",
"require": "./integrations/*.cjs"
},
".": {
"types": "./dist/index.d.ts",
Expand All @@ -35,17 +35,18 @@
"types": "./dist/index.d.ts",
"files": [
"dist",
"connectors"
"connectors",
"integrations"
],
"scripts": {
"build": "unbuild",
"db0": "pnpm jiti src/cli",
"dev": "vitest",
"lint": "eslint --ext .ts . && prettier -c src test",
"lint:fix": "eslint --ext .ts . --fix && prettier -w src test",
"prepack": "pnpm build",
"release": "pnpm test && changelogen --release --push && pnpm publish",
"test": "pnpm lint && vitest run --coverage",
"db0": "pnpm jiti src/cli"
"test": "pnpm lint && vitest run --coverage"
},
"devDependencies": {
"@libsql/client": "^0.5.2",
Expand All @@ -55,6 +56,7 @@
"automd": "^0.3.6",
"better-sqlite3": "^9.4.3",
"changelogen": "^0.5.5",
"db0": "link:.",
"dotenv": "^16.4.5",
"drizzle-orm": "^0.29.4",
"eslint": "^8.57.0",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

101 changes: 0 additions & 101 deletions src/drizzle.ts

This file was deleted.

125 changes: 125 additions & 0 deletions src/integrations/drizzle/_session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import {
entityKind,
Logger,
RelationalSchemaConfig,
type Query,
type TablesRelationalConfig,
NoopLogger,
} from "drizzle-orm";

import {
SQLiteAsyncDialect,
SQLiteSession,
SQLitePreparedQuery,
} from "drizzle-orm/sqlite-core";

import type {
PreparedQueryConfig,
SelectedFieldsOrdered,
SQLiteExecuteMethod,
SQLiteTransactionConfig,
} from "drizzle-orm/sqlite-core";

import type { Database, Statement } from "../../types";

// Used as reference: https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/d1/session.ts

export interface DB0SessionOptions {
logger?: Logger;
}

export class DB0Session<
TFullSchema extends Record<string, unknown>,
TSchema extends TablesRelationalConfig,
> extends SQLiteSession<"async", unknown, TFullSchema, TSchema> {
dialect: SQLiteAsyncDialect;

private logger: Logger;

constructor(
private db: Database,
dialect: SQLiteAsyncDialect,
private schema: RelationalSchemaConfig<TSchema> | undefined,
private options: DB0SessionOptions = {},
) {
super(dialect);
this.logger = options.logger ?? new NoopLogger();
}

prepareQuery(
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): DB0PreparedQuery {
const stmt = this.db.prepare(query.sql);
return new DB0PreparedQuery(
stmt,
query,
this.logger,
fields,
executeMethod,
customResultMapper,
);
}

// TODO: Implement batch

// TODO: Implement transaction
override transaction<T>(
transaction: (tx: any) => T | Promise<T>,
config?: SQLiteTransactionConfig,
): Promise<T> {
throw new Error("transaction is not implemented!");
// const tx = new D1Transaction('async', this.dialect, this, this.schema);
// await this.run(sql.raw(`begin${config?.behavior ? ' ' + config.behavior : ''}`));
// try {
// const result = await transaction(tx);
// await this.run(sql`commit`);
// return result;
// } catch (err) {
// await this.run(sql`rollback`);
// throw err;
// }
}
}

export class DB0PreparedQuery<
T extends PreparedQueryConfig = PreparedQueryConfig,
> extends SQLitePreparedQuery<{
type: "async";
run: Awaited<ReturnType<Statement["run"]>>;
all: T["all"];
get: T["get"];
values: T["values"];
execute: T["execute"];
}> {
static readonly [entityKind]: string = "DB0PreparedQuery";

constructor(
private stmt: Statement,
query: Query,
private logger: Logger,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
) {
super("async", executeMethod, query);
}

run() {
return this.stmt.run(...(this.query.params as any[]));
}

all() {
return this.stmt.all(...(this.query.params as any[]));
}

get() {
return this.stmt.get(...(this.query.params as any[]));
}

values() {
return Promise.reject(new Error("values is not implemented!"));
}
}
Loading

0 comments on commit 74c909e

Please sign in to comment.