@gmetrixr/rjson
Version:
(R)ecursive Json
52 lines (51 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deprecateGlobalVarsRecordMap = exports.migrateGlobalVarsDefitions = void 0;
const r_1 = require("../../r");
const VariableTypes_1 = require("../../r/definitions/variables/VariableTypes");
/**
* Because global variables "Definitions" are stored in the db in an older format, we use function to migrate them to the latest format
* Once all global variables definitions are migrated in the DB, we can stop calling this function
*/
const migrateGlobalVarsDefitions = (oldGlobalVarMap) => {
const globalVarsRM = {};
for (const [oldGId, oldGValue] of Object.entries(oldGlobalVarMap)) {
//First if the oldGValue of newer or older format.
//Newer format will be of type RecordNode. So it will have an "id" field. We can check for that.
if (oldGValue.id !== undefined) {
//We are already at the latest format. So change nothing.
globalVarsRM[oldGId] = oldGValue;
continue;
}
const globalVar = (0, r_1.createRecord)(r_1.RT.variable, Number(oldGId), oldGValue.var_name);
// copy over relevant properties
globalVar.name = oldGValue.var_name;
globalVar.props = oldGValue;
globalVar.props.var_category = VariableTypes_1.VarCategory.global;
// delete older properties
delete oldGValue.var_id;
delete oldGValue.var_name;
delete oldGValue.var_global;
globalVarsRM[oldGId] = globalVar;
}
return globalVarsRM;
};
exports.migrateGlobalVarsDefitions = migrateGlobalVarsDefitions;
/**
* This is the reverse of the migrateGlbalVars "Definitions" function, needed before saving back the global vars definitions into the db
* Convert back into the older format. We can stop using this now, as there is a fallback in migrateGlobalVarsDefitions
* to ignore conversion of values which are already in the latest format
*/
const deprecateGlobalVarsRecordMap = (globalVarsRM) => {
const oldGlobalVariableStructureMap = {};
for (const [gId, gValue] of Object.entries(globalVarsRM)) {
const oldGlobalVar = gValue.props;
oldGlobalVar.var_id = Number(gId);
oldGlobalVar.var_name = gValue.name;
oldGlobalVar.var_global = true;
delete oldGlobalVar["var_category"];
oldGlobalVariableStructureMap[gId] = oldGlobalVar;
}
return oldGlobalVariableStructureMap;
};
exports.deprecateGlobalVarsRecordMap = deprecateGlobalVarsRecordMap;