Skip to content

Commit 63001b2

Browse files
Merge pull request #4 from vladjerca/feat/formatter
feature / query logging with SQL formatting
2 parents 95c18bd + 1bffcfc commit 63001b2

File tree

7 files changed

+34
-21
lines changed

7 files changed

+34
-21
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

33
A beautiful, enhanced logger for Drizzle ORM that transforms your SQL queries into visually appealing, color-coded output with syntax highlighting, icons, and detailed formatting.
44

5-
## 📸 Before vs After
6-
7-
### Before (Default Drizzle Logger)
8-
![Before - Default Drizzle Logger](./images/before.png)
9-
10-
### After (Enhanced Query Logger)
11-
![After - Enhanced Query Logger](./images/after.png)
12-
13-
*See the dramatic difference! The enhanced logger transforms plain SQL output into beautifully formatted, color-coded queries with syntax highlighting, icons, and structured parameter display.*
14-
155
## ✨ Features
166

177
- 🎨 **Beautiful formatting** with box-drawing characters and colors
@@ -41,6 +31,16 @@ pnpm add drizzle-query-logger
4131
bun add drizzle-query-logger
4232
```
4333

34+
## 📸 Before vs After
35+
36+
### Before (Default Drizzle Logger)
37+
![Before - Default Drizzle Logger](./images/before.png)
38+
39+
### After (Enhanced Query Logger)
40+
![After - Enhanced Query Logger](./images/after.jpeg)
41+
42+
*See the dramatic difference! The enhanced logger transforms plain SQL output into beautifully formatted, color-coded queries with syntax highlighting, icons, and structured parameter display.*
43+
4444
## 🚀 Usage
4545

4646
### Basic Usage

bun.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"workspaces": {
44
"": {
55
"name": "drizzle-query-logger",
6+
"dependencies": {
7+
"@sqltools/formatter": "^1.2.5",
8+
},
69
"devDependencies": {
710
"@types/bun": "latest",
811
"tsup": "^8.5.0",
@@ -118,6 +121,8 @@
118121

119122
"@rollup/rollup-win32-x64-msvc": ["@rollup/[email protected]", "", { "os": "win32", "cpu": "x64" }, "sha512-HwHCH5GQTOeGYP5wBEBXFVhfQecwRl24Rugoqhh8YwGarsU09bHhOKuqlyW4ZolZCan3eTUax7UJbGSmKSM51A=="],
120123

124+
"@sqltools/formatter": ["@sqltools/[email protected]", "", {}, "sha512-Uy0+khmZqUrUGm5dmMqVlnvufZRSK0FbYzVgp0UMstm+F5+W2/jnEEQyc9vo1ZR/E5ZI/B1WjjoTqBqwJL6Krw=="],
125+
121126
"@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.2.19" } }, "sha512-d9ZCmrH3CJ2uYKXQIUuZ/pUnTqIvLDS0SK7pFmbx8ma+ziH/FRMoAq5bYpRG7y+w1gl+HgyNZbtqgMq4W4e2Lg=="],
122127

123128
"@types/estree": ["@types/[email protected]", "", {}, "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w=="],

images/after.jpeg

398 KB
Loading

images/after.png

-331 KB
Binary file not shown.

images/before.png

-101 KB
Loading

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@
4343
"query",
4444
"logging"
4545
],
46-
"license": "MIT"
46+
"license": "MIT",
47+
"dependencies": {
48+
"@sqltools/formatter": "^1.2.5"
49+
}
4750
}

src/enhanced-query-logger.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { format } from "@sqltools/formatter";
12
import type { Logger } from "drizzle-orm/logger";
23
import { colors } from "./colors";
34

@@ -32,9 +33,10 @@ export class EnhancedQueryLogger implements Logger {
3233
];
3334

3435
for (const pattern of patterns) {
35-
const match = query.match(pattern);
36-
if (match) return match[1];
36+
const [match] = query.match(pattern) ?? [];
37+
return match ?? null;
3738
}
39+
3840
return null;
3941
}
4042

@@ -104,16 +106,16 @@ export class EnhancedQueryLogger implements Logger {
104106
return `${colors.dim}$${index + 1}:${colors.reset} ${value}`;
105107
});
106108

107-
return `\n${colors.gray} ├─ Parameters: ${
108-
colors.reset
109-
}${formattedParams.join(", ")}`;
109+
return `${colors.gray}├─ Parameters: ${colors.reset}${
110+
formattedParams.join(", ")
111+
}`;
110112
}
111113

112114
private formatQuery(query: string): string {
113115
return query
114116
.replace(
115117
/\b(SELECT|FROM|WHERE|JOIN|INSERT|INTO|UPDATE|SET|DELETE|CREATE|DROP|ALTER|TABLE|INDEX|PRIMARY|KEY|FOREIGN|REFERENCES|NOT|NULL|DEFAULT|UNIQUE|AUTO_INCREMENT|IF|EXISTS|ON|DUPLICATE|KEY|UPDATE|VALUES|ORDER|BY|GROUP|HAVING|LIMIT|OFFSET|INNER|LEFT|RIGHT|OUTER|UNION|CASE|WHEN|THEN|ELSE|END|AS|DISTINCT|COUNT|SUM|AVG|MAX|MIN|AND|OR|IN|LIKE|BETWEEN|IS)\b/gi,
116-
match => `${colors.blue}${match.toUpperCase()}${colors.reset}`
118+
(match) => `${colors.blue}${match.toUpperCase()}${colors.reset}`,
117119
)
118120
.replace(/('[^']*'|"[^"]*")/g, `${colors.green}$1${colors.reset}`)
119121
.replace(/\b(\d+)\b/g, `${colors.cyan}$1${colors.reset}`);
@@ -136,7 +138,10 @@ export class EnhancedQueryLogger implements Logger {
136138
const typeColor = this.getQueryTypeColor(queryType);
137139
const icon = this.getQueryTypeIcon(queryType);
138140
const paramsStr = this.formatParams(params);
139-
const formattedQuery = this.formatQuery(query);
141+
const formattedQuery = this.formatQuery(
142+
`\n${format(query)}`
143+
.replaceAll("\n", `\n${colors.gray}│\t`),
144+
);
140145

141146
const header = `${colors.bright}${colors.cyan}╭─ Database Query ${colors.dim}#${this.queryCount}${colors.reset}`;
142147
const timeInfo = `${colors.gray}${colors.dim}Time: ${timestamp}${colors.reset}`;
@@ -149,15 +154,15 @@ export class EnhancedQueryLogger implements Logger {
149154
}`;
150155
const queryLine = `${colors.gray}${colors.dim}SQL:${colors.reset} ${formattedQuery}`;
151156
const footer = `${colors.gray}╰─${colors.dim}${"─".repeat(50)}${
152-
colors.reset
153-
}`;
157+
colors.reset}
158+
`;
154159

155160
this.options.log("\n" + header);
156161
this.options.log(timeInfo);
157162
this.options.log(queryInfo);
158163
this.options.log(queryLine);
159164
if (paramsStr) {
160-
this.options.log(`${colors.gray} ${paramsStr}`);
165+
this.options.log(paramsStr);
161166
}
162167
this.options.log(footer);
163168

0 commit comments

Comments
 (0)