@gmetrixr/rjson
Version:
(R)ecursive Json
75 lines (74 loc) • 4.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.confirmNoCorruption = exports.getHighestProjectVersion = exports.createNewProject = exports.runHealthCheckMigrations = exports.migrationsForNewProject = exports.migrateProjectRJson = exports.initialMigrateProjectRJson = void 0;
const migrationLists_1 = require("./migrationLists");
Object.defineProperty(exports, "getHighestProjectVersion", { enumerable: true, get: function () { return migrationLists_1.getHighestProjectVersion; } });
Object.defineProperty(exports, "confirmNoCorruption", { enumerable: true, get: function () { return migrationLists_1.confirmNoCorruption; } });
const m099_100_initial_r_migration_1 = __importDefault(require("./r-migration-commands/m099_100_initial_r_migration"));
const RecordTypes_1 = require("../../r/R/RecordTypes");
const r_1 = require("../../r");
const rMigrationVersions = Object.keys(migrationLists_1.rMigrationTree).map(numStr => parseInt(numStr)).sort((a, b) => (a - b));
const newProjectMigrationVersions = Object.keys(migrationLists_1.newProjectMigrationTree).map(numStr => parseInt(numStr)).sort((a, b) => (a - b));
const initialMigrateProjectRJson = (projectJson, uptoVersion) => {
var _a, _b;
//Check if project hasn't been converted to recordNode yet
if (((_a = projectJson === null || projectJson === void 0 ? void 0 : projectJson.props) === null || _a === void 0 ? void 0 : _a.version) === undefined || ((_b = projectJson === null || projectJson === void 0 ? void 0 : projectJson.props) === null || _b === void 0 ? void 0 : _b.version) < 100) {
//The following step converts the json to "r" type and makes the version number 100
projectJson = m099_100_initial_r_migration_1.default.execute(projectJson);
}
return projectJson;
};
exports.initialMigrateProjectRJson = initialMigrateProjectRJson;
/**
* Applies migrations for "r" type and returns a new project reference
*/
const migrateProjectRJson = (projectJson, uptoVersion) => {
// ! Initial migrations have been moved to a separate function -> initialMigrateProjectRJson
const rProjectJson = projectJson;
//rMigration version numbers start from 100
let jsonVersion = rProjectJson.props.version;
if (uptoVersion === undefined) {
uptoVersion = rMigrationVersions[rMigrationVersions.length - 1] + 1;
}
for (const key of rMigrationVersions) {
if (jsonVersion === key && key < uptoVersion) {
//console.log(`Running r migration ${key}`);
migrationLists_1.rMigrationTree[key].execute(rProjectJson);
jsonVersion = rProjectJson.props.version;
}
}
return rProjectJson;
};
exports.migrateProjectRJson = migrateProjectRJson;
/**
* Migrations to be run only on a new project (once)
*/
const migrationsForNewProject = (projectJson) => {
const rProjectJson = projectJson;
for (const key of newProjectMigrationVersions) {
migrationLists_1.newProjectMigrationTree[key].execute(rProjectJson);
}
return rProjectJson;
};
exports.migrationsForNewProject = migrationsForNewProject;
/**
* Healthcheck migrations that are supposed to be run many times, ideally on the server
* WIP
*/
const runHealthCheckMigrations = (projectJson) => {
const rProjectJson = projectJson;
const { corrections } = migrationLists_1.healthCheckMigration.execute(rProjectJson);
return { projectJson: rProjectJson, corrections };
};
exports.runHealthCheckMigrations = runHealthCheckMigrations;
const createNewProject = () => {
const project = r_1.R.createRecord(RecordTypes_1.RT.project);
const projectF = r_1.r.project(project);
projectF.set(r_1.rtp.project.version, (0, migrationLists_1.getHighestProjectVersion)());
(0, exports.migrationsForNewProject)(project);
return project;
};
exports.createNewProject = createNewProject;