UNPKG

neo4-js

Version:

Neo4j graphdb object graph mapper for javascript

134 lines (133 loc) 4.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const CharGenerator_1 = require("./CharGenerator"); const singlePredicates = { $sw: "STARTS WITH", $ew: "ENDS WITH", $contains: "CONTAINS", $reg: "=~", $eq: "=", $in: "IN", $gt: ">", $gte: ">=", $lt: "<", $lte: "<=", }; function _prepareWhere(props, variable, cg) { const where = []; let flatProps = {}; if (!props) { return { where: [], flatProps: {} }; } lodash_1.forIn(props, (v, k) => { const propChar = cg ? cg.next() : CharGenerator_1.default.next(); let found = false; if (v.$or) { found = true; const whereOr = []; for (const predicate of v.$or) { const tmp = _prepareWhere({ [k]: predicate }, variable, cg); whereOr.push(...tmp.where); flatProps = Object.assign({}, flatProps, tmp.flatProps); } where.push(`(${whereOr.join(" OR ")})`); } else if (v.$and) { found = true; const whereAnd = []; for (const predicate of v.$and) { const tmp = _prepareWhere({ [k]: predicate }, variable, cg); whereAnd.push(...tmp.where); flatProps = Object.assign({}, flatProps, tmp.flatProps); } where.push(`(${whereAnd.join(" AND ")})`); } else if (v.$not) { found = true; const tmp = _prepareWhere({ [k]: v.$not }, variable, cg); if (tmp.where.length) { where.push(`NOT ${tmp.where[0]}`); flatProps = Object.assign({}, flatProps, tmp.flatProps); } } else if (v.$between) { found = true; if (v.$between.length === 2) { const [num1, num2] = v.$between; const a = propChar; const b = cg ? cg.next() : CharGenerator_1.default.next(); where.push(`{${a}} <= ${variable}.${k} <= {${b}}`); flatProps[a] = num1 > num2 ? num2 : num1; flatProps[b] = num1 > num2 ? num1 : num2; } } else { lodash_1.forIn(singlePredicates, (predicateString, predicateKey) => { const val = v[predicateKey]; if (val || typeof val === "number" || typeof val === "string") { found = true; where.push(`${variable}.${k} ${predicateString} {${propChar}}`); flatProps[propChar] = v[predicateKey]; } }); } if (!found) { where.push(`${variable}.${k} = {${propChar}}`); flatProps[propChar] = v; } }); return { where, flatProps }; } function prepareWhere(properties, variables, cg) { if (!cg) CharGenerator_1.default.start("a"); let vars = []; let props = {}; if (typeof variables === "string") { vars = [variables]; props[variables] = properties; } else if (Array.isArray(variables)) { vars = variables; props = properties; } let where = []; let flatProps = {}; vars.forEach(variable => { const result = _prepareWhere(props[variable], variable, cg); where = where.concat(result.where); Object.assign(flatProps, result.flatProps); }); if (where.length === 0) { return { where: "", flatProps: undefined }; } return { where: "WHERE " + where.join(" AND "), flatProps }; } exports.prepareWhere = prepareWhere; function prepareSet(properties, variables) { const sets = []; const newProps = {}; let vars = []; let props = {}; if (typeof variables === "string") { vars = [variables]; props[variables] = properties; } else if (Array.isArray(variables)) { vars = variables; props = properties; } vars.forEach(variable => { lodash_1.forIn(props[variable], (v, k) => { if (k === "guid") return null; sets.push(`${variable}.${k}={_u_${variable}_${k}}`); newProps[`_u_${variable}_${k}`] = v; }); }); if (!sets.length) throw new Error(`Nothing to update`); return { str: sets.join(", "), newProps }; } exports.prepareSet = prepareSet;