Skip to content

Commit 40cda2f

Browse files
committed
Future fix for filters clobbering each other
``` { node: { id: '' } } { node: { type: '' } } ``` The latter wins with former logic. Now each object is converted to cypher string, then joined together.
1 parent 6a1efbb commit 40cda2f

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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+
}

0 commit comments

Comments
 (0)