Skip to content

Commit cefb92f

Browse files
authored
Merge pull request #3252 from SeedCompany/bugfix/filter-merge
2 parents 5c4728a + 40cda2f commit cefb92f

File tree

7 files changed

+85
-43
lines changed

7 files changed

+85
-43
lines changed

.yarn/patches/cypher-query-builder.patch renamed to .yarn/patches/cypher-query-builder-npm-6.0.4-e8707a5e8e.patch

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/dist/cjs5.js b/dist/cjs5.js
2-
index 2956424bcab153c6d7a6f24f80ae7aac6a82d62a..108858d5ac65801739a3f022b07113ebb0ef68d9 100644
2+
index 2956424bcab153c6d7a6f24f80ae7aac6a82d62a..92921394496f972735ed0ff3ebd6a5c38ae0817f 100644
33
--- a/dist/cjs5.js
44
+++ b/dist/cjs5.js
55
@@ -1006,10 +1006,13 @@ var comparisions = {
@@ -18,6 +18,20 @@ index 2956424bcab153c6d7a6f24f80ae7aac6a82d62a..108858d5ac65801739a3f022b07113eb
1818
return parts.join(' ');
1919
};
2020
}
21+
@@ -3868,4 +3871,13 @@ exports.regexp = regexp;
22+
exports.relation = relation;
23+
exports.startsWith = startsWith;
24+
exports.xor = xor;
25+
+exports.WhereOp = WhereOp;
26+
+exports.stringCons = stringCons;
27+
+exports.Precedence = Precedence;
28+
+exports.Pattern = Pattern;
29+
+exports.PatternClause = PatternClause;
30+
+exports.Parameter = Parameter;
31+
+exports.ParameterBag = ParameterBag;
32+
+exports.ParameterContainer = ParameterContainer;
33+
+exports.TermListClause = TermListClause;
34+
//# sourceMappingURL=cjs5.js.map
2135
diff --git a/dist/typings/builder.d.ts b/dist/typings/builder.d.ts
2236
index 1c17b5c3f0127cd5aaf03185120e7f72321e2805..b91875030036d20f47d4dda0d9e3eeb22bb9949d 100644
2337
--- a/dist/typings/builder.d.ts
@@ -44,6 +58,21 @@ index 1c17b5c3f0127cd5aaf03185120e7f72321e2805..b91875030036d20f47d4dda0d9e3eeb2
4458
/**
4559
* Adds a [skip]{@link https://neo4j.com/docs/developer-manual/current/cypher/clauses/skip}
4660
* clause to the query.
61+
diff --git a/dist/typings/clauses/index.d.ts b/dist/typings/clauses/index.d.ts
62+
index 32d022ed1018f0c30d5033967facb17f4ea9ba6e..bc3b23b44359fb0cdad832da03ed1f11cc30ef54 100644
63+
--- a/dist/typings/clauses/index.d.ts
64+
+++ b/dist/typings/clauses/index.d.ts
65+
@@ -20,6 +20,10 @@ export { OrderBy } from './order-by';
66+
export { Merge } from './merge';
67+
export { OnMatch } from './on-match';
68+
export { OnCreate } from './on-create';
69+
+export { Pattern } from './pattern';
70+
+export { PatternClause } from './pattern-clause';
71+
+export { TermListClause } from './term-list-clause';
72+
+export {WhereOp, stringCons, Precedence} from './where-utils';
73+
export { and, or, xor, not, operators } from './where-operators';
74+
export { equals, greaterThan, greaterEqualTo, lessThan, lessEqualTo, startsWith, endsWith, contains, inArray, hasLabel, exists, between, isNull, regexp, comparisions, } from './where-comparators';
75+
/**
4776
diff --git a/dist/typings/clauses/where-comparators.d.ts b/dist/typings/clauses/where-comparators.d.ts
4877
index 386c07740c1a24bf3194e5fc59c390b3da500266..1a3027df7872fecbe7df4bd0f6d5c11bac637df6 100644
4978
--- a/dist/typings/clauses/where-comparators.d.ts
@@ -78,6 +107,16 @@ index ad53c58fc2a173403b0ad584ea95c1c26a579538..cc36d50cc8f577e2debe6043fa998e1e
78107
- stream<R = any>(query: Query): Observable<Dictionary<R>>;
79108
+ stream<R>(query: Query<R>): Observable<R>;
80109
}
110+
diff --git a/dist/typings/index.d.ts b/dist/typings/index.d.ts
111+
index e048614470c60a98cb4f34fd2e34b441d9b3befa..713aa03f4814f4913152870eac1ca413ad1e0026 100644
112+
--- a/dist/typings/index.d.ts
113+
+++ b/dist/typings/index.d.ts
114+
@@ -5,3 +5,5 @@ export * from './clause-collection';
115+
export * from './clauses';
116+
export * from './query';
117+
export * from './transformer';
118+
+export {Parameter, ParameterBag} from './parameter-bag';
119+
+export {ParameterContainer} from './parameter-container';
81120
diff --git a/dist/typings/query.d.ts b/dist/typings/query.d.ts
82121
index b31088b60c221927a701e16f03e4a3ffcfddb19f..4e1d858d9b089f915238c955e532ddefd928a0db 100644
83122
--- a/dist/typings/query.d.ts

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"clipanion": "^4.0.0-rc.3",
6060
"common-tags": "^1.8.2",
6161
"cookie-parser": "^1.4.6",
62-
"cypher-query-builder": "patch:cypher-query-builder@6.0.4#.yarn/patches/cypher-query-builder.patch",
62+
"cypher-query-builder": "patch:cypher-query-builder@npm%3A6.0.4#~/.yarn/patches/cypher-query-builder-npm-6.0.4-e8707a5e8e.patch",
6363
"dotenv": "^16.3.1",
6464
"dotenv-expand": "^10.0.0",
6565
"edgedb": "^1.6.0-canary.20240506T235920",

src/core/database/query-augmentation/condition-variables.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,7 @@
22
* This file patches pattern conditions to support referencing existing variables.
33
* This is achieved by wrapping the variable name in a `variable()` call.
44
*/
5-
import { Clause, NodePattern } from 'cypher-query-builder';
6-
import type { Pattern as TSPattern } from 'cypher-query-builder/dist/typings/clauses/pattern';
7-
import type {
8-
Parameter as TSParameter,
9-
ParameterBag as TSParameterBag,
10-
} from 'cypher-query-builder/dist/typings/parameter-bag';
11-
import type { ParameterContainer as TSParameterContainer } from 'cypher-query-builder/dist/typings/parameter-container';
12-
import { Class } from 'type-fest';
13-
14-
// This class is not exported so grab it a hacky way
15-
const ParameterContainer = Object.getPrototypeOf(
16-
Clause,
17-
) as Class<TSParameterContainer>;
18-
const ParameterBag = new ParameterContainer().getParameterBag()
19-
.constructor as Class<TSParameterBag>;
20-
const Parameter = new ParameterBag().addParam('')
21-
.constructor as Class<TSParameter>;
22-
const Pattern = Object.getPrototypeOf(NodePattern) as Class<TSPattern>;
5+
import { Parameter, ParameterBag, Pattern } from 'cypher-query-builder';
236

247
export class Variable extends Parameter {
258
constructor(variable: string, name = variable) {
@@ -36,9 +19,10 @@ export class Variable extends Parameter {
3619
*/
3720
export const variable = (expression: string) => new Variable(expression);
3821

22+
// eslint-disable-next-line @typescript-eslint/unbound-method
3923
const origAddParam = ParameterBag.prototype.addParam;
4024
ParameterBag.prototype.addParam = function addParam(
41-
this: TSParameterBag,
25+
this: ParameterBag,
4226
value: any | Variable,
4327
name?: string,
4428
) {
@@ -48,7 +32,7 @@ ParameterBag.prototype.addParam = function addParam(
4832
};
4933

5034
Pattern.prototype.setExpandedConditions = function (
51-
this: TSPattern,
35+
this: Pattern,
5236
expanded: boolean,
5337
) {
5438
if (this.useExpandedConditions !== expanded) {

src/core/database/query-augmentation/pattern-formatting.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ import {
77
Match,
88
Merge,
99
NodePattern,
10+
PatternClause,
1011
Raw,
1112
Return,
13+
TermListClause,
1214
With,
1315
} from 'cypher-query-builder';
14-
import type { PatternClause as TSPatternClause } from 'cypher-query-builder/dist/typings/clauses/pattern-clause';
15-
import type {
16-
Term,
17-
TermListClause as TSTermListClause,
18-
} from 'cypher-query-builder/dist/typings/clauses/term-list-clause';
16+
import type { Term } from 'cypher-query-builder/dist/typings/clauses/term-list-clause';
1917
import type { Parameter } from 'cypher-query-builder/dist/typings/parameter-bag';
2018
import { camelCase, isPlainObject } from 'lodash';
21-
import { Class } from 'type-fest';
2219
import { isExp } from '../query';
2320

2421
// Add line breaks for each pattern when there's multiple per statement
2522
// And ignore empty patterns
26-
const PatternClause = Object.getPrototypeOf(Create) as Class<TSPatternClause>;
2723
PatternClause.prototype.build = function build() {
2824
// @ts-expect-error private but we are using it
2925
const patternStrings = this.patterns.map((pattern) =>
@@ -32,9 +28,6 @@ PatternClause.prototype.build = function build() {
3228
return patternStrings.filter(isNotFalsy).join(',\n ');
3329
};
3430

35-
// This class is not exported so grab it a hacky way
36-
const TermListClause = Object.getPrototypeOf(With) as Class<TSTermListClause>;
37-
3831
// Change With & Return clauses to not alias as same variable since cypher as
3932
// problems with it sometimes.
4033
// e.g. .with({ node: 'node' })
@@ -76,7 +69,7 @@ TermListClause.prototype.stringifyTerm = function stringifyTerm(term: Term) {
7669
// If the term list clause has no terms render empty string instead of broken cypher
7770
for (const Cls of [With, Return]) {
7871
const origBuild = Cls.prototype.build;
79-
Cls.prototype.build = function build(this: TSTermListClause) {
72+
Cls.prototype.build = function build(this: TermListClause) {
8073
if (TermListClause.prototype.build.call(this) === '') {
8174
return '';
8275
}
@@ -92,7 +85,7 @@ Raw.prototype.build = function build() {
9285
// If the pattern clause has no patterns render empty string instead of broken cypher
9386
for (const Cls of [Match, Create, Merge]) {
9487
const origBuild = Cls.prototype.build;
95-
Cls.prototype.build = function build(this: TSPatternClause) {
88+
Cls.prototype.build = function build(this: PatternClause) {
9689
if (PatternClause.prototype.build.call(this) === '') {
9790
return '';
9891
}

src/core/database/query/filters.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import {
1111
} from 'cypher-query-builder';
1212
import { PatternCollection } from 'cypher-query-builder/dist/typings/clauses/pattern-clause';
1313
import { Comparator } from 'cypher-query-builder/dist/typings/clauses/where-comparators';
14-
import { AndConditions } from 'cypher-query-builder/src/clauses/where-utils';
1514
import { identity, isFunction } from 'lodash';
1615
import { AbstractClass, ConditionalKeys } from 'type-fest';
1716
import { DateTimeFilter } from '~/common';
1817
import { ACTIVE } from './matching';
18+
import { WhereAndList } from './where-and-list';
1919
import { path as pathPattern } from './where-path';
2020

2121
export type Builder<T, K extends keyof T = keyof T> = (
@@ -55,7 +55,7 @@ export const builder =
5555
const type = filters.constructor === Object ? null : filters.constructor;
5656
query.comment(type?.name ?? 'Filters');
5757

58-
let conditions: AndConditions = {};
58+
const conditions = [];
5959
const afters: Array<(query: Query) => Query> = [];
6060
for (const key of Object.keys(builders)) {
6161
const value = filters[key];
@@ -70,11 +70,11 @@ export const builder =
7070
afters.push(res);
7171
continue;
7272
}
73-
conditions = { ...conditions, ...res };
73+
conditions.push(res);
7474
}
7575

76-
if (Object.keys(conditions).length > 0) {
77-
query.where(conditions);
76+
if (conditions.length > 0) {
77+
query.where(new WhereAndList(conditions));
7878
}
7979
for (const after of afters) {
8080
after(query);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
ParameterBag,
3+
Precedence,
4+
stringCons,
5+
WhereOp,
6+
} from 'cypher-query-builder';
7+
import { AnyConditions } from 'cypher-query-builder/dist/typings/clauses/where-utils';
8+
9+
export class WhereAndList extends WhereOp {
10+
constructor(public conditions: AnyConditions[]) {
11+
super();
12+
}
13+
14+
evaluate(params: ParameterBag, precedence = Precedence.None, name = '') {
15+
// If this operator will not be used, precedence should not be altered
16+
const newPrecedence =
17+
this.conditions.length < 2 ? precedence : Precedence.And;
18+
const strings = this.conditions.map((condition) =>
19+
stringCons(params, condition, newPrecedence, name),
20+
);
21+
22+
const string = strings.join(' AND ');
23+
const braces = precedence !== 0 && precedence > newPrecedence;
24+
return braces ? `(${string})` : string;
25+
}
26+
}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5386,7 +5386,7 @@ __metadata:
53865386
clipanion: "npm:^4.0.0-rc.3"
53875387
common-tags: "npm:^1.8.2"
53885388
cookie-parser: "npm:^1.4.6"
5389-
cypher-query-builder: "patch:cypher-query-builder@6.0.4#.yarn/patches/cypher-query-builder.patch"
5389+
cypher-query-builder: "patch:cypher-query-builder@npm%3A6.0.4#~/.yarn/patches/cypher-query-builder-npm-6.0.4-e8707a5e8e.patch"
53905390
debugger-is-attached: "npm:^1.2.0"
53915391
dotenv: "npm:^16.3.1"
53925392
dotenv-expand: "npm:^10.0.0"
@@ -5589,9 +5589,9 @@ __metadata:
55895589
languageName: node
55905590
linkType: hard
55915591

5592-
"cypher-query-builder@patch:cypher-query-builder@6.0.4#.yarn/patches/cypher-query-builder.patch::locator=cord-api-v3%40workspace%3A.":
5592+
"cypher-query-builder@patch:cypher-query-builder@npm%3A6.0.4#~/.yarn/patches/cypher-query-builder-npm-6.0.4-e8707a5e8e.patch":
55935593
version: 6.0.4
5594-
resolution: "cypher-query-builder@patch:cypher-query-builder@npm%3A6.0.4#.yarn/patches/cypher-query-builder.patch::version=6.0.4&hash=4dba1c&locator=cord-api-v3%40workspace%3A."
5594+
resolution: "cypher-query-builder@patch:cypher-query-builder@npm%3A6.0.4#~/.yarn/patches/cypher-query-builder-npm-6.0.4-e8707a5e8e.patch::version=6.0.4&hash=5ae1ee"
55955595
dependencies:
55965596
"@types/lodash": "npm:^4.14.136"
55975597
"@types/node": "npm:^12.6.1"
@@ -5600,7 +5600,7 @@ __metadata:
56005600
node-cleanup: "npm:^2.1.2"
56015601
rxjs: "npm:^6.5.2"
56025602
tslib: "npm:^1.10.0"
5603-
checksum: 10c0/3d1e45fd19a40019426051f272a5455e8745a659dec80f0f0d64e39757249bf13f52c2a42f7525880af6c6327dd5fcfff55d9bb447f4e27c3360a3f71a23adf4
5603+
checksum: 10c0/018f61a14d85d2b82e403baa48ce6455cd7c7992cd1c70bbe6ad6c6b40b7a7fb8fd0acb78446513ef3e431e6f345ca41325425c324ca4263215bf9bbce7e01cb
56045604
languageName: node
56055605
linkType: hard
56065606

0 commit comments

Comments
 (0)