UNPKG

couchbase-index-manager

Version:
48 lines 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Validator = 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"); /** * Executes a synchronization, loading definitions from disk */ class Validator { constructor(path, options) { this.paths = (0, lodash_1.isArrayLike)(path) ? Array.from(path) : [path]; this.options = (0, lodash_1.extend)({ logger: console }, options); } /** * Executes the synchronization */ async execute(manager) { var _a; const definitionLoader = new definition_1.DefinitionLoader(this.options.logger); const { definitions, nodeMap } = await definitionLoader.loadDefinitions(this.paths); if (manager) { await this.validateSyntax(manager, definitions, nodeMap); } (_a = this.options.logger) === null || _a === void 0 ? void 0 : _a.log(chalk_1.default.greenBright('Definitions validated, no errors found.')); } /** * Uses EXPLAIN to validate syntax of the CREATE INDEX statement. */ async validateSyntax(manager, definitions, nodeMap) { nodeMap.apply(definitions); for (const definition of definitions) { const statement = definition.getCreateStatement(manager.bucketName, '__cbim_validate'); try { // Use an EXPLAIN CREATE INDEX statement to validate the syntax. // So long as there is no real index named __cbim_validate, this // will ensure the condition syntax, etc, is correct. await manager.getQueryPlan(statement); } catch (e) { throw new Error(`Invalid index definition for ${definition.name}: ${e.message}`); } } } } exports.Validator = Validator; //# sourceMappingURL=validator.js.map