gremlin-helper
Version:
A wrapper around the gremlin client to introduce model validation and other useful functionality to use within a web api.
174 lines • 5.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class QueryBuilder {
constructor(query, props = {}, postfix = '') {
this.query = query;
this.props = props;
this.postfix = postfix;
this.prop = 0;
}
addV(node) {
if (this.query) {
throw new Error('addV must be used as the first call.');
}
const label = this.getProp();
this.query = `g.addV(${label})`;
this.props[label] = node.schema.label;
return this;
}
addE(edge, from, to) {
if (this.query) {
throw new Error('addE must be used as the first call.');
}
const label = this.getProp();
const fromProp = this.getProp();
const toProp = this.getProp();
this.query = `g.V(${fromProp}).addE(${label}).to(g.V(${toProp}))`;
this.props[label] = edge.schema.label;
this.props[fromProp] = from;
this.props[toProp] = to;
return this;
}
properties(obj) {
if (!this.query) {
throw new Error('props can not be used as the first call.');
}
for (const key in obj) {
const keyProp = this.getProp();
const valueProp = this.getProp();
this.query = `${this.query}.property(${keyProp}, ${valueProp})`;
this.props[keyProp] = key;
this.props[valueProp] = obj[key];
}
return this;
}
deleteV(vertex, id) {
if (this.query) {
throw new Error('deleteV must be used as the first call.');
}
const label = this.getProp();
const idProp = this.getProp();
this.query = `g.V(${idProp}).has('label', ${label}).drop()`;
this.props[label] = vertex.schema.label;
this.props[idProp] = id;
return this;
}
getV(vertex, id) {
if (this.query) {
throw new Error('getAll must be used as the first call.');
}
const label = this.getProp();
const idProp = this.getProp();
this.query = `g.V(${idProp}).as('x').has('label', ${label})`;
this.postfix = `.select('x')`;
this.props[label] = vertex.schema.label;
this.props[idProp] = id;
return this;
}
updateV(vertex, id) {
if (this.query) {
throw new Error('getAll must be used as the first call.');
}
const label = this.getProp();
const idProp = this.getProp();
this.query = `g.V(${idProp}).has('label', ${label})`;
this.props[label] = vertex.schema.label;
this.props[idProp] = id;
return this;
}
getAllV(node) {
if (this.query) {
throw new Error('getAll must be used as the first call.');
}
const label = this.getProp();
this.query = `g.V().as('x').has('label', ${label})`;
this.postfix = `.select('x')`;
this.props[label] = node.schema.label;
return this;
}
has(prop, value) {
if (!this.query) {
throw new Error('has can not be used as the first call.');
}
const label = this.getProp();
const valueProp = this.getProp();
this.query = `${this.query}.has(${label}, ${valueProp})`;
this.props[label] = prop;
this.props[valueProp] = value;
return this;
}
hasE(edge) {
if (!this.query) {
throw new Error('hasE can not be used as the first call.');
}
const label = this.getProp();
this.query = `${this.query}.bothE().has('label', ${label})`;
this.props[label] = edge.schema.label;
return this;
}
toOrFrom(node, id) {
if (!this.query) {
throw new Error('to can not be used as the first call.');
}
const label = this.getProp();
this.query = `${this.query}.bothV().has('label', ${label})`;
this.props[label] = node.schema.label;
if (id) {
const idProp = this.getProp();
this.query = `${this.query}.has('id', ${idProp})`;
this.props[idProp] = id;
}
return this;
}
hasOutE(edge) {
if (!this.query) {
throw new Error('hasOutE can not be used as the first call.');
}
const label = this.getProp();
this.query = `${this.query}.outE().has('label', ${label})`;
this.props[label] = edge.schema.label;
return this;
}
to(node, id) {
if (!this.query) {
throw new Error('to can not be used as the first call.');
}
const label = this.getProp();
this.query = `${this.query}.inV().has('label', ${label})`;
this.props[label] = node.schema.label;
if (id) {
const idProp = this.getProp();
this.query = `${this.query}.has('id', ${idProp})`;
this.props[idProp] = id;
}
return this;
}
hasInE(edge) {
if (!this.query) {
throw new Error('hasInE can not be used as the first call.');
}
const label = this.getProp();
this.query = `${this.query}.inE().has('label', ${label})`;
this.props[label] = edge.schema.label;
return this;
}
from(node, id) {
if (!this.query) {
throw new Error('from can not be used as the first call.');
}
const label = this.getProp();
this.query = `${this.query}.outV().has('label', ${label})`;
this.props[label] = node.schema.label;
if (id) {
const idProp = this.getProp();
this.query = `${this.query}.has('id', ${idProp})`;
this.props[idProp] = id;
}
return this;
}
getProp() {
return `prop${this.prop++}`;
}
}
exports.QueryBuilder = QueryBuilder;
//# sourceMappingURL=QueryBuilder.js.map