@ultipa-graph/ultipa-driver
Version:
NodeJS SDK for ultipa-server 5.2
402 lines • 13.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UQL = exports.UQLMAKER = void 0;
class UQLMAKER {
command;
commandP = "";
params = [];
paramsMapSpecail = {};
paramsKeysSpecail = ["as", "return", "group by", "order by", "limit"];
static variableStringPrefix = "\x1cVariableString\x1c";
static forceStringPrefix = "\x1cForceString\x1c";
commonParams;
static Format_RETURN(as, p_return) {
p_return = p_return || ["*"];
if (!Array.isArray(p_return)) {
p_return = [p_return];
}
let isMultipleReturn = Array.isArray(p_return?.[0]);
if (!isMultipleReturn) {
return `${as}{${p_return.join(",")}}`;
}
let s = `${as}`;
p_return.forEach(p => {
s += `{${p.join(",")}}`;
});
return s;
}
static Format_DEPTH(depth) {
let rs = depth;
if (Array.isArray(depth)) {
rs = depth.join(":");
}
return UQLMAKER.VariableString(rs);
}
constructor(command, commonParams, commandParams) {
this.setCommand(command);
this.setCommandParams(commandParams);
this.commonParams = commonParams;
}
static PointFunc(latitude, longitude) {
return `point({latitude:${latitude}, longitude:${longitude}})`;
}
static PointStr(latitude, longitude) {
return `POINT(${latitude} ${longitude})`;
}
replace_$(v) {
v = v.replace(/"(\$[a-z_A-Z]+)"/g, '$1');
v = v.replace(/"(point\([^)]*\))"/g, '$1');
return v;
}
static formatSpecialName = (s) => {
if (s && !s.match(new RegExp("^[a-zA-Z][0-9a-zA-Z_]{0,}$"))) {
return `\`${s}\``;
}
return s;
};
static SchemaStringWithDefault = (schema, property) => {
let str = `@${UQLMAKER.formatSpecialName(schema || "default")}`;
if (!!property) {
str += `.${UQLMAKER.formatSpecialName(property)}`;
}
return str;
};
static IsSchemaString(v) {
return v.startsWith("@");
}
static VariableString = (v) => {
return `${UQLMAKER.variableStringPrefix}${v || ""}`;
};
static IsVariableString = (v) => {
if (v.startsWith(UQLMAKER.variableStringPrefix)) {
return true;
}
};
static VariableStringBack = (v) => {
if (UQLMAKER.IsVariableString(v)) {
return v.slice(UQLMAKER.variableStringPrefix.length);
}
return v;
};
static ForceString = (v) => {
return `${UQLMAKER.forceStringPrefix}${v || ""}`;
};
static IsForceString = (v) => {
if (v.startsWith(UQLMAKER.forceStringPrefix)) {
return true;
}
};
static ForceStringBack = (v) => {
if (UQLMAKER.IsForceString(v)) {
return v.slice(UQLMAKER.forceStringPrefix.length);
}
return v;
};
static IsFilterString = (v) => {
if (v.startsWith("{") && v.endsWith("}")) {
return true;
}
};
static FilterString = (v) => {
if (!v.startsWith("{") && !v.endsWith("}")) {
return `{${v}}`;
}
return v;
};
static CommandList = {
/** Graph */
showGraph: "show().graph",
getGraph: "show().graph",
createGraph: "create().graph",
dropGraph: "drop().graph",
updateGraph: "alter().graph",
/** Schema */
listSchema: "show().schema",
listNodeSchema: "show().node_schema",
listEdgeSchema: "show().edge_schema",
getNodeSchema: "show().node_schema",
getEdgeSchema: "show().edge_schema",
createNodeSchema: "create().node_schema",
createEdgeSchema: "create().edge_schema",
dropNodeSchema: "drop().node_schema",
dropEdgeSchema: "drop().edge_schema",
updateNodeSchema: "alter().node_schema",
updateEdgeSchema: "alter().edge_schema",
/** Property */
showProperty: "show().property",
showNodeProperty: "show().node_property",
showEdgeProperty: "show().edge_property",
createNodeProperty: "create().node_property",
createEdgeProperty: "create().edge_property",
dropNodeProperty: "drop().node_property",
dropEdgeProperty: "drop().edge_property",
updateNodeProperty: "alter().node_property",
updateEdgeProperty: "alter().edge_property",
/** LTE */
lteNode: "LTE().node_property",
lteEdge: "LTE().edge_property",
ufeNode: "UFE().node_property",
ufeEdge: "UFE().edge_property",
/** Index */
showIndex: "show().index",
showNodeIndex: "show().node_index",
showEdgeIndex: "show().edge_index",
dropNodeIndex: "drop().node_index",
dropEdgeIndex: "drop().edge_index",
createNodeIndex: "create().node_index",
createEdgeIndex: "create().edge_index",
/** Fulltext */
showFulltext: "show().fulltext",
showNodeFulltext: "show().node_fulltext",
showEdgeFulltext: "show().edge_fulltext",
createNodeFulltext: "create().node_fulltext",
createEdgeFulltext: "create().edge_fulltext",
dropNodeFulltext: "drop().node_fulltext",
dropEdgeFulltext: "drop().edge_fulltext",
truncate: "truncate().graph",
compact: "compact().graph",
listAlgo: "show().algo",
listExta: "show().exta",
showTask: "show().task",
clearTask: "clear().task",
stopTask: "stop().task",
pauseTask: "pause().task",
resumeTask: "resume().task",
showJob: "show().job",
clearJob: "clear().job",
stopJob: "stop().job",
pauseJob: "pause().job",
resumeJob: "resume().job",
showUser: "show().user",
getUser: "show().user",
getSelfInfo: "show().self",
dropUser: "drop().user",
createUser: "create().user",
alterUser: "alter().user",
showPrivilege: "show().privilege",
createPolicy: "create().policy",
alterPolicy: "alter().policy",
dropPolicy: "drop().policy",
showPolicy: "show().policy",
getPolicy: "show().policy",
grantUser: "grant().user",
revokeUser: "revoke().user",
insertNode: "insert().into(<SCHEMA>).nodes",
insertOverrideNode: "insert().overwrite().into(<SCHEMA>).nodes",
insertEdge: "insert().into(<SCHEMA>).edges",
insertOverrideEdge: "insert().overwrite().into(<SCHEMA>).edges",
upsertNode: "upsert().into(<SCHEMA>).nodes",
upsertEdge: "upsert().into(<SCHEMA>).edges",
nodeSpread: "spread",
ab: "ab",
khop: "khop",
autoNet: "autonet",
// ❗️ sdk 4 support above ⤴️
nodes: "find().nodes",
edges: "find().edges",
deleteNodes: "delete().nodes",
deleteEdges: "delete().edges",
updateNodes: "update().nodes",
updateEdges: "update().edges",
template: "t",
stats: "stats",
top: "top",
kill: "kill",
algo: "algo",
algo_dv: "algo_dv",
mount: "mount",
unmount: "unmount",
mountGraph: "mount().graph",
unmountGraph: "unmount().graph",
licenseDump: "license.dump",
showHDCAlgo: "hdc.server.show",
createHDCGraph: "hdc.graph.create",
showHDCGraph: "hdc.graph.show",
dropHDCGraph: "hdc.graph.drop",
rebalanceGraph: "alter().graph",
};
setCommand(command) {
if (command) {
this.command = command;
}
}
_p = (p) => {
if (typeof p == "string") {
if (UQLMAKER.IsSchemaString(p)) {
return p;
}
if (UQLMAKER.IsVariableString(p)) {
return UQLMAKER.VariableStringBack(p);
}
if (UQLMAKER.IsForceString(p)) {
p = UQLMAKER.ForceStringBack(p);
}
}
if (typeof p == "object" || typeof p == "string") {
p = JSON.stringify(p);
p = this.replace_$(p);
}
return p;
};
setCommandParams(commandP) {
if (!commandP) {
return;
}
if (!Array.isArray(commandP)) {
commandP = [commandP];
}
commandP = commandP.map(p => this._p(p)).join(",");
this.commandP = commandP;
}
addEmptyParam(key, need) {
if (need) {
this.addParam(key, "", false);
}
}
addParam(key, value, required = true) {
if (typeof value == "boolean") {
if (value === true) {
required = false;
}
value = "";
}
if (required) {
if (!value) {
return;
}
}
let _notStringify = false;
if (typeof value == "string" && (UQLMAKER.IsVariableString(value) || UQLMAKER.IsSchemaString(value))) {
_notStringify = true;
value = UQLMAKER.VariableStringBack(value);
}
if (typeof value == "string" && UQLMAKER.IsForceString(value)) {
value = UQLMAKER.ForceStringBack(value);
}
switch (key) {
case "filter":
this.addParam("node_filter", value);
this.addParam("edge_filter", value);
return;
case "as":
case "select":
case "return":
case "select_node_properties":
case "select_edge_properties":
_notStringify = true;
if (Array.isArray(value)) {
value = value.join(",");
}
break;
case "graph_privileges":
if (!value) {
value = [];
}
value = value.map((v) => {
return {
[v["name"]]: v["values"]
};
});
break;
case "system_privileges":
case "policies":
// user json
break;
}
// console.log(1, key, value, _notStringify)
let _isObject = typeof value == "object";
if (_isObject || (typeof value == "string" && value.length > 0 && !_notStringify)) {
value = JSON.stringify(value);
if (_isObject) {
value = this.replace_$(value);
}
}
let p = { key, value };
if (this.paramsKeysSpecail.includes(key)) {
this.paramsMapSpecail[key] = p;
}
else {
this.params.push(p);
}
}
commandPToString() {
return this.commandP;
if (this.commandP || this.commandP.length > 0) {
if (this.commandP.trim()[0] == "{") {
return `${this.commandP}`;
}
return JSON.stringify(this.commandP);
}
return "";
}
toString() {
let uql = "";
uql += `${this.command}(${this.commandPToString()})`;
if (this.params.length > 0) {
uql += `.${this.params
.map(param => {
return `${param.key}(${param.value})`;
})
.join(".")}`;
}
this.paramsKeysSpecail.forEach(k => {
let param = this.paramsMapSpecail[k];
if (param) {
uql += ` ${param.key} ${param.value}`;
}
});
return uql;
}
}
exports.UQLMAKER = UQLMAKER;
exports.UQL = {
uqlObjectExample(uqlStr) {
return {
uql: uqlStr,
command: '',
commandParam: '',
params: {},
paramsOriginal: {},
toStringWithParamsOriginal: function () {
let { command, commandParam, paramsOriginal } = this;
if (!command) {
return "";
}
let param_str = (p) => {
p = p || "";
return p;
};
let commands = [
[command, commandParam]
];
for (const key in paramsOriginal) {
commands.push([key, paramsOriginal[key]]);
}
return commands.map(([c, p]) => `${c}(${param_str(p)})`).join(".");
}
};
},
// uql: showTask().id( "1").name('abc').status( 'pengding' ).limit(199 ).filter( {abc: "123", a: {$gt: 123}})
parse(uqlStr) {
let commandReg = /([a-z_A-Z]*)\(([^\(|^\)]*)\)/g;
let matchAll = uqlStr.matchAll(commandReg);
let result = exports.UQL.uqlObjectExample(uqlStr);
let index = 0;
for (const m of matchAll) {
let name = m[1];
let value = (m[2] || "");
value = value.replace(/^\s+|\s+$/g, ""); // 去除头尾空格
if (index == 0) {
result.command = name;
result.commandParam = value;
}
else {
result.params[name] = value.replace(/^[\'|\"]|[\'|\"]$/g, ""); //去除头尾的引号
result.paramsOriginal[name] = value;
}
index++;
}
return result;
}
};
//# sourceMappingURL=uql-maker.js.map