Skip to content

Commit 3387fb3

Browse files
committed
SQL のログを出力 (#648)
# PRの概要 - SQL のログを server のコンソールに出せるようにした
1 parent a5ca487 commit 3387fb3

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

bun.lock

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

server/.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ CORS_ALLOW_ORIGINS=http://localhost:3000,http://localhost:5173
88

99
# Firebase
1010
FIREBASE_PROJECT_ID=project-id
11+
12+
# Enable SQL Log (server/src/database/client.ts)
13+
SQL_LOG=true

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"firebase-admin": "^12.2.0",
2727
"sharp": "^0.33.5",
2828
"socket.io": "^4.7.5",
29+
"sql-formatter": "^15.4.10",
2930
"zod": "^3.23.8"
3031
},
3132
"devDependencies": {

server/src/database/client.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
11
import { PrismaClient } from "@prisma/client";
2+
import { format } from "sql-formatter";
23
import "../load-env";
34

4-
export let prisma = new PrismaClient();
5+
const { SQL_LOG } = process.env;
56

6-
// not sure if this is necessary
7-
export function reload() {
8-
prisma = new PrismaClient();
9-
}
7+
export const prisma = new PrismaClient(
8+
SQL_LOG === "true"
9+
? {
10+
log: [
11+
{
12+
emit: "event",
13+
level: "query",
14+
},
15+
{
16+
emit: "stdout",
17+
level: "error",
18+
},
19+
{
20+
emit: "stdout",
21+
level: "info",
22+
},
23+
{
24+
emit: "stdout",
25+
level: "warn",
26+
},
27+
],
28+
}
29+
: undefined,
30+
);
31+
32+
prisma.$on("query", (e) => {
33+
console.log(format(e.query, { language: "postgresql" }));
34+
console.log(`Params: ${e.params}`);
35+
console.log("\n");
36+
});

0 commit comments

Comments
 (0)