From 56b73ff160ffa092cb7ca2f5c6f96175f7eb85fb Mon Sep 17 00:00:00 2001 From: "Beomhee.Lee" Date: Fri, 31 Jul 2026 12:48:40 +0900 Subject: [PATCH] =?UTF-8?q?DB=20=EC=A7=88=EC=9D=98=20=EB=A1=9C=EA=B7=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/database.ts | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/lib/database.ts b/src/lib/database.ts index d4c0848..a2ce8b2 100644 --- a/src/lib/database.ts +++ b/src/lib/database.ts @@ -38,15 +38,35 @@ async function connectWithRetry(maxRetries: number = 10, baseDelay: number = 100 throw new Error('Max retries exceeded'); } +function wrapPoolWithWriteLogging(pool: pg.Pool): pg.Pool { + const originalQuery = pool.query.bind(pool); + + pool.query = ((text: any, values?: any, callback?: any) => { + const sqlText = typeof text === 'string' ? text : text?.text; + const isWriteQuery = typeof sqlText === 'string' && /^(INSERT|UPDATE|DELETE)\b/i.test(sqlText.trim()); + + if (isWriteQuery) { + console.info('[DB WRITE]', { + sql: sqlText, + params: values ?? null, + }); + } + + return originalQuery(text as any, values as any, callback as any); + }) as typeof pool.query; + + return pool; +} + // Initialize database schema export async function initializeDatabase(options: DatabaseOptions = {}) { // Initialize pool - db = new Pool({ + db = wrapPoolWithWriteLogging(new Pool({ connectionString: options.url || process.env.DATABASE_URL || 'postgresql://postgres:password@localhost:5432/linkforty', ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false, min: options.pool?.min || 2, max: options.pool?.max || 10, - }); + })); const client = await connectWithRetry();