diff --git a/README.md b/README.md index 2baae4e..968edad 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/bun.lock b/bun.lock index 97b267e..e43bd5e 100644 --- a/bun.lock +++ b/bun.lock @@ -3,6 +3,9 @@ "workspaces": { "": { "name": "drizzle-query-logger", + "dependencies": { + "@sqltools/formatter": "^1.2.5", + }, "devDependencies": { "@types/bun": "latest", "tsup": "^8.5.0", @@ -118,6 +121,8 @@ "@rollup/rollup-win32-x64-msvc": ["@rollup/rollup-win32-x64-msvc@4.45.3", "", { "os": "win32", "cpu": "x64" }, "sha512-HwHCH5GQTOeGYP5wBEBXFVhfQecwRl24Rugoqhh8YwGarsU09bHhOKuqlyW4ZolZCan3eTUax7UJbGSmKSM51A=="], + "@sqltools/formatter": ["@sqltools/formatter@1.2.5", "", {}, "sha512-Uy0+khmZqUrUGm5dmMqVlnvufZRSK0FbYzVgp0UMstm+F5+W2/jnEEQyc9vo1ZR/E5ZI/B1WjjoTqBqwJL6Krw=="], + "@types/bun": ["@types/bun@1.2.19", "", { "dependencies": { "bun-types": "1.2.19" } }, "sha512-d9ZCmrH3CJ2uYKXQIUuZ/pUnTqIvLDS0SK7pFmbx8ma+ziH/FRMoAq5bYpRG7y+w1gl+HgyNZbtqgMq4W4e2Lg=="], "@types/estree": ["@types/estree@1.0.8", "", {}, "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w=="], diff --git a/images/after.jpeg b/images/after.jpeg new file mode 100644 index 0000000..b87fd2f Binary files /dev/null and b/images/after.jpeg differ diff --git a/images/after.png b/images/after.png deleted file mode 100644 index da2eed6..0000000 Binary files a/images/after.png and /dev/null differ diff --git a/images/before.png b/images/before.png index 665aab9..8238a71 100644 Binary files a/images/before.png and b/images/before.png differ diff --git a/package.json b/package.json index 12cfd61..984e830 100644 --- a/package.json +++ b/package.json @@ -43,5 +43,8 @@ "query", "logging" ], - "license": "MIT" + "license": "MIT", + "dependencies": { + "@sqltools/formatter": "^1.2.5" + } } diff --git a/src/enhanced-query-logger.ts b/src/enhanced-query-logger.ts index f8ba14b..0827e04 100644 --- a/src/enhanced-query-logger.ts +++ b/src/enhanced-query-logger.ts @@ -1,3 +1,4 @@ +import { format } from "@sqltools/formatter"; import type { Logger } from "drizzle-orm/logger"; import { colors } from "./colors"; @@ -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; } @@ -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}`); @@ -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}`; @@ -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);