From a5ab05303fba72bca5a26223acca16ee0474ffa4 Mon Sep 17 00:00:00 2001 From: Anthony GAEREMYNCK <1@anthony.sh> Date: Wed, 29 Jul 2026 14:09:46 +0200 Subject: [PATCH] feat: add MariaDB schema and connection pool Co-Authored-By: Claude Sonnet 5 --- db/schema.sql | 19 +++++++++++++++++++ src/db.js | 23 +++++++++++++++++++++++ test/db.test.js | 26 ++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 db/schema.sql create mode 100644 src/db.js create mode 100644 test/db.test.js diff --git a/db/schema.sql b/db/schema.sql new file mode 100644 index 0000000..3388d9b --- /dev/null +++ b/db/schema.sql @@ -0,0 +1,19 @@ +CREATE TABLE IF NOT EXISTS conversion_jobs ( + id CHAR(36) NOT NULL PRIMARY KEY, + status ENUM('pending', 'processing', 'done', 'failed') NOT NULL DEFAULT 'pending', + family VARCHAR(32) NOT NULL, + source_format VARCHAR(16) NOT NULL, + target_format VARCHAR(16) NOT NULL, + original_filename VARCHAR(255) NOT NULL, + input_path VARCHAR(255) NOT NULL, + output_path VARCHAR(255) NULL, + input_mime_type VARCHAR(128) NOT NULL, + output_mime_type VARCHAR(128) NULL, + error_message VARCHAR(255) NULL, + error_log TEXT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + expires_at DATETIME NOT NULL, + INDEX idx_status (status), + INDEX idx_expires_at (expires_at) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/src/db.js b/src/db.js new file mode 100644 index 0000000..bbc8af8 --- /dev/null +++ b/src/db.js @@ -0,0 +1,23 @@ +import mariadb from 'mariadb'; + +let pool; + +export function getPool(config) { + if (!pool) { + pool = mariadb.createPool({ + host: config.db.host, + user: config.db.user, + password: config.db.password, + database: config.db.database, + connectionLimit: 10, + }); + } + return pool; +} + +export async function closePool() { + if (pool) { + await pool.end(); + pool = undefined; + } +} diff --git a/test/db.test.js b/test/db.test.js new file mode 100644 index 0000000..a2c3560 --- /dev/null +++ b/test/db.test.js @@ -0,0 +1,26 @@ +import { describe, it, expect, afterAll } from 'vitest'; +import { getPool, closePool } from '../src/db.js'; +import { loadConfig } from '../src/config.js'; + +describe('getPool', () => { + afterAll(async () => { + await closePool(); + }); + + it('returns a working pool that can run a query', async () => { + const config = loadConfig(); + const pool = getPool(config); + + const rows = await pool.query('SELECT 1 AS value'); + + expect(Number(rows[0].value)).toBe(1); + }); + + it('returns the same pool instance on repeated calls', () => { + const config = loadConfig(); + const poolA = getPool(config); + const poolB = getPool(config); + + expect(poolA).toBe(poolB); + }); +});