@ultipa-graph/ultipa-driver
Version:
NodeJS SDK for ultipa-server 5.1
353 lines • 15.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchInsertExtra = void 0;
const utils_1 = require("../../utils");
const ultipa_pb_1 = require("../../proto/ultipa_pb");
const types_1 = require("../../types/types");
const schema_extra_1 = require("./schema.extra");
const { CommandList, SchemaStringWithDefault, VariableString, FilterString } = utils_1.UQLMAKER;
class BatchInsertExtra extends schema_extra_1.SchemaExra {
/**
* Inserts new nodes of a schema into the current graph through gRPC. The properties within the node values must be consistent with those declared in the schema structure.
* @param table
* @param config
* @returns
*/
async insertNodesBatch(table, config) {
return new Promise(async (resolve, reject) => {
try {
let request = new ultipa_pb_1.InsertNodesRequest();
let clientInfo = await this.getClientInfo({
graphSetName: config?.graph || this.defaultConfig.defaultGraph,
timezone: config?.timezone,
timezoneOffset: config?.timezoneOffset,
});
request.setSilent(config?.silent);
request.setNodeTable(table);
request.setGraphName(clientInfo.goGraphName);
request.setInsertType(config?.insertType);
clientInfo.client.rpcsClient.insertNodes(request, clientInfo.metadata, async (err, reply) => {
let response = new types_1.InsertResponse();
if (err) {
response.status = {
code: types_1.ErrorCode.UNKNOWN_ERROR,
message: err.message || err.details,
};
resolve(response);
return;
}
if (!config?.silent) {
response.uuids = reply.getUuidsList();
}
let ignore_indexes = reply.getIgnoreIndexesList();
let ignore_error_codes = reply.getIgnoreErrorCodeList();
let errorItems = new Map();
if (reply.getStatus().getErrorCode() != ultipa_pb_1.ErrorCode.SUCCESS) {
let error_length = table.getEntityRowsList().length;
ignore_error_codes = new Array(error_length).fill(1);
ignore_indexes = [];
for (let index = 0; index < error_length; index++) {
ignore_indexes.push(index);
errorItems.set(index, ignore_error_codes[index]);
}
}
else {
ignore_indexes.forEach((index, i) => {
errorItems.set(index, ignore_error_codes[i]);
});
}
response = {
status: utils_1.FormatType.status(reply.getStatus()),
statistics: {
engineCost: reply.getEngineTimeCost(),
totalCost: reply.getTimeCost(),
},
ids: reply.getIdsList(),
uuids: reply.getUuidsList(),
errorItems
};
resolve(response);
});
}
catch (error) {
reject(error);
}
});
}
/**
* Inserts new nodes of a schema into the current graph through gRPC. The properties within the node values must be consistent with those declared in the schema structure.
* @param schema
* @param nodes
* @param config
* @returns InsertResponse
*/
async insertNodesBatchBySchema(schema, nodes, config) {
let nodeTable = utils_1.FormatType.toNodeTableWithSchema(schema, nodes, { timezone: config?.timezone, timezoneOffset: config?.timezoneOffset, timestampToString: true });
return this.insertNodesBatch(nodeTable, config);
}
/**
* Insert node list to shcem by schemaName
*/
async insertNodes(schemaName, nodes, config) {
let command = config?.insertType == types_1.InsertType.UPSERT
? CommandList.upsertNode
: config?.insertType == types_1.InsertType.OVERWRITE
? CommandList.insertOverrideNode
: CommandList.insertNode;
command = command.replace("<SCHEMA>", SchemaStringWithDefault(schemaName));
let row = [];
nodes.map(n => {
let res = {};
Object.assign(res, n.values);
if (n.uuid) {
res._uuid = n.uuid;
}
if (n.id) {
res._id = n.id;
}
row.push(res);
});
let uqlMaker = new utils_1.UQLMAKER(command, config, [row]);
if (!config?.silent) {
uqlMaker.addParam("as", "nodes");
uqlMaker.addParam("return", "nodes{_id, _uuid}");
}
let res = await this.uql(uqlMaker.toString(), config);
return res;
}
/**
* Insert edge list to shcem by schemaName
*/
async insertEdges(schemaName, edges, config) {
let command = config?.insertType == types_1.InsertType.UPSERT
? CommandList.upsertEdge
: config?.insertType == types_1.InsertType.OVERWRITE
? CommandList.insertOverrideEdge
: CommandList.insertEdge;
command = command.replace("<SCHEMA>", SchemaStringWithDefault(schemaName));
let row = [];
edges.map(n => {
let res = {};
Object.assign(res, n.values);
if (n.uuid) {
res._uuid = n.uuid;
delete n.uuid;
}
if (n.from) {
res._from = n.from;
delete n.from;
}
if (n.to) {
res._to = n.to;
delete n.to;
}
if (n.fromUuid) {
res._fromUuid = n.fromUuid;
delete n.fromUuid;
}
if (n.toUuid) {
res._toUuid = n.toUuid;
delete n.toUuid;
}
row.push(res);
});
let uqlMaker = new utils_1.UQLMAKER(command, config, [row]);
if (!config?.silent) {
uqlMaker.addParam("as", "edges");
uqlMaker.addParam("return", "edges{_uuid}");
}
let res = await this.uql(uqlMaker.toString(), config);
return res;
}
/**
* Inserts new nodes of one or multiple schemas to the current graph through gRPC. The properties within node values must be consistent with those defined in the corresponding schema structure.
* @param nodes
* @param config
* @returns
*/
async insertNodesBatchAuto(nodes, config) {
let result = new Map();
let schemas = await this.showNodeSchema(config);
let schemaMap = {};
schemas?.forEach(s => {
schemaMap[s.name] = s;
});
let batchMap = {};
nodes.forEach((r, index) => {
// r.set("_index", index)
if (!schemaMap[r.schema]) {
throw new Error(`Row[${index}] Node Schema not found:[${r.schema}]`);
}
if (!batchMap[r.schema]) {
batchMap[r.schema] = [];
}
batchMap[r.schema].push(r);
});
let schemaNames = Object.keys(batchMap);
for (let index = 0; index < schemaNames.length; index++) {
const schemaName = schemaNames[index];
let s = schemaMap[schemaName];
let newS = {
dbType: types_1.DBType.DBNODE,
name: s.name,
properties: s.properties,
};
let insertRows = batchMap[schemaName];
let nodeTable = utils_1.FormatType.toNodeTableWithSchema(newS, insertRows, { timezone: config.timezone, timezoneOffset: config.timezoneOffset, timestampToString: true });
config.silent = false;
let res = await this.insertNodesBatch(nodeTable, config);
result.set(schemaName, res);
}
return result;
}
/**
* Inserts new edges of a schema into the current graph through gRPC. The properties within the edge values must be consistent with those declared in the schema structure.
* @param table
* @param config
* @returns
*/
async insertEdgesBatch(table, config) {
return new Promise(async (resolve, reject) => {
try {
let request = new ultipa_pb_1.InsertEdgesRequest();
let clientInfo = await this.getClientInfo({
graphSetName: config?.graph || this.defaultConfig.defaultGraph,
timezone: config?.timezone,
timezoneOffset: config?.timezoneOffset,
});
request.setSilent(config?.silent);
request.setEdgeTable(table);
request.setGraphName(clientInfo.goGraphName);
request.setInsertType(config.insertType);
clientInfo.client.rpcsClient.insertEdges(request, clientInfo.metadata, async (err, reply) => {
let response = new types_1.InsertResponse();
if (err) {
response.status = {
code: types_1.ErrorCode.UNKNOWN_ERROR,
message: err.message || err.details,
};
resolve(response);
return;
}
let ignore_indexes = reply.getIgnoreIndexesList();
let ignore_error_codes = reply.getIgnoreErrorCodeList();
let errorItems = new Map();
if (reply.getStatus().getErrorCode() != ultipa_pb_1.ErrorCode.SUCCESS) {
let error_length = table.getEntityRowsList().length;
ignore_error_codes = new Array(error_length).fill(1);
ignore_indexes = [];
for (let index = 0; index < error_length; index++) {
ignore_indexes.push(index);
errorItems.set(index, ignore_error_codes[index]);
}
}
else {
ignore_indexes.forEach((index, i) => {
errorItems.set(index, ignore_error_codes[i]);
});
}
if (!config?.silent) {
response.uuids = [...new Set(reply.getUuidsList())];
}
response.errorItems = errorItems;
response.status = utils_1.FormatType.status(reply.getStatus());
response.statistics = {
engineCost: reply.getEngineTimeCost(),
totalCost: reply.getTimeCost()
};
resolve(response);
});
}
catch (error) {
reject(error);
}
});
}
/**
* Inserts new edges of a schema into the current graph through gRPC. The properties within the edge values must be consistent with those declared in the schema structure.
*/
async insertEdgesBatchBySchema(schema, edges, config) {
let valid = this.validEdgeRows(edges);
if (!valid.ok) {
throw new Error(`row[${valid.failedIndex}] error: ${valid.msg}`);
}
let edgeTable = utils_1.FormatType.toEdgeTableWithSchema(schema, edges, { timezone: config?.timezone, timezoneOffset: config?.timezoneOffset, timestampToString: true });
return this.insertEdgesBatch(edgeTable, config);
}
validEdgeRows(rows) {
let validResult = {
ok: true
};
let setFailed = (index, msg) => {
validResult.ok = false;
validResult.msg = msg;
validResult.failedIndex = index;
};
for (let index = 0; index < rows.length; index++) {
const row = rows[index];
if (!row.to && !row.from) {
let msg = `_from/_from_uuid and _to/_to_uuid are null`;
setFailed(index, msg);
break;
}
if (row.from && !row.to) {
let msg = `_from has value [${row.from}] but _to got ${row.to}`;
setFailed(index, msg);
break;
}
if (row.to && !row.from) {
let msg = `_to has value [${row.to}] but _from got ${row.from}`;
setFailed(index, msg);
break;
}
}
return validResult;
}
/**
* Inserts new edges of one or multiple schemas to the current graph through gRPC. The properties within edge values must be consistent with those defined in the corresponding schema structure.
* @param edges
* @param config
* @returns
*/
async insertEdgesBatchAuto(edges, config) {
let valid = this.validEdgeRows(edges);
if (!valid.ok) {
throw new Error(`row[${valid.failedIndex}] error: ${valid.msg}`);
}
let result = new Map();
let schemas = await this.showEdgeSchema(config);
let schemaMap = {};
schemas.forEach(s => {
schemaMap[s.name] = s;
});
let batchMap = {};
edges.forEach((r, index) => {
// r.set("_index", index)
if (!schemaMap[r.schema]) {
throw new Error(`Row[${index}] Edge Schema not found:[${r.schema}]`);
}
if (!batchMap[r.schema]) {
batchMap[r.schema] = [];
}
batchMap[r.schema].push(r);
});
let schemaNames = Object.keys(batchMap);
for (let index = 0; index < schemaNames.length; index++) {
const schemaName = schemaNames[index];
let s = schemaMap[schemaName];
let newS = {
dbType: types_1.DBType.DBEDGE,
name: s.name,
properties: s.properties,
};
let insertRows = batchMap[schemaName];
let table = utils_1.FormatType.toEdgeTableWithSchema(newS, insertRows, { timezone: config.timezone, timezoneOffset: config.timezoneOffset, timestampToString: true });
config.silent = false;
let res = await this.insertEdgesBatch(table, config);
result.set(schemaName, res);
}
return result;
}
}
exports.BatchInsertExtra = BatchInsertExtra;
//# sourceMappingURL=batch.insert.extra.js.map