@ultipa-graph/ultipa-driver
Version:
NodeJS SDK for ultipa-server 5.2
151 lines • 6.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphExra = void 0;
const connection_base_1 = require("./connection.base");
const utils_1 = require("../../utils");
const types_1 = require("../../types/types");
const types_extra_1 = require("../../types/types.extra");
const CommandList = utils_1.UQLMAKER.CommandList;
class GraphExra extends connection_base_1.ConnectionBase {
/**
* retrieve all graphs, graphs should contain total of nodes and edges.
* @example client.showGraph(config)
*/
async showGraph(config) {
let command = CommandList.showGraph;
let uqlMaker = new utils_1.UQLMAKER(command, config);
uqlMaker.addEmptyParam("more", true);
let res = await this.uql(uqlMaker.toString(), config);
return res.items[types_extra_1.ResponseTableName.GRAPH]?.asGraphSets();
}
/**
* Retrieves all GraphSets from the database.
* TODO delete param partitionByHash
*/
async createGraph(graphSet, config) {
if (!graphSet.name) {
throw new Error("The name of graphSet connot be null.");
}
if (graphSet.shards && !(graphSet.partitionBy)) {
throw new Error("The partitionBy cannot be null.");
}
let gql = `CREATE GRAPH ${graphSet.name} {} `;
(graphSet.shards) && (gql += `PARTITION BY HASH(${graphSet.partitionBy}) SHARDS [${graphSet.shards.map(i => +i)}] `);
(graphSet.description) && (gql += `COMMENT "${graphSet.description}" `);
return this.gql(gql, config);
}
/**
* Creates a new GraphSet in the database, handling cases where the given GraphSet name already exists by ignoring the error.
* @param graph
* @param partitionByHash
* @param config
* @returns
*/
async createGraphIfNotExist(graphSet, config) {
let isGraphExit = await this.hasGraph(graphSet.name, config);
if (isGraphExit) {
return {
response: {
status: utils_1.FormatResponse.successStatus(),
statistics: utils_1.FormatResponse.statisticsSum(),
},
exist: true
};
}
let res = await this.createGraph(graphSet, config);
return {
response: res,
exist: false
};
}
/**
* Retrieves one GraphSet from the database by its name.
*/
async getGraph(graphName, config) {
let command = CommandList.getGraph;
let uqlMaker = new utils_1.UQLMAKER(command, config, graphName);
uqlMaker.addEmptyParam("more", true);
let res = await this.uql(uqlMaker.toString(), config);
return res.items[types_extra_1.ResponseTableName.GRAPH]?.asGraphSets()?.pop();
}
/**
* Checks the existence of a GraphSet in the database by its name.
*/
async hasGraph(graphName, config) {
let res = await this.getGraph(graphName, config);
return res ? true : false;
}
/**
* Drops one GraphSet from the database by its name.
* @param graphName
* @param config
* @returns Response
*/
async dropGraph(graphName, config) {
let command = CommandList.dropGraph;
let uqlMaker = new utils_1.UQLMAKER(command, config, graphName);
return this.uql(uqlMaker.toString(), config);
}
/**
* Alters the name and description of one existing GraphSet in the database by its name.
*/
async alterGraph(graphName, alterGraphset, config) {
let command = CommandList.updateGraph;
let uqlMaker = new utils_1.UQLMAKER(command, config, graphName);
let set = {};
if (alterGraphset.name) {
set.name = alterGraphset.name;
}
if ((0, utils_1.isNotNullString)(alterGraphset.description)) {
set.description = alterGraphset.description;
}
uqlMaker.addParam("set", set);
return this.uql(uqlMaker.toString(), config);
}
/**
* Compacts a GraphSet by clearing its invalid and redundant data on the server disk. Valid data will not be affected.
*/
async compact(graphName, config) {
let command = CommandList.compact;
let uqlMaker = new utils_1.UQLMAKER(command, config, graphName);
let res = await this.uql(uqlMaker.toString(), config);
return {
statisics: res.statistics,
status: utils_1.FormatResponse.successStatus(),
jobId: res.items[types_extra_1.ResponseTableName.JOB_RESULT].asTable().toKV().pop().new_job_id
};
}
/**
* Truncates (Deletes) the specified nodes or edges in the given GraphSet or truncates the entire GraphSet.
* Note that truncating nodes will cause the deletion of edges attached to those affected nodes.
* The truncating operation retains the definition of schemas and properties while deleting the data.
* @param params TruncateParams object containing graphName, schemaName and dbType
* @param config Optional request configuration
*/
async truncate(params, config) {
let { graphName, schemaName, dbType } = params;
if (!graphName) {
throw new Error("GraphName cannot be null.");
}
let command = CommandList.truncate;
let uqlMaker = new utils_1.UQLMAKER(command, config, graphName);
let scope = "*";
if (schemaName) {
if (dbType === undefined) {
throw new Error("DbType is required when schema is specified.");
}
if (schemaName !== "*") {
scope = utils_1.UQLMAKER.SchemaStringWithDefault(schemaName);
}
if (dbType === types_1.DBType.DBNODE) {
uqlMaker.addParam("nodes", scope);
}
else if (dbType === types_1.DBType.DBEDGE) {
uqlMaker.addParam("edges", scope);
}
}
return this.uqlResponse(uqlMaker, config);
}
}
exports.GraphExra = GraphExra;
//# sourceMappingURL=graph.extra.js.map