Skip to content
Open
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
38 changes: 31 additions & 7 deletions src/integrations/drizzle/_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
type TablesRelationalConfig,
entityKind,
NoopLogger,
type AnyColumn,
type SelectedFieldsOrdered,
} from "drizzle-orm";

import {
Expand All @@ -15,12 +17,12 @@ import {

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

import type { Database, Statement } from "db0";
import { mapResultRow } from "./_utils.ts";

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

Expand Down Expand Up @@ -49,7 +51,7 @@ export class DB0Session<
// @ts-expect-error TODO
prepareQuery(
query: Query,
fields: SelectedFieldsOrdered | undefined,
fields: SelectedFieldsOrdered<AnyColumn> | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): DB0PreparedQuery {
Expand Down Expand Up @@ -95,27 +97,49 @@ export class DB0PreparedQuery<
values: T["values"];
execute: T["execute"];
}> {
fields?: SelectedFieldsOrdered<AnyColumn>;

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

run(): Promise<{ success: boolean }> {
return this.stmt.run(...(this.query.params as any[]));
}

all(): Promise<unknown[]> {
return this.stmt.all(...(this.query.params as any[]));
async all(): Promise<unknown[]> {
const rows = await this.stmt.all(...(this.query.params as any[]));

if (!this.fields) {
return rows;
}

return rows.map((row) => {
const rowArray = this.fields.map(({ field }) => row[field.name]);
return mapResultRow(this.fields, rowArray, undefined);
});
}

get(): Promise<unknown> {
return this.stmt.get(...(this.query.params as any[]));
async get(): Promise<unknown> {
const row = await this.stmt.get(...(this.query.params as any[]));

if (!this.fields) {
return row;
}

return mapResultRow(
this.fields,
this.fields.map(({ field }) => row[field.name]),
undefined,
);
}

values(): Promise<unknown[]> {
Expand Down
17 changes: 16 additions & 1 deletion test/integrations/drizzle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe("integrations: drizzle: with schema parameter", () => {
id: dSqlite.numeric("id"),
name: dSqlite.text("name"),
email: dSqlite.text("email"),
joinDate: dSqlite.integer({ mode: "timestamp" }),
});

const schema = { users };
Expand All @@ -75,7 +76,8 @@ describe("integrations: drizzle: with schema parameter", () => {
await db.sql`create table if not exists users_schema (
id integer primary key autoincrement,
name text,
email text
email text,
joinDate integer
)`;
});

Expand All @@ -85,6 +87,7 @@ describe("integrations: drizzle: with schema parameter", () => {
.values({
name: "Jane Doe",
email: "jane@example.com",
joinDate: new Date("2025-01-30T12:00:00.000Z"),
})
.returning();

Expand All @@ -101,6 +104,18 @@ describe("integrations: drizzle: with schema parameter", () => {
expect(res[0].email).toBe("jane@example.com");
});

it(".all() converts integers to dates", async () => {
const res = await drizzleDb.select().from(users).all();

expect(res[0].joinDate).toStrictEqual(new Date("2025-01-30T12:00:00.000Z"));
});

it(".get() converts integers to dates", async () => {
const res = await drizzleDb.select().from(users).get();

expect(res!.joinDate).toStrictEqual(new Date("2025-01-30T12:00:00.000Z"));
});

afterAll(async () => {
await db.sql`DROP TABLE IF EXISTS users_schema`;
});
Expand Down