47 lines
1.3 KiB
JavaScript
47 lines
1.3 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.
|
|
//
|
|
// No module-level cache: in multi-tenant mode a single un-keyed cache would
|
|
// serve one tenant's row to another. Reads are infrequent (onLoad + dashboard),
|
|
// so we query each time within the caller's tenant context.
|
|
|
|
const crypto = require("crypto");
|
|
const db = require("@saltcorn/data/db");
|
|
|
|
const { TABLE_ENV } = require("./constants");
|
|
|
|
|
|
const getEnv = async () => {
|
|
const rows = await db.select(TABLE_ENV, {});
|
|
return rows.length > 0 ? rows[0] : null;
|
|
};
|
|
|
|
|
|
const initEnvIfMissing = async () => {
|
|
const existing = await getEnv();
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
const now = new Date().toISOString();
|
|
const row = {
|
|
env_id: crypto.randomUUID(),
|
|
env_label: null,
|
|
created_at: now,
|
|
bootstrapped_at: null
|
|
};
|
|
await db.insert(TABLE_ENV, row, { noid: true });
|
|
return row;
|
|
};
|
|
|
|
|
|
const markBootstrapped = async (envId) => {
|
|
const now = new Date().toISOString();
|
|
await db.updateWhere(TABLE_ENV, { bootstrapped_at: now }, { env_id: envId });
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
getEnv,
|
|
initEnvIfMissing,
|
|
markBootstrapped
|
|
};
|