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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 | 9x
9x
9x
9x
104x
104x
81x
44x
104x
5x
99x
9x
13x
13x
13x
13x
13x
13x
13x
9x
4x
4x
4x
4x
9x
80x
80x
80x
138x
138x
138x
80x
9x
1x
1x
1x
2x
2x
1x
9x
3x
3x
3x
3x
3x
9x
3x
3x
3x
3x
3x
| import neo4js, { ModelInstance } from "./index";
import { prepareWhere, prepareSet } from "./utils";
import { keys } from "lodash";
import { RelationType } from "./Relation";
import { Neo4jResultStats } from "./Types";
const relationPropsKey = "relationProps";
function getRelationString(
label: string,
relationType: RelationType,
relationProps?: any
) {
let relationPropsStr = "";
if (relationProps) {
relationPropsStr = keys(relationProps)
.map(key => `${key}: {${relationPropsKey}}.${key}`)
.join(", ");
}
if (relationType.any) {
return `-[r:${label} {${relationPropsStr}}]-`;
}
return `${
!relationType.out && !relationType.any ? "<" : ""
}-[r:${label} {${relationPropsStr}}]-${
!relationType.out && !relationType.any ? "" : ">"
}`;
}
export async function get(
instance: ModelInstance<any>,
label: string,
relationType: RelationType,
props: any,
relationProps?: any
): Promise<any> {
const { where, flatProps } = prepareWhere({ b: props, r: relationProps }, [
"b",
"r",
]);
const relationString = getRelationString(label, relationType);
const result = await neo4js.run(
`
MATCH (a:${this.src.label} {guid:{_srcGuid}})${relationString}(b:${
this.dest.label
})
${where}
RETURN b, r
`,
{ _srcGuid: instance.props.guid, ...flatProps }
);
return Promise.resolve(
result.map(p => {
const instance = this.dest._createModelInstance(p.b);
instance.relationProps = p.r;
return instance;
})
);
}
export async function remove(
instance: ModelInstance<any>,
label: string,
relationType: RelationType,
props: any,
relationProps?: any
): Promise<Neo4jResultStats> {
const { where, flatProps } = prepareWhere({ b: props, r: relationProps }, [
"b",
"r",
]);
const relationString = getRelationString(label, relationType);
const result = await neo4js.run(
`
MATCH (a:${this.src.label} {guid:{_srcGuid}})${relationString}(b:${
this.dest.label
})
${where}
DELETE r
`,
{ _srcGuid: instance.props.guid, ...flatProps }
);
return Promise.resolve(result._stats);
}
export async function create(
instance: ModelInstance<any>,
label: string,
relationType: RelationType,
propsArray: any[],
relationProps?: any
): Promise<any> {
const destInstances = [];
const relationString = getRelationString(label, relationType, relationProps);
for (const props of propsArray) {
const destInstance = await this.dest.create(props);
destInstances.push(destInstance);
await neo4js.run(
`
MATCH
(a:${this.src.label} {guid:{srcGuid}}),
(b:${this.dest.label} {guid:{destGuid}})
MERGE (a)${relationString}(b)
`,
{
srcGuid: instance.props.guid,
destGuid: destInstance.props.guid,
relationProps,
}
);
}
return Promise.resolve(destInstances);
}
export async function add(
instance: ModelInstance<any>,
label: string,
relationType: RelationType,
instances: ModelInstance<any>[],
relationProps?: any
): Promise<number> {
const relationString = getRelationString(label, relationType, relationProps);
let relationshipsCreated = 0;
for (const destInstance of instances) {
const result = await neo4js.run(
`
MATCH
(a:${this.src.label} {guid:{srcGuid}}),
(b:${this.dest.label} {guid:{destGuid}})
MERGE (a)${relationString}(b)
`,
{
srcGuid: instance.props.guid,
destGuid: destInstance.props.guid,
relationProps,
}
);
relationshipsCreated += result._stats.relationshipsCreated;
}
return Promise.resolve(relationshipsCreated);
}
export async function count(
instance: ModelInstance<any>,
label: string,
relationType: RelationType,
props: any,
relationProps?: any
): Promise<number> {
const { where, flatProps } = prepareWhere({ b: props, r: relationProps }, [
"b",
"r",
]);
const relationString = getRelationString(label, relationType);
const result = await neo4js.run(
`
MATCH (a:${this.src.label} {guid:{_srcGuid}})${relationString}(b:${
this.dest.label
})
${where}
RETURN COUNT(b) as b
`,
{ _srcGuid: instance.props.guid, ...flatProps }
);
const low = result._raw.records[0]._fields[0].low || -1;
return Promise.resolve(low);
}
export async function update(
instance: ModelInstance<any>,
label: string,
relationType: RelationType,
props: any,
whereProps: any,
relationProps?: any,
whereRelationProps?: any
): Promise<any> {
const { where, flatProps } = prepareWhere(
{ b: whereProps, r: whereRelationProps },
["b", "r"]
);
const { str: setPropsStr, newProps } = prepareSet(
{
b: props,
r: relationProps,
},
["b", "r"]
);
const relationString = getRelationString(label, relationType);
const result = await neo4js.run(
`
MATCH (a:${this.src.label} {guid:{_srcGuid}})${relationString}(b:${
this.dest.label
})
${where}
SET ${setPropsStr}
RETURN b
`,
{ _srcGuid: instance.props.guid, ...flatProps, ...newProps }
);
return Promise.resolve(result.map(a => this.dest._createModelInstance(a.b)));
}
|