Skip to content

Commit ffee1a6

Browse files
committed
Use @logtape/drizzle-orm for database query logging
Replace custom LogTapeLogger implementation with the official @logtape/drizzle-orm package, which provides the same functionality with better maintenance and support. This simplifies the codebase by removing ~50 lines of custom logger implementation.
1 parent 68d235c commit ffee1a6

File tree

3 files changed

+15
-51
lines changed

3 files changed

+15
-51
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@hono/node-server": "^1.19.8",
3030
"@hono/zod-validator": "^0.7.6",
3131
"@js-temporal/polyfill": "^0.5.0",
32+
"@logtape/drizzle-orm": "~2.0.0",
3233
"@logtape/file": "~2.0.0",
3334
"@logtape/logtape": "~2.0.0",
3435
"@logtape/sentry": "~2.0.0",

pnpm-lock.yaml

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

src/db.ts

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { getLogger } from "@logtape/logtape";
1+
import { getLogger } from "@logtape/drizzle-orm";
22
import type { ExtractTablesWithRelations } from "drizzle-orm";
3-
import type { Logger } from "drizzle-orm/logger";
43
import type { PgDatabase, PgTransaction } from "drizzle-orm/pg-core";
54
import {
65
drizzle,
@@ -13,59 +12,11 @@ import * as schema from "./schema";
1312
const databaseUrl = process.env["DATABASE_URL"];
1413
if (databaseUrl == null) throw new Error("DATABASE_URL must be defined");
1514

16-
class LogTapeLogger implements Logger {
17-
readonly logger = getLogger("drizzle-orm");
18-
19-
logQuery(query: string, params: unknown[]): void {
20-
const stringifiedParams = params.map(LogTapeLogger.serialize);
21-
const formattedQuery = query.replace(/\$(\d+)/g, (m) => {
22-
const index = Number.parseInt(m.slice(1), 10);
23-
return stringifiedParams[index - 1];
24-
});
25-
this.logger.debug("Query: {formattedQuery}", {
26-
formattedQuery,
27-
query,
28-
params,
29-
});
30-
}
31-
32-
static serialize(p: unknown): string {
33-
if (typeof p === "undefined" || p === null) return "NULL";
34-
if (typeof p === "string") return LogTapeLogger.stringLiteral(p);
35-
if (typeof p === "number" || typeof p === "bigint") return p.toString();
36-
if (typeof p === "boolean") return p ? "'t'" : "'f'";
37-
if (p instanceof Date) return LogTapeLogger.stringLiteral(p.toISOString());
38-
if (Array.isArray(p)) {
39-
return `ARRAY[${p.map(LogTapeLogger.serialize).join(", ")}]`;
40-
}
41-
if (typeof p === "object") {
42-
// Assume it's a JSON object
43-
return LogTapeLogger.stringLiteral(JSON.stringify(p));
44-
}
45-
return LogTapeLogger.stringLiteral(String(p));
46-
}
47-
48-
static stringLiteral(s: string) {
49-
if (/\\'\n\r\t\b\f/.exec(s)) {
50-
let str = s;
51-
str = str.replaceAll("\\", "\\\\");
52-
str = str.replaceAll("'", "\\'");
53-
str = str.replaceAll("\n", "\\n");
54-
str = str.replaceAll("\r", "\\r");
55-
str = str.replaceAll("\t", "\\t");
56-
str = str.replaceAll("\b", "\\b");
57-
str = str.replaceAll("\f", "\\f");
58-
return `E'${str}'`;
59-
}
60-
return `'${s}'`;
61-
}
62-
}
63-
6415
export const postgres = createPostgres(databaseUrl, {
6516
connect_timeout: 5,
6617
connection: { IntervalStyle: "iso_8601" },
6718
});
68-
export const db = drizzle(postgres, { schema, logger: new LogTapeLogger() });
19+
export const db = drizzle(postgres, { schema, logger: getLogger() });
6920

7021
export type Database = PgDatabase<
7122
PostgresJsQueryResultHKT,

0 commit comments

Comments
 (0)