From 5e0b832fcee291245186b9f26e36bee14cb84ac7 Mon Sep 17 00:00:00 2001 From: Anthony GAEREMYNCK <1@anthony.sh> Date: Fri, 31 Jul 2026 01:11:35 +0200 Subject: [PATCH] Add buildDatabaseUrl and a CLI helper to print it Co-Authored-By: Claude Sonnet 5 --- scripts/printDatabaseUrl.js | 4 ++++ src/db.js | 5 +++++ test/db.test.js | 20 +++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 scripts/printDatabaseUrl.js diff --git a/scripts/printDatabaseUrl.js b/scripts/printDatabaseUrl.js new file mode 100644 index 0000000..e21ce30 --- /dev/null +++ b/scripts/printDatabaseUrl.js @@ -0,0 +1,4 @@ +import { loadConfig } from '../src/config.js'; +import { buildDatabaseUrl } from '../src/db.js'; + +console.log(buildDatabaseUrl(loadConfig())); diff --git a/src/db.js b/src/db.js index bbc8af8..a630dda 100644 --- a/src/db.js +++ b/src/db.js @@ -2,6 +2,11 @@ import mariadb from 'mariadb'; let pool; +export function buildDatabaseUrl(config) { + const { host, user, password, database } = config.db; + return `mysql://${encodeURIComponent(user)}:${encodeURIComponent(password)}@${host}:3306/${database}?connection_limit=10`; +} + export function getPool(config) { if (!pool) { pool = mariadb.createPool({ diff --git a/test/db.test.js b/test/db.test.js index a2c3560..11e0147 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1,7 +1,25 @@ import { describe, it, expect, afterAll } from 'vitest'; -import { getPool, closePool } from '../src/db.js'; +import { getPool, closePool, buildDatabaseUrl } from '../src/db.js'; import { loadConfig } from '../src/config.js'; +describe('buildDatabaseUrl', () => { + it('builds a mysql connection string from db config', () => { + const url = buildDatabaseUrl({ + db: { host: '127.0.0.1', user: 'convert_user', password: 'change_me', database: 'file_converter' }, + }); + + expect(url).toBe('mysql://convert_user:change_me@127.0.0.1:3306/file_converter?connection_limit=10'); + }); + + it('percent-encodes special characters in user and password', () => { + const url = buildDatabaseUrl({ + db: { host: '127.0.0.1', user: 'a@b', password: 'p@ss:word', database: 'file_converter' }, + }); + + expect(url).toBe('mysql://a%40b:p%40ss%3Aword@127.0.0.1:3306/file_converter?connection_limit=10'); + }); +}); + describe('getPool', () => { afterAll(async () => { await closePool();