UNPKG

sedk-neo4j

Version:

Cypher builder and validator for Neo4j

51 lines 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReturnStep = void 0; const BaseStep_1 = require("./BaseStep"); const singletoneConstants_1 = require("../singletoneConstants"); const Variable_1 = require("../Variable"); class ReturnStep extends BaseStep_1.BaseStep { constructor(prevStep, items) { super(prevStep); this.items = items; checkItemsIsNotEmpty(); checkItemsAreNotDuplicated(); checkAsteriskIsLast(); checkItemsExistInReturn(prevStep.matchItems); checkThereIsVariableForAsterisk(prevStep.matchItems); function checkItemsIsNotEmpty() { if (items.length === 0) { throw new Error('At least one variable must be provided'); } } function checkItemsAreNotDuplicated() { const itemsSet = new Set(items); if (items.length !== itemsSet.size) { throw new Error('Return item duplicated'); } } function checkAsteriskIsLast() { if (items.find((item, index) => item instanceof singletoneConstants_1.Asterisk && index !== items.length - 1)) { throw new Error('Asterisk must be the last item'); } } function checkItemsExistInReturn(matchItems) { if (!items .filter(it => it instanceof Variable_1.Variable) .every(item => matchItems === null || matchItems === void 0 ? void 0 : matchItems.some(findItem => findItem === item))) { throw new Error('One or more variables are not in the match clause'); } } function checkThereIsVariableForAsterisk(matchItems) { if (items[items.length - 1] instanceof singletoneConstants_1.Asterisk && !(matchItems === null || matchItems === void 0 ? void 0 : matchItems.some(it => it instanceof Variable_1.Variable))) { throw new Error('RETURN ASTERISK is not allowed when there are no variables'); } } } toString() { return `RETURN ${this.items.map(it => it.getStmt()).join(', ')}`; } } exports.ReturnStep = ReturnStep; //# sourceMappingURL=ReturnStep.js.map