-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmemgraphGraphStore.ts
More file actions
181 lines (165 loc) · 5.99 KB
/
memgraphGraphStore.ts
File metadata and controls
181 lines (165 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { Entity, Relationship } from '@jupiterone/integration-sdk-core';
import {
sanitizeValue,
buildPropertyParameters,
sanitizePropertyName,
getFromTypeLabel,
getToTypeLabel,
} from './memgraphUtilities';
import * as memgraph from 'neo4j-driver';
export interface MemgraphGraphObjectStoreParams {
uri: string;
username: string;
password: string;
integrationInstanceID: string;
session?: memgraph.Session;
database?: string;
}
export class MemgraphGraphStore {
private memgraphDriver: memgraph.Driver;
private persistedSession: memgraph.Session;
private databaseName = 'memgraph';
private typeList = new Set<string>();
private integrationInstanceID: string;
constructor(params: MemgraphGraphObjectStoreParams) {
if (params.session) {
this.persistedSession = params.session;
} else {
this.memgraphDriver = memgraph.driver(
params.uri,
memgraph.auth.basic(params.username, params.password),
);
}
this.integrationInstanceID = params.integrationInstanceID;
if (params.database) {
this.databaseName = params.database;
}
}
private async runCypherCommand(
cypherCommand: string,
cypherParameters?: any,
): Promise<memgraph.Result> {
if (this.persistedSession) {
const result = await this.persistedSession.run(cypherCommand);
return result;
} else {
const session = this.memgraphDriver.session({
database: this.databaseName,
defaultAccessMode: memgraph.session.WRITE,
});
const result = await session.run(cypherCommand, cypherParameters);
await session.close();
return result;
}
}
async addEntities(newEntities: Entity[]) {
const nodeAlias: string = 'entityNode';
const promiseArray: Promise<memgraph.Result>[] = [];
for (const entity of newEntities) {
let classLabels = '';
if (entity._class) {
if (typeof entity._class === 'string') {
classLabels += `:${sanitizePropertyName(entity._class)}`;
} else {
for (const className of entity._class) {
classLabels += `:${sanitizePropertyName(className)}`;
}
}
}
if (!this.typeList.has(entity._type)) {
await this.runCypherCommand(`CREATE INDEX ON :${entity._type}(_key);`);
await this.runCypherCommand(`CREATE INDEX ON :${entity._type}(_integrationInstanceID);`);
this.typeList.add(entity._type);
}
const sanitizedType = sanitizePropertyName(entity._type);
const propertyParameters = buildPropertyParameters(entity);
const finalKeyValue = sanitizeValue(entity._key.toString());
const buildCommand = `
MERGE (${nodeAlias} {_key: $finalKeyValue, _integrationInstanceID: $integrationInstanceID})
SET ${nodeAlias} += $propertyParameters
SET ${nodeAlias}:${sanitizedType}${classLabels};`;
promiseArray.push(
this.runCypherCommand(buildCommand, {
propertyParameters: propertyParameters,
finalKeyValue: finalKeyValue,
integrationInstanceID: this.integrationInstanceID,
}),
);
}
await Promise.all(promiseArray);
}
async addRelationships(newRelationships: Relationship[]) {
const promiseArray: Promise<memgraph.Result>[] = [];
for (const relationship of newRelationships) {
const relationshipAlias: string = 'relationship';
const propertyParameters = buildPropertyParameters(relationship);
let startEntityKey = '';
let endEntityKey = '';
if (relationship._fromEntityKey) {
startEntityKey = sanitizeValue(relationship._fromEntityKey.toString());
}
if (relationship._toEntityKey) {
endEntityKey = sanitizeValue(relationship._toEntityKey.toString());
}
//Attempt to get start and end types
const startEntityTypeLabel = getFromTypeLabel(relationship);
const endEntityTypeLabel = getToTypeLabel(relationship);
if (relationship._mapping) {
//Mapped Relationship
if (relationship._mapping['skipTargetCreation'] === false) {
const targetEntity = relationship._mapping['targetEntity'];
//Create target entity first
const tempEntity: Entity = {
...targetEntity,
_class: targetEntity._class,
_key: sanitizeValue(
relationship._key.replace(
relationship._mapping['sourceEntityKey'],
'',
),
),
_type: targetEntity._type,
};
await this.addEntities([tempEntity]);
}
startEntityKey = sanitizeValue(
relationship._mapping['sourceEntityKey'],
);
endEntityKey = sanitizeValue(
relationship._key.replace(
relationship._mapping['sourceEntityKey'],
'',
),
);
}
const sanitizedRelationshipClass = sanitizePropertyName(
relationship._class,
);
const buildCommand = `
MERGE (start${startEntityTypeLabel} {_key: $startEntityKey, _integrationInstanceID: $integrationInstanceID})
MERGE (end${endEntityTypeLabel} {_key: $endEntityKey, _integrationInstanceID: $integrationInstanceID})
MERGE (start)-[${relationshipAlias}:${sanitizedRelationshipClass}]->(end)
SET ${relationshipAlias} += $propertyParameters;`;
promiseArray.push(
this.runCypherCommand(buildCommand, {
propertyParameters: propertyParameters,
startEntityKey: startEntityKey,
endEntityKey: endEntityKey,
integrationInstanceID: this.integrationInstanceID,
}),
);
}
await Promise.all(promiseArray);
}
async wipeInstanceIdData() {
const wipeCypherCommand = `MATCH (n {_integrationInstanceID: '${this.integrationInstanceID}'}) DETACH DELETE n`;
await this.runCypherCommand(wipeCypherCommand);
}
async wipeDatabase() {
const wipeCypherCommand = `MATCH (n) DETACH DELETE n`;
await this.runCypherCommand(wipeCypherCommand);
}
async close() {
await this.memgraphDriver.close();
}
}