Load local MariaDB settings from env

This commit is contained in:
2026-08-05 12:29:38 +02:00
parent 2e7d83b679
commit d7d5c0f912
2 changed files with 31 additions and 10 deletions
+30 -9
View File
@@ -5,6 +5,7 @@ export function loadConfig({ configPath = "config/local.json", dryRun } = {}) {
const localConfig = readJsonFile(configPath); const localConfig = readJsonFile(configPath);
const exampleConfig = readJsonFile("config/local.example.json"); const exampleConfig = readJsonFile("config/local.example.json");
const config = mergeConfig(exampleConfig, localConfig); const config = mergeConfig(exampleConfig, localConfig);
const env = readEnvFile(".env");
const appMode = String(config.appMode || "dry-run").toLowerCase(); const appMode = String(config.appMode || "dry-run").toLowerCase();
const resolvedDryRun = dryRun === undefined ? appMode !== "live" : Boolean(dryRun); const resolvedDryRun = dryRun === undefined ? appMode !== "live" : Boolean(dryRun);
@@ -26,15 +27,15 @@ export function loadConfig({ configPath = "config/local.json", dryRun } = {}) {
password: config.endpoint?.password || "", password: config.endpoint?.password || "",
}, },
database: { database: {
driver: String(config.database?.driver || "endpoint").toLowerCase(), driver: String(env.DB_DRIVER || config.database?.driver || "endpoint").toLowerCase(),
mode: String(config.database?.mode || "local").toLowerCase(), mode: String(env.DB_MODE || config.database?.mode || "local").toLowerCase(),
allowWrites: Boolean(config.database?.allowWrites), allowWrites: parseBoolean(env.DB_ALLOW_WRITES, config.database?.allowWrites),
host: config.database?.host || "127.0.0.1", host: env.DB_HOST || config.database?.host || "127.0.0.1",
port: Number(config.database?.port || 3306), port: Number(env.DB_PORT || config.database?.port || 3306),
name: config.database?.name || "catalog_maker_test", name: env.DB_NAME || config.database?.name || "catalog_maker_test",
user: config.database?.user || "catalog_maker", user: env.DB_USER || config.database?.user || "catalog_maker",
password: config.database?.password || "", password: env.DB_PASSWORD || config.database?.password || "",
connectionLimit: Number(config.database?.connectionLimit || 5), connectionLimit: Number(env.DB_CONNECTION_LIMIT || config.database?.connectionLimit || 5),
}, },
}; };
} }
@@ -47,6 +48,26 @@ function readJsonFile(filePath) {
return JSON.parse(fs.readFileSync(filePath, "utf8")); return JSON.parse(fs.readFileSync(filePath, "utf8"));
} }
function readEnvFile(filePath) {
if (!fs.existsSync(filePath)) return {};
return Object.fromEntries(
fs.readFileSync(filePath, "utf8")
.split(/\r?\n/)
.map((line) => line.trim())
.filter((line) => line && !line.startsWith("#") && line.includes("="))
.map((line) => {
const separator = line.indexOf("=");
return [line.slice(0, separator).trim(), line.slice(separator + 1).trim()];
}),
);
}
function parseBoolean(value, fallback = false) {
if (value === undefined) return Boolean(fallback);
return ["1", "true", "yes", "on"].includes(String(value).toLowerCase());
}
function mergeConfig(base, override) { function mergeConfig(base, override) {
return { return {
...base, ...base,
+1 -1
View File
@@ -57,7 +57,7 @@ export async function executeMariaDbWrite(config, sql) {
} }
} }
async function getPool(config) { function getPool(config) {
if (!pool) { if (!pool) {
pool = mariadb.createPool({ pool = mariadb.createPool({
host: config.database.host, host: config.database.host,