@magic-mustard/sqlsync
Version:
SQLSync simplifies database schema evolution by allowing a declarative approach to table management
78 lines (77 loc) • 3.17 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StateProcessor = void 0;
const namespace_1 = require("./namespace");
class StateProcessor extends namespace_1.State.AbstractProcessor {
constructor(stateFileContents, fileIO, latestMigrationFileName) {
let state = { migrations: {}, namespace: {} };
if (stateFileContents.fileExists) {
state = JSON.parse(stateFileContents.contents);
}
super(state, fileIO, latestMigrationFileName);
}
/**
* Retrieves the current state of migrations for a specific namespace.
*/
getMigrationState(namespace) {
const migrations = this.state.migrations || {};
const result = {};
Object.entries(migrations).forEach(([migrationFile, namespaces]) => {
if (namespaces[namespace]) {
result[migrationFile] = namespaces;
}
});
return result;
}
/**
* Updates the state with a new migration state for the latest migration only.
*/
updateMigrationState(newState) {
const latestMigration = this.getLatestMigrationName();
if (!this.state.migrations[latestMigration]) {
this.state.migrations[latestMigration] = {};
}
// Assuming the newState is already structured with namespace
Object.entries(newState).forEach(([namespace, data]) => {
this.state.migrations[latestMigration][namespace] = data;
});
}
/**
* Returns the state for a specific namespace.
*/
getNamespaceState(namespace) {
var _a;
const dataToReturn = {
[namespace]: (_a = this.state.namespace[namespace]) !== null && _a !== void 0 ? _a : {}
};
return dataToReturn;
}
/**
* Updates the state in the namespace structure.
*/
updateNamespaceState(namespace, newState) {
this.state.namespace[namespace] = newState[namespace];
}
/**
* Serializes and writes the state to disk using the provided fileIO.
* Only migration filenames (ending with .sql and not source SQL files) are saved as top-level keys.
*/
write(configDir) {
return __awaiter(this, void 0, void 0, function* () {
yield this.fileIO.write(configDir, JSON.stringify(this.state, null, 2));
});
}
getLatestMigrationName() {
return this.latestMigrationFileName;
}
}
exports.StateProcessor = StateProcessor;