sgnm-neo4j
Version:
neo4j module for nestJs
801 lines • 442 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Neo4jService = void 0;
const common_1 = require("@nestjs/common");
const neo4j_driver_1 = __importStar(require("neo4j-driver"));
const neo4j_constants_1 = require("./neo4j.constants");
const transaction_1 = __importDefault(require("neo4j-driver-core/lib/transaction"));
const neo4j_driver_core_1 = require("neo4j-driver-core");
const common_func_1 = require("./func/common.func");
const custom_error_object_1 = require("./constant/custom.error.object");
const relation_direction_enum_1 = require("./constant/relation.direction.enum");
const pagination_enum_1 = require("./constant/pagination.enum");
const filter_properties_type_enum_1 = require("./constant/filter.properties.type.enum");
const nestjs_request_context_1 = require("nestjs-request-context");
let Neo4jService = class Neo4jService {
constructor(config, driver) {
this.driver = driver;
this.config = config;
}
getDriver() {
return this.driver;
}
getConfig() {
return this.config;
}
int(value) {
return (0, neo4j_driver_1.int)(value);
}
beginTransaction(database) {
const currentDatabase = database ? database : this.getCurrentDatabase();
const session = this.getWriteSession(currentDatabase);
return session.beginTransaction();
}
getReadSession(database) {
return this.driver.session({
database: database || this.config.database,
defaultAccessMode: neo4j_driver_1.default.session.READ,
});
}
getWriteSession(database) {
return this.driver.session({
database: database || this.config.database,
defaultAccessMode: neo4j_driver_1.default.session.WRITE,
});
}
getCurrentDatabase() {
var _a;
const ctx = nestjs_request_context_1.RequestContext.currentContext;
const tenantId = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.req) === null || _a === void 0 ? void 0 : _a.neo4jDatabase;
return this.config.database || tenantId || "neo4j";
}
read(cypher, params, databaseOrTransaction) {
const database = typeof databaseOrTransaction === 'string'
? databaseOrTransaction
: this.getCurrentDatabase();
if (databaseOrTransaction instanceof transaction_1.default) {
return databaseOrTransaction.run(cypher, params);
}
console.log("databaseombayusread", database);
const session = this.getReadSession(database);
return session.run(cypher, params);
}
write(cypher, params, databaseOrTransaction) {
const database = typeof databaseOrTransaction === 'string'
? databaseOrTransaction
: this.getCurrentDatabase();
if (databaseOrTransaction instanceof transaction_1.default) {
return databaseOrTransaction.run(cypher, params);
}
console.log("databaseombayuswrite", database);
const session = this.getWriteSession(database);
return session.run(cypher, params);
}
convertStringToDateTime(dateTime) {
return neo4j_driver_1.default.types.DateTime.fromStandardDate(new Date(dateTime));
}
convertStringToLocalDateTime(localDateTime) {
return neo4j_driver_1.default.types.LocalDateTime.fromStandardDate(new Date(localDateTime));
}
onApplicationShutdown() {
return this.driver.close();
}
getLocalDateTime(localDateTime) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.convertStringToLocalDateTime(localDateTime);
});
}
getDateTime(dateTime) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.convertStringToDateTime(dateTime);
});
}
changeObjectChildOfPropToChildren(node) {
return __awaiter(this, void 0, void 0, function* () {
node["root"]["children"] = node["root"]["parent_of"];
delete node["root"]["parent_of"];
if (node["root"]["children"]) {
for (let i = 0; i < node["root"]["children"].length; i++) {
node["root"]["children"][i]["children"] =
node["root"]["children"][i]["parent_of"];
delete node["root"]["children"][i]["parent_of"];
if (node["root"]["children"][i]["children"]) {
for (let j = 0; j < node["root"]["children"][i]["children"].length; j++) {
node["root"]["children"][i]["children"][j]["children"] =
node["root"]["children"][i]["children"][j]["parent_of"];
delete node["root"]["children"][i]["children"][j]["parent_of"];
if (node["root"]["children"][i]["children"][j]["children"]) {
for (let k = 0; k <
node["root"]["children"][i]["children"][j]["children"].length; k++) {
node["root"]["children"][i]["children"][j]["children"][k]["children"] =
node["root"]["children"][i]["children"][j]["children"][k]["parent_of"];
delete node["root"]["children"][i]["children"][j]["children"][k]["parent_of"];
if (node["root"]["children"][i]["children"][j]["children"][k]["children"]) {
for (let l = 0; l <
node["root"]["children"][i]["children"][j]["children"][k]["children"].length; l++) {
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"] =
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["parent_of"];
delete node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["parent_of"];
if (node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"]) {
for (let m = 0; m <
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"].length; m++) {
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"] =
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["parent_of"];
delete node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["parent_of"];
if (node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"]) {
for (let n = 0; n <
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"]
.length; n++) {
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"] =
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["parent_of"];
delete node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["parent_of"];
if (node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"]) {
for (let o = 0; o <
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"].length; o++) {
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["children"] =
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["parent_of"];
delete node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["parent_of"];
if (node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["children"]) {
for (let p = 0; p <
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["children"].length; p++) {
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["children"][p]["children"] =
node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["children"][p]["parent_of"];
delete node["root"]["children"][i]["children"][j]["children"][k]["children"][l]["children"][m]["children"][n]["children"][o]["children"][p]["parent_of"];
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return node;
});
}
findByIdAndFilters(id, labels, filter_properties = {}, excluded_labels = [], databaseOrTransaction) {
return __awaiter(this, void 0, void 0, function* () {
const LabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(labels);
const excludedLabelsLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(excluded_labels);
let query = "match (n" +
(0, common_func_1.dynamicLabelAdder)(LabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(filter_properties) +
` where id(n)=${id} `;
if (excludedLabelsLabelsWithoutEmptyString &&
excludedLabelsLabelsWithoutEmptyString.length > 0) {
query =
query +
" and " +
(0, common_func_1.dynamicNotLabelAdder)("n", excludedLabelsLabelsWithoutEmptyString) +
` return n`;
}
else {
query = query + ` return n`;
}
filter_properties["id"] = id;
const node = yield this.read(query, filter_properties, databaseOrTransaction);
delete filter_properties["id"];
if (node.records.length === 0) {
throw new common_1.HttpException(custom_error_object_1.node_not_found, 404);
}
else {
return node.records[0]["_fields"][0];
}
});
}
findByLabelAndFilters(labels = [""], filter_properties = {}, excluded_labels = [""], databaseOrTransaction) {
return __awaiter(this, void 0, void 0, function* () {
const excludedLabelsLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(excluded_labels);
let query = "match (n" +
(0, common_func_1.dynamicLabelAdder)(labels) +
(0, common_func_1.dynamicFilterPropertiesAdder)(filter_properties);
if (excludedLabelsLabelsWithoutEmptyString &&
excludedLabelsLabelsWithoutEmptyString.length > 0) {
query =
query +
" where " +
(0, common_func_1.dynamicNotLabelAdder)("n", excludedLabelsLabelsWithoutEmptyString) +
` return n`;
}
else {
query = query + ` return n`;
}
console.log("findByIdAndFilters", query);
console.log("findByIdAndFilters", filter_properties);
const node = yield this.read(query, filter_properties, databaseOrTransaction);
return node.records;
});
}
findByOrLabelsAndFilters(or_labels = [""], filter_properties = {}, databaseOrTransaction) {
return __awaiter(this, void 0, void 0, function* () {
const orLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(or_labels);
let query = "match (n " + (0, common_func_1.dynamicFilterPropertiesAdder)(filter_properties);
if (orLabelsWithoutEmptyString && orLabelsWithoutEmptyString.length > 0) {
query =
query +
" where " +
(0, common_func_1.dynamicOrLabelAdder)("n", orLabelsWithoutEmptyString) +
` return n`;
}
else {
query = query + ` return n`;
}
const node = yield this.read(query, filter_properties, databaseOrTransaction);
if (node) {
return node.records;
}
else {
return [];
}
});
}
findByIdAndOrLabelsAndFilters(id, or_labels = [""], filter_properties = {}, databaseOrTransaction) {
return __awaiter(this, void 0, void 0, function* () {
const orLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(or_labels);
let query = "match (n " +
(0, common_func_1.dynamicFilterPropertiesAdder)(filter_properties) +
` where id(n)=${id} `;
if (orLabelsWithoutEmptyString && orLabelsWithoutEmptyString.length > 0) {
query =
query +
" and" +
(0, common_func_1.dynamicOrLabelAdder)("n", orLabelsWithoutEmptyString) +
` return n`;
}
else {
query = query + ` return n`;
}
const node = yield this.read(query, filter_properties, databaseOrTransaction);
return node.records;
});
}
updateByIdAndFilter(id, labels = [""], filter_properties = {}, update_labels = [], update_properties = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const labelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(labels);
const updateLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(update_labels);
const isNodeExist = yield this.findByIdAndFilters(id, labelsWithoutEmptyString, filter_properties);
if (!isNodeExist) {
throw new common_1.HttpException(custom_error_object_1.node_not_found, 404);
}
let query = `match (n` +
(0, common_func_1.dynamicLabelAdder)(labelsWithoutEmptyString) +
`)` +
` where id(n)=${id} set ` +
(0, common_func_1.dynamicUpdatePropertyAdder)("n", update_properties);
if (updateLabelsWithoutEmptyString &&
updateLabelsWithoutEmptyString.length > 0) {
if (!update_properties || Object.keys(update_properties).length === 0) {
query =
query +
" n" +
(0, common_func_1.dynamicLabelAdder)(updateLabelsWithoutEmptyString) +
" return n";
}
else {
query =
query +
", n" +
(0, common_func_1.dynamicLabelAdder)(updateLabelsWithoutEmptyString) +
" return n";
}
}
else {
query = query + " return n";
}
update_properties["id"] = id;
const parameters = update_properties;
const node = yield this.write(query, parameters, databaseOrTransaction);
if (node.records.length === 0) {
return null;
}
else {
return node.records[0]["_fields"][0];
}
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(custom_error_object_1.library_server_error, common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
updateByLabelAndFilter(labels = [], filter_properties = {}, update_labels = [], update_properties = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const nodelabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(labels);
const updateLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(update_labels);
let query = "match (n" +
(0, common_func_1.dynamicLabelAdder)(nodelabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(filter_properties) +
` set ` +
(0, common_func_1.dynamicUpdatePropertyAdderAndAddParameterKey)("n", update_properties);
if (updateLabelsWithoutEmptyString &&
updateLabelsWithoutEmptyString.length > 0) {
query =
query +
", n" +
(0, common_func_1.dynamicLabelAdder)(updateLabelsWithoutEmptyString) +
" return n";
}
else {
query = query + " return n";
}
const update_properties1 = (0, common_func_1.changeObjectKeyName)(update_properties, "1");
const parameters = Object.assign(Object.assign({}, filter_properties), update_properties1);
console.log("updateByLabelAndFilter", query);
console.log("updateByLabelAndFilter", parameters);
const result = this.write(query, parameters, databaseOrTransaction);
return result;
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(error, common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
findChildrensByLabelsAsTree(root_labels = [], root_filters = {}, children_labels = [], children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const rootLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(root_labels);
const childrenLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
const cypher = `MATCH p=(n` +
(0, common_func_1.dynamicLabelAdder)(rootLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(root_filters) +
"-[:PARENT_OF*]->(m" +
(0, common_func_1.dynamicLabelAdder)(childrenLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(children_filters) +
` WITH COLLECT(p) AS ps CALL apoc.convert.toTree(ps) yield value RETURN value`;
children_filters = (0, common_func_1.changeObjectKeyName)(children_filters);
const parameters = Object.assign(Object.assign({}, root_filters), children_filters);
const result = yield this.read(cypher, parameters, databaseOrTransaction);
return result["records"][0]["_fields"][0];
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(custom_error_object_1.library_server_error, common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
findByLabelAndFiltersWithTreeStructure(root_labels = [], root_filters = {}, children_labels = [], children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const rootLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(root_labels);
const childrenLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
let tree = yield this.findChildrensByLabelsAsTree(rootLabelsWithoutEmptyString, root_filters, childrenLabelsWithoutEmptyString, children_filters);
if (!tree) {
throw new common_1.HttpException(custom_error_object_1.tree_structure_not_found_by_realm_name_error, 404);
}
else if (Object.keys(tree).length === 0) {
tree = yield this.findByLabelAndFilters(rootLabelsWithoutEmptyString, root_filters);
if (!tree.length) {
const rootNodeObject = { root: {} };
return rootNodeObject;
}
const rootNodeObject = { root: tree[0]["_fields"][0] };
return rootNodeObject;
}
else {
const rootNodeObject = { root: tree };
return rootNodeObject;
}
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(custom_error_object_1.library_server_error, common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
findChildrensByIdsAsTree(root_id, root_labels = [""], root_filters = {}, children_labels = [], children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const rootLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(root_labels);
const childrenLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
const rootNode = yield this.findByIdAndFilters(root_id, rootLabelsWithoutEmptyString, root_filters);
if (!rootNode) {
throw new common_1.HttpException(custom_error_object_1.find_with_children_by_realm_as_tree__find_by_realm_error, 404);
}
const rootId = rootNode.identity.low;
const cypher = `MATCH p=(n` +
(0, common_func_1.dynamicLabelAdder)(rootLabelsWithoutEmptyString) +
`)-[:PARENT_OF*]->(m` +
(0, common_func_1.dynamicLabelAdder)(childrenLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(children_filters) +
` WHERE id(n) = $rootId WITH COLLECT(p) AS ps CALL apoc.convert.toTree(ps) yield value RETURN value`;
children_filters["rootId"] = rootId;
const result = yield this.read(cypher, children_filters, databaseOrTransaction);
return result["records"][0]["_fields"][0];
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(custom_error_object_1.library_server_error, common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
findByIdAndFiltersWithTreeStructure(root_id, root_labels = [""], root_filters = {}, children_labels = [], children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const childrenLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
let tree = yield this.findChildrensByIdsAsTree(root_id, root_labels, root_filters, childrenLabelsWithoutEmptyString, children_filters);
if (!tree) {
throw new common_1.HttpException(custom_error_object_1.tree_structure_not_found_by_realm_name_error, 404);
}
else if (Object.keys(tree).length === 0) {
tree = yield this.findByIdAndFilters(root_id, root_labels, root_filters);
const rootNodeObject = { root: tree };
return rootNodeObject;
}
else {
const rootNodeObject = { root: tree };
return rootNodeObject;
}
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(custom_error_object_1.library_server_error, common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
getParentByIdAndFilters(id, node_labels = [""], node_filters = {}, parent_labels = [""], parent_filters = {}, relation_name, relation_filters, relation_depth, databaseOrTransaction) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
const nodeLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(node_labels);
const parentLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(parent_labels);
const node = yield this.findByIdAndFilters(+id, node_labels, node_filters);
if (!node) {
throw new common_1.HttpException(custom_error_object_1.node_not_found, 404);
}
const query = "MATCH (n" +
(0, common_func_1.dynamicLabelAdder)(nodeLabelsWithoutEmptyString) +
") where id(n)= $id match(m" +
(0, common_func_1.dynamicLabelAdder)(parentLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(parent_filters) +
"match (m)-" +
`[r:${relation_name}*1..${relation_depth}` +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(relation_filters, filter_properties_type_enum_1.FilterPropertiesType.RELATION) +
`]->(n) return m as parent,n as children`;
relation_filters = (0, common_func_1.changeObjectKeyName)(relation_filters);
const parameters = Object.assign(Object.assign({ id }, parent_filters), relation_filters);
const res = yield this.read(query, parameters, databaseOrTransaction);
if (!res["records"][0].length) {
throw new common_1.HttpException(custom_error_object_1.parent_of_child_not_found, 404);
}
return res["records"][0];
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: error.response.message, code: error.response.code }, error.status);
}
else {
throw new common_1.HttpException("library_server_error", common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
addRelationByLabelsAndFiltersAndRelationName(first_node_labels = [], first_node_properties = {}, second_node_labels = [], second_node_properties = {}, relation_name, relation_properties = {}, relation_direction = relation_direction_enum_1.RelationDirection.RIGHT, databaseOrTransaction) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
try {
if (!relation_name) {
throw new common_1.HttpException(custom_error_object_1.required_fields_must_entered, 404);
}
const firstNodelabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(first_node_labels);
const secondNodelabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(second_node_labels);
let cyper;
let res;
let parameters;
switch (relation_direction) {
case relation_direction_enum_1.RelationDirection.RIGHT:
cyper =
`MATCH (n` +
(0, common_func_1.dynamicLabelAdder)(firstNodelabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(first_node_properties) +
`MATCH (m` +
(0, common_func_1.dynamicLabelAdder)(secondNodelabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(second_node_properties) +
`MERGE (n)-[:${relation_name} ` +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(relation_properties, filter_properties_type_enum_1.FilterPropertiesType.RELATION, "3") +
`]-> (m) return n as parent,m as children`;
second_node_properties = (0, common_func_1.changeObjectKeyName)(second_node_properties);
relation_properties = (0, common_func_1.changeObjectKeyName)(relation_properties, "3");
parameters = Object.assign(Object.assign(Object.assign({}, second_node_properties), first_node_properties), relation_properties);
res = yield this.write(cyper, parameters, databaseOrTransaction);
break;
case relation_direction_enum_1.RelationDirection.LEFT:
cyper =
`MATCH (m` +
(0, common_func_1.dynamicLabelAdder)(firstNodelabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(first_node_properties) +
`MATCH (n` +
(0, common_func_1.dynamicLabelAdder)(secondNodelabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(second_node_properties) +
`MERGE (m)<-[:${relation_name}` +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(relation_properties, filter_properties_type_enum_1.FilterPropertiesType.RELATION, "3") +
`]- (n) return n as parent,m as children`;
second_node_properties = (0, common_func_1.changeObjectKeyName)(second_node_properties);
relation_properties = (0, common_func_1.changeObjectKeyName)(relation_properties, "3");
parameters = Object.assign(Object.assign(Object.assign({}, second_node_properties), first_node_properties), relation_properties);
res = yield this.write(cyper, parameters, databaseOrTransaction);
break;
default:
throw new common_1.HttpException(custom_error_object_1.invalid_direction_error, 400);
}
const { relationshipsCreated } = yield res.summary.updateStatistics.updates();
if (relationshipsCreated === 0) {
throw new common_1.HttpException(custom_error_object_1.add_relation_with_relation_name__create_relation_error, 400);
}
return res;
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: error.response.message, code: error.response.code }, error.status);
}
else {
throw new common_1.HttpException(error, common_1.HttpStatus.INTERNAL_SERVER_ERROR);
}
}
});
}
findChildrensByLabelsAsTreeOneLevel(root_labels = [], root_filters = {}, children_labels = [], children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const rootLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(root_labels);
const childrenLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
const cypher = `MATCH p=(n` +
(0, common_func_1.dynamicLabelAdder)(rootLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(root_filters) +
`-[:PARENT_OF]->(m` +
(0, common_func_1.dynamicLabelAdder)(childrenLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(children_filters) +
` WITH COLLECT(p) AS ps CALL apoc.convert.toTree(ps) yield value RETURN value`;
children_filters = (0, common_func_1.changeObjectKeyName)(children_filters);
const parameters = Object.assign(Object.assign({}, root_filters), children_filters);
const result = yield this.read(cypher, parameters, databaseOrTransaction);
return result["records"][0]["_fields"][0];
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(error, 500);
}
}
});
}
findByLabelAndFiltersWithTreeStructureOneLevel(root_labels = [], root_filters = {}, children_labels = [], children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const rootlabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(root_labels);
const childrenlabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
let tree = yield this.findChildrensByLabelsAsTreeOneLevel(rootlabelsWithoutEmptyString, root_filters, childrenlabelsWithoutEmptyString, children_filters);
if (!tree) {
throw new common_1.HttpException(custom_error_object_1.tree_structure_not_found_by_realm_name_error, 404);
}
else if (Object.keys(tree).length === 0) {
tree = yield this.findByLabelAndFilters(rootlabelsWithoutEmptyString, root_filters);
if (!tree.length) {
const rootNodeObject = { root: {} };
return rootNodeObject;
}
const rootNodeObject = { root: tree[0]["_fields"][0] };
return rootNodeObject;
}
else {
const rootNodeObject = { root: tree };
return rootNodeObject;
}
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(error, 500);
}
}
});
}
findChildrensByLabelsOneLevel(root_labels = [], root_filters = {}, children_labels = [], children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const rootLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(root_labels);
const childrenLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
const cypher = `MATCH p=(n` +
(0, common_func_1.dynamicLabelAdder)(rootLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(root_filters) +
`-[:PARENT_OF]->(m` +
(0, common_func_1.dynamicLabelAdder)(childrenLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(children_filters) +
` RETURN n as parent,m as children`;
children_filters = (0, common_func_1.changeObjectKeyName)(children_filters);
const parameters = Object.assign(Object.assign({}, root_filters), children_filters);
const result = yield this.read(cypher, parameters, databaseOrTransaction);
return result["records"];
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(error, 500);
}
}
});
}
findChildrensByIdsAsTreeOneLevel(id, root_labels = [""], root_filters = {}, children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
const rootNode = yield this.findByIdAndFilters(id, root_labels, root_filters);
if (!rootNode || rootNode.length == 0) {
throw new common_1.HttpException(custom_error_object_1.find_with_children_by_realm_as_tree__find_by_realm_error, 404);
}
const rootId = id;
const cypher = `MATCH p=(n` +
(0, common_func_1.dynamicLabelAdder)(rootNode.labels) +
`)-[:PARENT_OF]->(m` +
(0, common_func_1.dynamicFilterPropertiesAdder)(children_filters) +
` WHERE id(n) = $rootId WITH COLLECT(p) AS ps CALL apoc.convert.toTree(ps) yield value RETURN value`;
children_filters["rootId"] = rootId;
const result = yield this.read(cypher, children_filters, databaseOrTransaction);
return result["records"][0]["_fields"][0];
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(error, 500);
}
}
});
}
findByIdAndFiltersWithTreeStructureOneLevel(id, root_labels = [""], root_filters = {}, children_filters = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
let tree = yield this.findChildrensByIdsAsTreeOneLevel(id, root_labels, root_filters, children_filters);
if (!tree) {
throw new common_1.HttpException(custom_error_object_1.tree_structure_not_found_by_realm_name_error, 404);
}
else if (Object.keys(tree).length === 0) {
tree = yield this.findByIdAndFilters(id, root_labels, root_filters);
if (!tree.length) {
const rootNodeObject = { root: {} };
return rootNodeObject;
}
const rootNodeObject = { root: tree[0]["_fields"][0] };
return rootNodeObject;
}
else {
const rootNodeObject = { root: tree };
return rootNodeObject;
}
}
catch (error) {
if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.code) {
throw new common_1.HttpException({ message: (_b = error.response) === null || _b === void 0 ? void 0 : _b.message, code: (_c = error.response) === null || _c === void 0 ? void 0 : _c.code }, error.status);
}
else {
throw new common_1.HttpException(error, 500);
}
}
});
}
updateNodeChildrensByIdAndFilter(id, root_labels = [""], root_filters = {}, children_labels = [], children_filters = {}, relation_name, relation_filters = {}, relation_depth, update_labels = [], update_properties = {}, databaseOrTransaction) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
try {
if (!relation_name) {
throw new common_1.HttpException(custom_error_object_1.required_fields_must_entered, 404);
}
const rootLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(root_labels);
const updateLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(update_labels);
const childrenLabelsWithoutEmptyString = (0, common_func_1.filterArrayForEmptyString)(children_labels);
yield this.findByIdAndFilters(id, rootLabelsWithoutEmptyString, root_filters);
let query = `match (m` +
(0, common_func_1.dynamicLabelAdder)(rootLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(root_filters, filter_properties_type_enum_1.FilterPropertiesType.NODE, "3") +
`match(n` +
(0, common_func_1.dynamicLabelAdder)(childrenLabelsWithoutEmptyString) +
(0, common_func_1.dynamicFilterPropertiesAdder)(children_filters) +
` match (m)-[:${relation_name}*1..${relation_depth} ` +
(0, common_func_1.dynamicFilterPropertiesAdderAndAddParameterKey)(relation_filters, filter_properties_type_enum_1.FilterPropertiesType.RELATION, "2") +
`]->(n)` +
` where id(m)=$rootId set ` +
(0, common_func_1.dynamicUpdatePropertyAdderAndAddParameterKey)("n", update_properties);
if (updateLabelsWithoutEmptyString &&
updateLabelsWithoutEmptyString.length > 0) {
query =
query +
", n" +
(0, common_func_1.dynamicLabelAdder)(updateLabelsWithoutEmptyString) +
" return m as parent, n a