Add the reusable RMS core application (server, web UI, plugins, tests, tools) with generic defaults, GPL licensing, and maintainer context documentation so deployments can consume this repo as software source independent of station-specific overlays.
61 lines
2.1 KiB
JavaScript
61 lines
2.1 KiB
JavaScript
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
const { createJsonStorage } = require("./providers/json");
|
|
const { createSqliteStorage } = require("./providers/sqlite");
|
|
|
|
async function createStorageProvider(config) {
|
|
const providerId = String(config.storageProvider || "json").trim().toLowerCase();
|
|
|
|
if (providerId === "json") {
|
|
const provider = createJsonStorage({ dataDir: config.dataDir });
|
|
await provider.init();
|
|
return provider;
|
|
}
|
|
|
|
if (providerId === "sqlite") {
|
|
const sqlitePath = config.storageSqlitePath || path.join(config.dataDir, "rms-storage.db");
|
|
const provider = createSqliteStorage({
|
|
sqlitePath
|
|
});
|
|
try {
|
|
await provider.init();
|
|
return provider;
|
|
} catch (error) {
|
|
const sqliteMissing = /node:sqlite/i.test(String(error && error.message ? error.message : error));
|
|
const sqliteDbExists = fs.existsSync(sqlitePath);
|
|
if (sqliteMissing && !sqliteDbExists) {
|
|
console.warn(`WARN: SQLite not available (${String(error.message || error)}), falling back to JSON storage at ${config.dataDir}`);
|
|
const fallback = createJsonStorage({ dataDir: config.dataDir });
|
|
await fallback.init();
|
|
return fallback;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
if (providerId === "module") {
|
|
const modulePath = config.storageModulePath;
|
|
if (!modulePath) {
|
|
throw new Error("STORAGE_PROVIDER=module requires STORAGE_MODULE_PATH");
|
|
}
|
|
const resolved = path.isAbsolute(modulePath) ? modulePath : path.resolve(config.rootDir, modulePath);
|
|
const mod = require(resolved);
|
|
const create = typeof mod.createStorage === "function" ? mod.createStorage : mod;
|
|
const provider = await create(config);
|
|
if (!provider || typeof provider.readJson !== "function" || typeof provider.writeJson !== "function") {
|
|
throw new Error("Custom storage module must implement readJson/writeJson");
|
|
}
|
|
if (typeof provider.init === "function") {
|
|
await provider.init();
|
|
}
|
|
return provider;
|
|
}
|
|
|
|
throw new Error(`Unknown STORAGE_PROVIDER: ${providerId}`);
|
|
}
|
|
|
|
module.exports = {
|
|
createStorageProvider
|
|
};
|