-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathQueryRunner.ts
339 lines (295 loc) · 10.3 KB
/
QueryRunner.ts
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import {
Driver,
QueryResult,
Session,
Transaction,
DateTime as Neo4jDateTime,
Date as Neo4jDate,
Point as Neo4jPoint,
Time as Neo4jTime,
Integer as Neo4jInteger,
LocalDateTime as Neo4jLocalDateTime,
LocalTime as Neo4jLocalTime,
Duration as Neo4jDuration,
} from 'neo4j-driver/types';
import * as uuid from 'uuid';
import { getRunnable } from '../../Sessions';
import { AnyWhereI, Where } from '../Where/Where';
import { trimWhitespace } from '../../utils/string';
import { QueryBuilder } from '../QueryBuilder';
type AnyObject = Record<string, any>;
/** the single types that Neo4j supports (not including an array of them) */
export type Neo4jSingleTypes =
| number
| Neo4jInteger
| string
| boolean
| Neo4jPoint
| Neo4jDate
| Neo4jTime
| Neo4jLocalTime
| Neo4jDateTime
| Neo4jLocalDateTime
| Neo4jDuration;
/** all the types that Neo4j supports (single or array) */
export type Neo4jSupportedTypes = Neo4jSingleTypes | Neo4jSingleTypes[];
export type Neo4jSupportedProperties = Record<
string,
Neo4jSupportedTypes | undefined
>;
/** can run queries, is either a Session or a Transaction */
export type Runnable = Session | Transaction;
export interface CreateRelationshipParamsI {
source: {
label?: string;
/** identifier to be used in the query. Defaults to the value of QueryRunner.identifiers.createRelationship.source */
identifier?: string;
};
target: {
label?: string;
/** identifier to be used in the query. Defaults to the value of QueryRunner.identifiers.createRelationship.target */
identifier?: string;
};
relationship: {
name: string;
direction: 'out' | 'in' | 'none';
/** properties to be set as relationship attributes */
properties?: AnyObject;
};
/** can access query identifiers by setting the "identifier" property of source/target, else by the values of QueryRunner.identifiers.createRelationship */
where?: AnyWhereI;
/** the session or transaction for running this query */
session?: Runnable | null;
}
export class QueryRunner {
private driver: Driver;
/** whether to log the statements and parameters with the given function */
private logger:
| null
| ((...val: Array<string | boolean | AnyObject | number>) => any);
constructor(params: {
driver: QueryRunner['driver'];
logger?: QueryRunner['logger'];
}) {
this.driver = params.driver;
this.logger = params?.logger || null;
}
public getDriver(): Driver {
return this.driver;
}
private log(...val: Array<string | boolean | AnyObject | number>) {
this.logger?.(...val);
}
public create = async <T>(params: {
/** the label of the nodes to create */
label: string;
/** the data to create */
data: T[];
/** identifier for the nodes */
identifier?: string;
/** the session or transaction for running this query */
session?: Runnable | null;
}): Promise<QueryResult> => {
const { label, data: options } = params;
const identifier = params.identifier || QueryRunner.identifiers.default;
const queryBuilder = new QueryBuilder()
.unwind('{options} as data')
.create({
identifier,
label,
})
.set(`${identifier} += data`)
.return(identifier);
// we won't use the queryBuilder bindParams as we've used "options" as a literal
const parameters = { options };
return this.run(
queryBuilder.getStatement(),
parameters,
params.session,
);
};
public update = async <T extends Neo4jSupportedProperties>(params: {
/** the label of the nodes to create */
label?: string;
/** the where object for matching the nodes to be edited */
data: Partial<T>;
/** the new data data, to be edited */
where?: AnyWhereI;
/** identifier for the nodes */
identifier?: string;
/** whether to return the nodes */
return?: boolean;
/** the session or transaction for running this query */
session?: Runnable | null;
}): Promise<QueryResult> => {
const { label } = params;
const data = params.data as Record<
string,
Neo4jSupportedTypes | undefined
>;
const identifier = params.identifier || QueryRunner.identifiers.default;
const where = Where.acquire(params.where);
const queryBuilder = new QueryBuilder(
/* clone the where bind param and construct one for the update, as there might be common keys between where and data */
where?.getBindParam().clone(),
);
queryBuilder.match({
identifier,
label,
});
if (where) {
queryBuilder.where(where);
}
queryBuilder.set({
identifier,
properties: data,
});
if (params.return) {
queryBuilder.return(identifier);
}
return queryBuilder.run(this, params.session);
};
public delete = async (params: {
label?: string;
where?: AnyWhereI;
identifier?: string;
/** detach relationships */
detach?: boolean;
/** the session or transaction for running this query */
session?: Runnable | null;
}): Promise<QueryResult> => {
const { label, detach } = params;
const where = Where.acquire(params.where);
const identifier = params.identifier || QueryRunner.identifiers.default;
const queryBuilder = new QueryBuilder(where?.getBindParam());
queryBuilder.match({
identifier,
label,
});
if (where) {
queryBuilder.where(where);
}
queryBuilder.delete({
identifiers: identifier,
detach,
});
return queryBuilder.run(this, params.session);
};
public createRelationship = async (
params: CreateRelationshipParamsI,
): Promise<QueryResult> => {
const { source, target, relationship } = params;
const where = Where.acquire(params.where);
const relationshipIdentifier = 'r';
const identifiers = {
source:
source.identifier ||
QueryRunner.identifiers.createRelationship.source,
target:
target.identifier ||
QueryRunner.identifiers.createRelationship.target,
};
const queryBuilder = new QueryBuilder(
/** the params of the relationship value */
where?.getBindParam()?.clone(),
);
queryBuilder.match({
multiple: [
{
identifier: identifiers.source,
label: source.label,
},
{
identifier: identifiers.target,
label: target.label,
},
],
});
if (where) {
queryBuilder.where(where);
}
queryBuilder.create({
related: [
{
identifier: identifiers.source,
},
{
direction: relationship.direction,
name: relationship.name,
identifier: relationshipIdentifier,
},
{
identifier: identifiers.target,
},
],
});
const relationshipProperties = params.relationship.properties;
if (
relationshipProperties &&
Object.keys(relationshipProperties).length
) {
/** the relationship properties statement to be inserted into the final statement string */
queryBuilder.set({
identifier: relationshipIdentifier,
properties: relationshipProperties,
});
}
return queryBuilder.run(this, params.session);
};
/** maps a session object to a uuid, for logging purposes */
private sessionIdentifiers = new WeakMap<Runnable, string>([]);
/** runs a statement */
public run(
/** the statement to run */
statement: string,
/** parameters for the query */
parameters?: Record<string, any>,
/** the session or transaction for running this query */
existingSession?: Runnable | null,
): Promise<QueryResult> {
return getRunnable(
existingSession,
async (session) => {
parameters = parameters || {};
/** an identifier to be used for logging purposes */
let sessionIdentifier = 'Default';
const existingSessionIdentifier = this.sessionIdentifiers.get(
session,
);
if (existingSessionIdentifier) {
sessionIdentifier = existingSessionIdentifier;
} else {
sessionIdentifier = uuid.v4();
this.sessionIdentifiers.set(session, sessionIdentifier);
}
const trimmedStatement = trimWhitespace(statement);
this.log(sessionIdentifier);
this.log(`\tStatement:`, trimmedStatement);
this.log(`\tParameters:`, parameters);
return session.run(trimmedStatement, parameters);
},
this.driver,
);
}
/** default identifiers for the queries */
public static readonly identifiers = {
/** general purpose default identifier */
default: 'nodes',
/** default identifiers for createRelationship */
createRelationship: {
/** default identifier for the source node */
source: 'source',
/** default identifier for the target node */
target: 'target',
},
};
public static getResultProperties = <T>(
result: QueryResult,
identifier: string,
): T[] => {
return result.records.map((v) => v.get(identifier).properties);
};
public static getNodesDeleted = (result: QueryResult): number => {
return result.summary.counters.updates().nodesDeleted;
};
}