Add buildDatabaseUrl and a CLI helper to print it
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
import { loadConfig } from '../src/config.js';
|
||||
import { buildDatabaseUrl } from '../src/db.js';
|
||||
|
||||
console.log(buildDatabaseUrl(loadConfig()));
|
||||
@@ -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({
|
||||
|
||||
+19
-1
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user