sc-dev-deploy/lib/env.js
2026-06-17 17:37:16 -05:00

59 lines
1.6 KiB
JavaScript

// This Saltcorn instance's dev-deploy identity (env_id, label, policies).
//
// Stored in _sc_config (key "dev_deploy_env") rather than a _dd_env table.
// _sc_config survives backup AND is restored BEFORE the plugin's onLoad runs, so
// a restored instance keeps its env_id and onLoad does not create a duplicate
// (see lib/configStore.js). Reads are DB-fresh (no in-process cache needed).
const { randomUuid } = require("./ids");
const { DESTRUCTIVE_POLICY } = require("./constants");
const { readKey, writeKey } = require("./configStore");
const ENV_KEY = "dev_deploy_env";
const getEnv = async () => {
return (await readKey(ENV_KEY)) || null;
};
const refreshEnvCache = async () => {
// No in-process cache anymore; reads are always DB-fresh. Kept for callers.
return await getEnv();
};
const initEnvIfMissing = async () => {
const existing = await getEnv();
if (existing) {
return existing;
}
const env = {
env_id: randomUuid(),
env_label: null,
on_destructive_op: DESTRUCTIVE_POLICY.CONFIRM,
require_tls: 0,
created_at: new Date().toISOString(),
bootstrapped_at: null
};
await writeKey(ENV_KEY, 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(ENV_KEY, env);
}
};
module.exports = {
getEnv,
initEnvIfMissing,
markBootstrapped,
refreshEnvCache
};