sc-idp/lib/env.js
2026-06-17 17:37:45 -05:00

48 lines
1.2 KiB
JavaScript

// Singleton per-instance (per-tenant) environment row for saltcorn-idp. Tracks
// first-run bootstrap state and an instance label for the admin UI.
//
// Stored in _sc_config (key CFG_ENV) rather than a _idp_env table: _sc_config
// survives backup and is restored BEFORE onLoad, so a restored instance keeps
// its env_id and onLoad does not create a duplicate (see lib/configStore.js).
const crypto = require("crypto");
const { CFG_ENV } = require("./constants");
const { readKey, writeKey } = require("./configStore");
const getEnv = async () => {
return (await readKey(CFG_ENV)) || null;
};
const initEnvIfMissing = async () => {
const existing = await getEnv();
if (existing) {
return existing;
}
const env = {
env_id: crypto.randomUUID(),
env_label: null,
created_at: new Date().toISOString(),
bootstrapped_at: null
};
await writeKey(CFG_ENV, env);
return env;
};
const markBootstrapped = async (envId) => {
const env = await getEnv();
if (env && env.env_id === envId) {
env.bootstrapped_at = new Date().toISOString();
await writeKey(CFG_ENV, env);
}
};
module.exports = {
getEnv,
initEnvIfMissing,
markBootstrapped
};