@gmetrixr/rjson
Version:
(R)ecursive Json
63 lines (62 loc) • 2.95 kB
JavaScript
import { getHighestProjectVersion, newProjectMigrationTree, rMigrationTree, healthCheckMigration, confirmNoCorruption } from "./migrationLists";
import initialRMigration from "./r-migration-commands/m099_100_initial_r_migration";
import { RT } from "../../r/R/RecordTypes";
import { r, R, rtp } from "../../r";
const rMigrationVersions = Object.keys(rMigrationTree).map(numStr => parseInt(numStr)).sort((a, b) => (a - b));
const newProjectMigrationVersions = Object.keys(newProjectMigrationTree).map(numStr => parseInt(numStr)).sort((a, b) => (a - b));
export 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 = initialRMigration.execute(projectJson);
}
return projectJson;
};
/**
* Applies migrations for "r" type and returns a new project reference
*/
export 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}`);
rMigrationTree[key].execute(rProjectJson);
jsonVersion = rProjectJson.props.version;
}
}
return rProjectJson;
};
/**
* Migrations to be run only on a new project (once)
*/
export const migrationsForNewProject = (projectJson) => {
const rProjectJson = projectJson;
for (const key of newProjectMigrationVersions) {
newProjectMigrationTree[key].execute(rProjectJson);
}
return rProjectJson;
};
/**
* Healthcheck migrations that are supposed to be run many times, ideally on the server
* WIP
*/
export const runHealthCheckMigrations = (projectJson) => {
const rProjectJson = projectJson;
const { corrections } = healthCheckMigration.execute(rProjectJson);
return { projectJson: rProjectJson, corrections };
};
export const createNewProject = () => {
const project = R.createRecord(RT.project);
const projectF = r.project(project);
projectF.set(rtp.project.version, getHighestProjectVersion());
migrationsForNewProject(project);
return project;
};
export { getHighestProjectVersion, confirmNoCorruption };