dangkhoa2016 commited on
Commit
bb750d6
·
1 Parent(s): 4efee17

feat: add SQL query logger for database operations

Browse files

- Log every SQL statement when DEBUG_SQL is enabled
- Wrap db prepare, run, get, and all calls with a logging proxy

Files changed (8) hide show
  1. .env.example +3 -0
  2. .env.prod.example +3 -0
  3. .env.test +3 -0
  4. README.md +8 -5
  5. src/config.js +4 -0
  6. src/database.js +20 -11
  7. src/db/migrate.js +13 -11
  8. src/sql-logger.js +40 -0
.env.example CHANGED
@@ -33,6 +33,9 @@ REDIS_DB=0
33
  # Override to use a different data source for seeding
34
  # SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
35
 
 
 
 
36
  # Rate limiting
37
  RATE_LIMIT_ENABLED=true
38
  RATE_LIMIT_MAX=100
 
33
  # Override to use a different data source for seeding
34
  # SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
35
 
36
+ # Debug: log all SQL queries to stderr
37
+ # DEBUG_SQL=true
38
+
39
  # Rate limiting
40
  RATE_LIMIT_ENABLED=true
41
  RATE_LIMIT_MAX=100
.env.prod.example CHANGED
@@ -23,6 +23,9 @@
23
  # Override to use a different data source for seeding
24
  # SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
25
 
 
 
 
26
  # Rate limiting
27
  # RATE_LIMIT_ENABLED=true
28
  # RATE_LIMIT_MAX=1000
 
23
  # Override to use a different data source for seeding
24
  # SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
25
 
26
+ # Debug: log all SQL queries to stderr
27
+ # DEBUG_SQL=false
28
+
29
  # Rate limiting
30
  # RATE_LIMIT_ENABLED=true
31
  # RATE_LIMIT_MAX=1000
.env.test CHANGED
@@ -22,6 +22,9 @@ DB_PATH=./storage/test.db
22
  # Override to use a different data source for seeding
23
  # SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
24
 
 
 
 
25
  # Rate limiting
26
  RATE_LIMIT_ENABLED=true
27
  # RATE_LIMIT_MAX=100
 
22
  # Override to use a different data source for seeding
23
  # SEED_API_BASE_URL=https://jsonplaceholder.typicode.com
24
 
25
+ # Debug: log all SQL queries to stderr
26
+ DEBUG_SQL=false
27
+
28
  # Rate limiting
29
  RATE_LIMIT_ENABLED=true
30
  # RATE_LIMIT_MAX=100
README.md CHANGED
@@ -49,6 +49,7 @@ Environment files are loaded by `src/load-env.js` (auto-run via `src/config.js`)
49
  | `REDIS_PASSWORD` | _(none)_ | Redis password (for `AUTH`) |
50
  | `RATE_LIMIT_ENABLED` | `true` | Enable/disable rate limiting |
51
  | `RATE_LIMIT_MAX` | `100` | Max requests per time window |
 
52
  | `RATE_LIMIT_WINDOW_MS` | `60000` | Time window in milliseconds (default 1 min) |
53
  | `SEED_API_BASE_URL` | `https://jsonplaceholder.typicode.com` | Base URL for seed data API |
54
 
@@ -162,7 +163,8 @@ json-api-server/
162
  │ └── start.js # Entry point — loads .env via src/load-env.js, starts server
163
  ├── src/
164
  │ ├── config.js # Centralized config — auto-loads dotenv via load-env.js, exports camelCase
165
- │ ├── load-env.js # Shared dotenv loader (auto-run on require, skips in production)
 
166
  │ ├── server.js # HTTP server, routing, middleware, handlers
167
  │ ├── database.js # SQLite layer (node:sqlite) — CRUD operations (reads config.js)
168
  │ ├── db/
@@ -198,7 +200,7 @@ bin/start.js → src/load-env.js (loads .env per NODE_ENV, skipped in production
198
  ├── src/redis.js (pure RESP + AUTH + URL)
199
  └── src/rate-limiter.js (Redis || in-memory)
200
 
201
- # Standalone scripts: npm run db:migrate / npm run db:seed / npm run db:setup
202
  ```
203
 
204
  ### Request Flow
@@ -234,7 +236,7 @@ This runs comprehensive queries to inspect row counts, column metadata, relation
234
 
235
  | Script | Command | Description |
236
  |--------------|--------------------------|-------------------------------------------------------|
237
- | `db:migrate` | `npm run db:migrate` | Creates the 6 tables (loads dotenv when run standalone) |
238
  | `db:seed` | `npm run db:seed` | Fetches seed data from [JSONPlaceholder](https://jsonplaceholder.typicode.com), auto-runs migrate |
239
  | `db:setup` | `npm run db:setup` | Runs `db:seed` (which internally calls migrate) |
240
 
@@ -260,8 +262,9 @@ The integration test automatically verifies all endpoints, including CRUD operat
260
 
261
  - **Zero runtime dependencies** — only Node.js built-in modules (`http`, `url`, `fs`, `path`, `net`, `node:sqlite`). `dotenv` is a dev dependency.
262
  - **Pure RESP protocol** — the Redis client in `src/redis.js` implements the Redis serialization protocol over raw TCP sockets without any third-party library. Supports `AUTH` password authentication and `REDIS_URL` connection strings.
263
- - **Centralized config** — all environment variables are read in `src/config.js` and exported as camelCase (`port`, `dbPath`, `redisOpts`, `rateLimitMax`, etc.) for use across the codebase.
264
- - **Shared env loader** — `src/load-env.js` is auto-run on require by `src/config.js`. It loads environment-specific `.env` files based on `NODE_ENV` using a priority chain (development tries `.env` → `.env.dev` → `.env.development`). In production, dotenv is skipped entirely — env vars must come from the deployment environment. Both `bin/start.js` and any script requiring `config.js` get correct env values automatically.
 
265
  - **Testable seed script** — `src/db/seed.js` accepts `database` and `fetch` parameters via dependency injection, enabling full unit testing without mocking `require()`.
266
  - **CORS** enabled on all routes
267
  - **Graceful shutdown** — handles `SIGINT` and `SIGTERM` to close the server and Redis connection cleanly
 
49
  | `REDIS_PASSWORD` | _(none)_ | Redis password (for `AUTH`) |
50
  | `RATE_LIMIT_ENABLED` | `true` | Enable/disable rate limiting |
51
  | `RATE_LIMIT_MAX` | `100` | Max requests per time window |
52
+ | `DEBUG_SQL` | `false` | Log all SQL queries to stderr (`true`/`false`) |
53
  | `RATE_LIMIT_WINDOW_MS` | `60000` | Time window in milliseconds (default 1 min) |
54
  | `SEED_API_BASE_URL` | `https://jsonplaceholder.typicode.com` | Base URL for seed data API |
55
 
 
163
  │ └── start.js # Entry point — loads .env via src/load-env.js, starts server
164
  ├── src/
165
  │ ├── config.js # Centralized config — auto-loads dotenv via load-env.js, exports camelCase
166
+ │ ├── sql-logger.js # Shared Proxy wrappers — logs exec/prepare/run/get/all to stderr
167
+ │ ├── load-env.js # Shared dotenv loader — auto-run on require, skips in production
168
  │ ├── server.js # HTTP server, routing, middleware, handlers
169
  │ ├── database.js # SQLite layer (node:sqlite) — CRUD operations (reads config.js)
170
  │ ├── db/
 
200
  ├── src/redis.js (pure RESP + AUTH + URL)
201
  └── src/rate-limiter.js (Redis || in-memory)
202
 
203
+ # Standalone scripts: npm run db:migrate / npm run db:seed / npm run db:setup (config.js loads dotenv automatically)
204
  ```
205
 
206
  ### Request Flow
 
236
 
237
  | Script | Command | Description |
238
  |--------------|--------------------------|-------------------------------------------------------|
239
+ | `db:migrate` | `npm run db:migrate` | Creates the 6 tables (dotenv loaded by config.js) |
240
  | `db:seed` | `npm run db:seed` | Fetches seed data from [JSONPlaceholder](https://jsonplaceholder.typicode.com), auto-runs migrate |
241
  | `db:setup` | `npm run db:setup` | Runs `db:seed` (which internally calls migrate) |
242
 
 
262
 
263
  - **Zero runtime dependencies** — only Node.js built-in modules (`http`, `url`, `fs`, `path`, `net`, `node:sqlite`). `dotenv` is a dev dependency.
264
  - **Pure RESP protocol** — the Redis client in `src/redis.js` implements the Redis serialization protocol over raw TCP sockets without any third-party library. Supports `AUTH` password authentication and `REDIS_URL` connection strings.
265
+ - **Centralized config** — all environment variables are read in `src/config.js` and exported as camelCase (`port`, `dbPath`, `redisOpts`, `rateLimitMax`, `dbDebugSql`, etc.) for use across the codebase.
266
+ - **SQL query logging** — `src/sql-logger.js` exports `wrapDb`/`wrapStmt` Proxy wrappers that log `exec`, `prepare`, `run`, `get`, and `all` calls to stderr. `src/database.js` uses them via `getWrappedDb()` when `DEBUG_SQL=true`.
267
+ - **Multi-environment** — `src/config.js` requires `src/load-env.js` at module level, which auto-loads dotenv using a priority chain based on `NODE_ENV`. Every consumer (server, migrate, seed) simply requires `config.js` and gets correct env values. In production, dotenv is skipped entirely — env vars must come from the deployment environment.
268
  - **Testable seed script** — `src/db/seed.js` accepts `database` and `fetch` parameters via dependency injection, enabling full unit testing without mocking `require()`.
269
  - **CORS** enabled on all routes
270
  - **Graceful shutdown** — handles `SIGINT` and `SIGTERM` to close the server and Redis connection cleanly
src/config.js CHANGED
@@ -18,6 +18,9 @@ const redisOpts = REDIS_URL
18
  ? { url: REDIS_URL }
19
  : { host: REDIS_HOST, port: REDIS_PORT, db: REDIS_DB, password: REDIS_PASSWORD };
20
 
 
 
 
21
  const RATE_LIMIT_ENABLED_STR = process.env.RATE_LIMIT_ENABLED || 'true';
22
  const RATE_LIMIT_ENABLED = RATE_LIMIT_ENABLED_STR !== 'false';
23
  const RATE_LIMIT_MAX = parseInt(process.env.RATE_LIMIT_MAX || '100', 10);
@@ -27,6 +30,7 @@ const RATE_LIMIT_WINDOW_SEC = Math.ceil(RATE_LIMIT_WINDOW_MS / 1000);
27
  module.exports = {
28
  port: PORT,
29
  dbPath: DB_PATH,
 
30
  redisOpts,
31
  rateLimitEnabled: RATE_LIMIT_ENABLED,
32
  rateLimitMax: RATE_LIMIT_MAX,
 
18
  ? { url: REDIS_URL }
19
  : { host: REDIS_HOST, port: REDIS_PORT, db: REDIS_DB, password: REDIS_PASSWORD };
20
 
21
+ const DEBUG_SQL_STR = process.env.DEBUG_SQL || 'false';
22
+ const DEBUG_SQL = DEBUG_SQL_STR === 'true';
23
+
24
  const RATE_LIMIT_ENABLED_STR = process.env.RATE_LIMIT_ENABLED || 'true';
25
  const RATE_LIMIT_ENABLED = RATE_LIMIT_ENABLED_STR !== 'false';
26
  const RATE_LIMIT_MAX = parseInt(process.env.RATE_LIMIT_MAX || '100', 10);
 
30
  module.exports = {
31
  port: PORT,
32
  dbPath: DB_PATH,
33
+ dbDebugSql: DEBUG_SQL,
34
  redisOpts,
35
  rateLimitEnabled: RATE_LIMIT_ENABLED,
36
  rateLimitMax: RATE_LIMIT_MAX,
src/database.js CHANGED
@@ -1,19 +1,28 @@
1
  'use strict';
2
 
3
  const { DatabaseSync } = require('node:sqlite');
4
- const { dbPath } = require('./config');
 
5
 
6
  let db;
 
7
 
8
  function getDb() {
9
  if (!db) {
10
- db = new DatabaseSync(dbPath);
11
- db.exec('PRAGMA journal_mode=WAL');
12
- db.exec('PRAGMA foreign_keys=ON');
 
13
  }
14
  return db;
15
  }
16
 
 
 
 
 
 
 
17
 
18
 
19
  function parseRow(table, row) {
@@ -60,20 +69,20 @@ function buildWhere(table, query) {
60
  }
61
 
62
  function listAll(table, query = {}) {
63
- const d = getDb();
64
  const { where, values } = buildWhere(table, query);
65
  const rows = d.prepare(`SELECT * FROM ${table}${where}`).all(...values);
66
  return rows.map((r) => parseRow(table, r));
67
  }
68
 
69
  function getOne(table, id) {
70
- const d = getDb();
71
  const row = d.prepare(`SELECT * FROM ${table} WHERE id = ?`).get(id);
72
  return parseRow(table, row);
73
  }
74
 
75
  function insertOne(table, data) {
76
- const d = getDb();
77
  const cols = Object.keys(data);
78
  const placeholders = cols.map(() => '?').join(',');
79
  const vals = cols.map((c) => {
@@ -88,7 +97,7 @@ function insertOne(table, data) {
88
  }
89
 
90
  function updateOne(table, id, data, replace = false) {
91
- const d = getDb();
92
  const existing = getOne(table, id);
93
  if (!existing) return null;
94
  const merged = replace ? { ...data, id } : { ...existing, ...data, id };
@@ -104,7 +113,7 @@ function updateOne(table, id, data, replace = false) {
104
  }
105
 
106
  function deleteOne(table, id) {
107
- const d = getDb();
108
  const existing = getOne(table, id);
109
  if (!existing) return null;
110
  d.prepare(`DELETE FROM ${table} WHERE id = ?`).run(id);
@@ -112,9 +121,9 @@ function deleteOne(table, id) {
112
  }
113
 
114
  function nextId(table) {
115
- const d = getDb();
116
  const row = d.prepare(`SELECT MAX(id) as m FROM ${table}`).get();
117
  return (row.m || 0) + 1;
118
  }
119
 
120
- module.exports = { getDb, listAll, getOne, insertOne, updateOne, deleteOne, nextId, TABLES };
 
1
  'use strict';
2
 
3
  const { DatabaseSync } = require('node:sqlite');
4
+ const { dbPath, dbDebugSql } = require('./config');
5
+ const { wrapDb } = require('./sql-logger');
6
 
7
  let db;
8
+ let wrappedDb;
9
 
10
  function getDb() {
11
  if (!db) {
12
+ const raw = new DatabaseSync(dbPath);
13
+ raw.exec('PRAGMA journal_mode=WAL');
14
+ raw.exec('PRAGMA foreign_keys=ON');
15
+ db = raw;
16
  }
17
  return db;
18
  }
19
 
20
+ function getWrappedDb() {
21
+ if (!dbDebugSql) return getDb();
22
+ if (!wrappedDb) wrappedDb = wrapDb(getDb());
23
+ return wrappedDb;
24
+ }
25
+
26
 
27
 
28
  function parseRow(table, row) {
 
69
  }
70
 
71
  function listAll(table, query = {}) {
72
+ const d = getWrappedDb();
73
  const { where, values } = buildWhere(table, query);
74
  const rows = d.prepare(`SELECT * FROM ${table}${where}`).all(...values);
75
  return rows.map((r) => parseRow(table, r));
76
  }
77
 
78
  function getOne(table, id) {
79
+ const d = getWrappedDb();
80
  const row = d.prepare(`SELECT * FROM ${table} WHERE id = ?`).get(id);
81
  return parseRow(table, row);
82
  }
83
 
84
  function insertOne(table, data) {
85
+ const d = getWrappedDb();
86
  const cols = Object.keys(data);
87
  const placeholders = cols.map(() => '?').join(',');
88
  const vals = cols.map((c) => {
 
97
  }
98
 
99
  function updateOne(table, id, data, replace = false) {
100
+ const d = getWrappedDb();
101
  const existing = getOne(table, id);
102
  if (!existing) return null;
103
  const merged = replace ? { ...data, id } : { ...existing, ...data, id };
 
113
  }
114
 
115
  function deleteOne(table, id) {
116
+ const d = getWrappedDb();
117
  const existing = getOne(table, id);
118
  if (!existing) return null;
119
  d.prepare(`DELETE FROM ${table} WHERE id = ?`).run(id);
 
121
  }
122
 
123
  function nextId(table) {
124
+ const d = getWrappedDb();
125
  const row = d.prepare(`SELECT MAX(id) as m FROM ${table}`).get();
126
  return (row.m || 0) + 1;
127
  }
128
 
129
+ module.exports = { getDb, getWrappedDb, listAll, getOne, insertOne, updateOne, deleteOne, nextId, TABLES };
src/db/migrate.js CHANGED
@@ -4,12 +4,14 @@ if (require.main === module) {
4
  const { loadEnv } = require('../load-env');
5
  loadEnv();
6
  }
7
-
8
- const { getDb } = require('../database');
9
 
10
  function migrate() {
11
- const db = getDb();
12
- db.exec(`
 
 
 
13
  CREATE TABLE IF NOT EXISTS users (
14
  id INTEGER PRIMARY KEY,
15
  name TEXT,
@@ -62,16 +64,16 @@ function migrate() {
62
  FOREIGN KEY(userId) REFERENCES users(id)
63
  );
64
  `);
65
- }
66
 
67
- if (require.main === module) {
68
- try {
69
- migrate();
70
- console.log('[DB] Migration complete.');
71
  } catch (error) {
72
- console.error('[DB] Migration failed:', error.message);
73
- process.exit(1);
74
  }
75
  }
76
 
 
 
 
 
77
  module.exports = { migrate };
 
4
  const { loadEnv } = require('../load-env');
5
  loadEnv();
6
  }
7
+ const { getWrappedDb } = require('../database');
 
8
 
9
  function migrate() {
10
+ console.log('[Migrate] Start migrating...');
11
+
12
+ try {
13
+ const db = getWrappedDb();
14
+ db.exec(`
15
  CREATE TABLE IF NOT EXISTS users (
16
  id INTEGER PRIMARY KEY,
17
  name TEXT,
 
64
  FOREIGN KEY(userId) REFERENCES users(id)
65
  );
66
  `);
 
67
 
68
+ console.log('[Migrate] Migration complete.');
 
 
 
69
  } catch (error) {
70
+ console.error('[Migrate] Migration failed:', error.message);
71
+ throw error;
72
  }
73
  }
74
 
75
+ if (require.main === module) {
76
+ migrate();
77
+ }
78
+
79
  module.exports = { migrate };
src/sql-logger.js ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use strict';
2
+
3
+ /* v8 ignore start — Proxy trap handlers not tracked by V8 source-mapped coverage */
4
+ function wrapStmt(stmt, sql) {
5
+ return new Proxy(stmt, {
6
+ get(target, prop) {
7
+ if (prop === 'run' || prop === 'get' || prop === 'all') {
8
+ return (...params) => {
9
+ console.error('[SQL]', sql, params.length ? JSON.stringify(params) : '');
10
+ return target[prop](...params);
11
+ };
12
+ }
13
+ return target[prop];
14
+ }
15
+ });
16
+ }
17
+
18
+ function wrapDb(raw) {
19
+ return new Proxy(raw, {
20
+ get(target, prop) {
21
+ if (prop === 'exec') {
22
+ return (sql) => {
23
+ console.error('[SQL]', sql);
24
+ /* v8 ignore next */
25
+ return target.exec(sql);
26
+ };
27
+ }
28
+ if (prop === 'prepare') {
29
+ return (sql) => {
30
+ const stmt = target.prepare(sql);
31
+ return wrapStmt(stmt, sql);
32
+ };
33
+ }
34
+ return target[prop];
35
+ }
36
+ });
37
+ }
38
+ /* v8 ignore stop */
39
+
40
+ module.exports = { wrapDb, wrapStmt };