couchbase-index-manager
Version:
Manage Couchbase indexes during the CI/CD process
75 lines • 2.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sync = void 0;
const tslib_1 = require("tslib");
const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
const lodash_1 = require("lodash");
const definition_1 = require("./definition");
const plan_1 = require("./plan");
/**
* Executes a synchronization, loading definitions from disk
*/
class Sync {
constructor(manager, path, options) {
this.manager = manager;
this.manager = manager;
this.paths = (0, lodash_1.isArrayLike)(path) ? Array.from(path) : [path];
this.options = (0, lodash_1.extend)({ logger: console }, options);
if (this.paths.find((p) => p === '-')) {
// Can't do interactive prompts if processing from stdin
this.options.interactive = false;
}
}
/**
* Executes the synchronization
*/
async execute() {
var _a;
const plan = await this.createPlan();
const options = this.options;
if (options.interactive) {
plan.print();
}
if (options.dryRun || plan.isEmpty()) {
return;
}
else if (options.interactive && options.confirmSync) {
if (!(await options.confirmSync('Execute index sync plan?'))) {
(_a = options.logger) === null || _a === void 0 ? void 0 : _a.info(chalk_1.default.yellowBright('Cancelling due to user input...'));
return;
}
}
await plan.execute();
}
/**
* Creates the plan
*/
async createPlan() {
var _a;
const definitionLoader = new definition_1.DefinitionLoader(this.options.logger);
const { definitions, nodeMap } = await definitionLoader.loadDefinitions(this.paths);
if (definitions.length === 0) {
(_a = this.options.logger) === null || _a === void 0 ? void 0 : _a.warn(chalk_1.default.yellowBright('No index definitions found'));
}
if (definitions.filter((def) => def.is_primary).length > 1) {
throw new Error('Cannot define more than one primary index');
}
// Apply the node map
nodeMap.apply(definitions);
const mutationContext = {
currentIndexes: await this.manager.getIndexes(),
clusterVersion: await this.manager.getClusterVersion(),
};
// Normalize the definitions before testing for mutations
for (const def of definitions) {
await def.normalize(this.manager);
}
let mutations = (0, lodash_1.compact)((0, lodash_1.flatten)(definitions.map((definition) => Array.from(definition.getMutations(mutationContext)))));
if (this.options.safe) {
mutations = mutations.filter((p) => !p.isSafe());
}
return new plan_1.Plan(this.manager, mutations, this.options);
}
}
exports.Sync = Sync;
//# sourceMappingURL=sync.js.map