UNPKG

sedk-neo4j

Version:

Cypher builder and validator for Neo4j

72 lines 2.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Step = void 0; const Variable_1 = require("../Variable"); const singletoneConstants_1 = require("../singletoneConstants"); class Step { match(...varLabels) { if (varLabels.length === 0) { throw new Error('No variable or labels provided'); } this.matchItems = [...varLabels]; return this; } return(...items) { checkItemsIsNotEmpty(); checkItemsAreNotDuplicated(); checkAsteriskIsLast(); checkItemsExistInReturn(this.matchItems); checkThereIsVariableForAsterisk(this.matchItems); this.returnItems = [...items]; return this; 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'); } } } getCypher() { if (this.matchItems === undefined || this.matchItems.length === 0) { throw new Error('No variable or labels provided'); } const matchArray = this.matchItems.map(it => it.getStmt()); if (!(this.matchItems[0] instanceof Variable_1.Variable)) { matchArray.unshift(''); } let cypher = `MATCH (${matchArray.join(':')})`; if (this.returnItems !== undefined && this.returnItems.length > 0) { cypher += ` RETURN ${this.returnItems.map(it => it.getStmt()).join(', ')}`; } return cypher; } cleanUp() { this.matchItems = undefined; this.returnItems = undefined; } } exports.Step = Step; //# sourceMappingURL=Step.js.map