74 lines
2.1 KiB
JavaScript
74 lines
2.1 KiB
JavaScript
// This Saltcorn instance's site-sync identity (env_id, label, policies).
|
|
//
|
|
// Stored in _sc_config (key "site_sync_env") rather than a 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/common/configStore.js). Reads are DB-fresh (no in-process cache needed).
|
|
|
|
const { randomUuid } = require("./ids");
|
|
const { readKey, writeKey } = require("./common/configStore");
|
|
const {
|
|
SYNC_MODES,
|
|
DELETE_POLICY,
|
|
APPLY_POLICY
|
|
} = require("./constants");
|
|
|
|
|
|
const ENV_KEY = "site_sync_env";
|
|
|
|
|
|
const getEnv = async () => {
|
|
return (await readKey(ENV_KEY)) || null;
|
|
};
|
|
|
|
|
|
const initEnvIfMissing = async () => {
|
|
const existing = await getEnv();
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
const env = {
|
|
env_id: randomUuid(),
|
|
env_label: null,
|
|
// Full-mirror is the default posture: the target converges exactly to the
|
|
// source, pruning entities the source no longer has (metadata prunes freely;
|
|
// data-bearing drops are still gated by on_delete + an explicit allow-list).
|
|
sync_mode: SYNC_MODES.MIRROR,
|
|
on_delete: DELETE_POLICY.CONFIRM,
|
|
on_apply: APPLY_POLICY.CONFIRM,
|
|
require_tls: 0,
|
|
created_at: new Date().toISOString()
|
|
};
|
|
await writeKey(ENV_KEY, env);
|
|
return env;
|
|
};
|
|
|
|
|
|
const setEnvFields = async (patch) => {
|
|
const env = await getEnv();
|
|
if (!env) {
|
|
return null;
|
|
}
|
|
const next = Object.assign({}, env, patch);
|
|
await writeKey(ENV_KEY, next);
|
|
return next;
|
|
};
|
|
|
|
|
|
// The identity is created in onLoad, so a missing one on a live request is an
|
|
// installation fault; fail loudly instead of answering with a null env_id.
|
|
const requireEnv = async () => {
|
|
const env = await getEnv();
|
|
if (!env) {
|
|
throw new Error("site-sync identity (site_sync_env) is missing; reload the plugin");
|
|
}
|
|
return env;
|
|
};
|
|
|
|
|
|
module.exports = {
|
|
getEnv,
|
|
requireEnv,
|
|
initEnvIfMissing,
|
|
setEnvFields
|
|
};
|