@ultipa-graph/ultipa-node-sdk
Version:
NodeJS SDK for ultipa-server 4.0
78 lines • 3.74 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InsertEdges = exports.InsertNodes = void 0;
const types_1 = require("../../types");
const UltipaTransaction_Recorder_1 = require("../UltipaTransaction.Recorder");
var InsertType = types_1.ULTIPA.InsertType;
function InsertNodes(session, nodes) {
return __awaiter(this, void 0, void 0, function* () {
// find all nodes that existed in the databases
const ids = [], uuids = [];
nodes.forEach(node => {
node.getID() && ids.push('"' + node.getID() + '"');
node.getUUID() && uuids.push(node.getUUID());
});
// n({_id in ["u1"] || _uuid in [20]} as n) return n{*}
const res = yield session.conn.uql(`n({_id in [${ids.join(",")}] || _uuid in [${uuids.join(",")}]} as n) return count(n) as total limit 1`);
const total = res.data.alias("total").asAttrs();
if (total.values[0] > 0) {
yield session.throwError("there are some existed nodes when inserting InsertNodes");
return;
}
// fix me
const newNodesResp = yield session.conn.insertNodesBatchAuto(nodes, {
insertType: InsertType.INSERT_TYPE_NORMAL,
silent: false
});
if (newNodesResp.status.code != types_1.ULTIPA.Code.SUCCESS) {
yield session.throwError(newNodesResp.status.message);
}
session.recorder.logs.push({
Op: UltipaTransaction_Recorder_1.UltipaTransactionRecorderOp.New_Nodes,
node_uuids: newNodesResp.data.uuids.map(id => parseInt(id))
});
return newNodesResp;
});
}
exports.InsertNodes = InsertNodes;
function InsertEdges(session, edges) {
return __awaiter(this, void 0, void 0, function* () {
// find all nodes that existed in the databases
const ids = [], uuids = [];
edges.forEach(e => {
e.getUUID() && uuids.push(e.getUUID());
});
if (uuids.length > 0) {
const res = yield session.conn.uql(`find().edges({ _uuid in [${uuids.join(",")}]}) as e return count(e) as total limit 1`);
const total = res.data.alias("total").asAttrs();
if (total.values[0] > 0) {
yield session.throwError("there are some existed edges when inserting InsertEdges");
return;
}
}
// fix me
const newEdgesResp = yield session.conn.insertEdgesBatchAuto(edges, {
insertType: InsertType.INSERT_TYPE_NORMAL,
silent: false
});
if (newEdgesResp.status.code != types_1.ULTIPA.Code.SUCCESS) {
yield session.throwError(newEdgesResp.status.message);
}
session.recorder.logs.push({
Op: UltipaTransaction_Recorder_1.UltipaTransactionRecorderOp.New_Edges,
edge_uuids: newEdgesResp.data.uuids.map(id => parseInt(id))
});
return newEdgesResp;
});
}
exports.InsertEdges = InsertEdges;
//# sourceMappingURL=Transaction.Inserts.js.map