Skip to content
Merged
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
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@

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.

## 📸 Before vs After

### Before (Default Drizzle Logger)
![Before - Default Drizzle Logger](./images/before.png)

### After (Enhanced Query Logger)
![After - Enhanced Query Logger](./images/after.png)

*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.*

## ✨ Features

- 🎨 **Beautiful formatting** with box-drawing characters and colors
Expand Down Expand Up @@ -41,6 +31,16 @@ pnpm add drizzle-query-logger
bun add drizzle-query-logger
```

## 📸 Before vs After

### Before (Default Drizzle Logger)
![Before - Default Drizzle Logger](./images/before.png)

### After (Enhanced Query Logger)
![After - Enhanced Query Logger](./images/after.jpeg)

*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.*

## 🚀 Usage

### Basic Usage
Expand Down
5 changes: 5 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"workspaces": {
"": {
"name": "drizzle-query-logger",
"dependencies": {
"@sqltools/formatter": "^1.2.5",
},
"devDependencies": {
"@types/bun": "latest",
"tsup": "^8.5.0",
Expand Down Expand Up @@ -118,6 +121,8 @@

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

"@sqltools/formatter": ["@sqltools/[email protected]", "", {}, "sha512-Uy0+khmZqUrUGm5dmMqVlnvufZRSK0FbYzVgp0UMstm+F5+W2/jnEEQyc9vo1ZR/E5ZI/B1WjjoTqBqwJL6Krw=="],

"@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.2.19" } }, "sha512-d9ZCmrH3CJ2uYKXQIUuZ/pUnTqIvLDS0SK7pFmbx8ma+ziH/FRMoAq5bYpRG7y+w1gl+HgyNZbtqgMq4W4e2Lg=="],

"@types/estree": ["@types/[email protected]", "", {}, "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w=="],
Expand Down
Binary file added images/after.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/after.png
Binary file not shown.
Binary file modified images/before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
"query",
"logging"
],
"license": "MIT"
"license": "MIT",
"dependencies": {
"@sqltools/formatter": "^1.2.5"
}
}
25 changes: 15 additions & 10 deletions src/enhanced-query-logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { format } from "@sqltools/formatter";
import type { Logger } from "drizzle-orm/logger";
import { colors } from "./colors";

Expand Down Expand Up @@ -32,9 +33,10 @@ export class EnhancedQueryLogger implements Logger {
];

for (const pattern of patterns) {
const match = query.match(pattern);
if (match) return match[1];
const [match] = query.match(pattern) ?? [];
return match ?? null;
}

return null;
}

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

return `\n${colors.gray} ├─ Parameters: ${
colors.reset
}${formattedParams.join(", ")}`;
return `${colors.gray}├─ Parameters: ${colors.reset}${
formattedParams.join(", ")
}`;
}

private formatQuery(query: string): string {
return query
.replace(
/\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,
match => `${colors.blue}${match.toUpperCase()}${colors.reset}`
(match) => `${colors.blue}${match.toUpperCase()}${colors.reset}`,
)
.replace(/('[^']*'|"[^"]*")/g, `${colors.green}$1${colors.reset}`)
.replace(/\b(\d+)\b/g, `${colors.cyan}$1${colors.reset}`);
Expand All @@ -136,7 +138,10 @@ export class EnhancedQueryLogger implements Logger {
const typeColor = this.getQueryTypeColor(queryType);
const icon = this.getQueryTypeIcon(queryType);
const paramsStr = this.formatParams(params);
const formattedQuery = this.formatQuery(query);
const formattedQuery = this.formatQuery(
`\n${format(query)}`
.replaceAll("\n", `\n${colors.gray}│\t`),
);

const header = `${colors.bright}${colors.cyan}╭─ Database Query ${colors.dim}#${this.queryCount}${colors.reset}`;
const timeInfo = `${colors.gray}│ ${colors.dim}Time: ${timestamp}${colors.reset}`;
Expand All @@ -149,15 +154,15 @@ export class EnhancedQueryLogger implements Logger {
}`;
const queryLine = `${colors.gray}│ ${colors.dim}SQL:${colors.reset} ${formattedQuery}`;
const footer = `${colors.gray}╰─${colors.dim}${"─".repeat(50)}${
colors.reset
}`;
colors.reset}
`;

this.options.log("\n" + header);
this.options.log(timeInfo);
this.options.log(queryInfo);
this.options.log(queryLine);
if (paramsStr) {
this.options.log(`${colors.gray} ${paramsStr}`);
this.options.log(paramsStr);
}
this.options.log(footer);

Expand Down