Load local MariaDB settings from env
This commit is contained in:
+30
-9
@@ -5,6 +5,7 @@ export function loadConfig({ configPath = "config/local.json", dryRun } = {}) {
|
||||
const localConfig = readJsonFile(configPath);
|
||||
const exampleConfig = readJsonFile("config/local.example.json");
|
||||
const config = mergeConfig(exampleConfig, localConfig);
|
||||
const env = readEnvFile(".env");
|
||||
|
||||
const appMode = String(config.appMode || "dry-run").toLowerCase();
|
||||
const resolvedDryRun = dryRun === undefined ? appMode !== "live" : Boolean(dryRun);
|
||||
@@ -26,15 +27,15 @@ export function loadConfig({ configPath = "config/local.json", dryRun } = {}) {
|
||||
password: config.endpoint?.password || "",
|
||||
},
|
||||
database: {
|
||||
driver: String(config.database?.driver || "endpoint").toLowerCase(),
|
||||
mode: String(config.database?.mode || "local").toLowerCase(),
|
||||
allowWrites: Boolean(config.database?.allowWrites),
|
||||
host: config.database?.host || "127.0.0.1",
|
||||
port: Number(config.database?.port || 3306),
|
||||
name: config.database?.name || "catalog_maker_test",
|
||||
user: config.database?.user || "catalog_maker",
|
||||
password: config.database?.password || "",
|
||||
connectionLimit: Number(config.database?.connectionLimit || 5),
|
||||
driver: String(env.DB_DRIVER || config.database?.driver || "endpoint").toLowerCase(),
|
||||
mode: String(env.DB_MODE || config.database?.mode || "local").toLowerCase(),
|
||||
allowWrites: parseBoolean(env.DB_ALLOW_WRITES, config.database?.allowWrites),
|
||||
host: env.DB_HOST || config.database?.host || "127.0.0.1",
|
||||
port: Number(env.DB_PORT || config.database?.port || 3306),
|
||||
name: env.DB_NAME || config.database?.name || "catalog_maker_test",
|
||||
user: env.DB_USER || config.database?.user || "catalog_maker",
|
||||
password: env.DB_PASSWORD || config.database?.password || "",
|
||||
connectionLimit: Number(env.DB_CONNECTION_LIMIT || config.database?.connectionLimit || 5),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -47,6 +48,26 @@ function readJsonFile(filePath) {
|
||||
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) {
|
||||
return {
|
||||
...base,
|
||||
|
||||
@@ -57,7 +57,7 @@ export async function executeMariaDbWrite(config, sql) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getPool(config) {
|
||||
function getPool(config) {
|
||||
if (!pool) {
|
||||
pool = mariadb.createPool({
|
||||
host: config.database.host,
|
||||
|
||||
Reference in New Issue
Block a user