Skip to content

Commit

Permalink
Merge pull request #3252 from SeedCompany/bugfix/filter-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Jun 21, 2024
2 parents 5c4728a + 40cda2f commit cefb92f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/dist/cjs5.js b/dist/cjs5.js
index 2956424bcab153c6d7a6f24f80ae7aac6a82d62a..108858d5ac65801739a3f022b07113ebb0ef68d9 100644
index 2956424bcab153c6d7a6f24f80ae7aac6a82d62a..92921394496f972735ed0ff3ebd6a5c38ae0817f 100644
--- a/dist/cjs5.js
+++ b/dist/cjs5.js
@@ -1006,10 +1006,13 @@ var comparisions = {
Expand All @@ -18,6 +18,20 @@ index 2956424bcab153c6d7a6f24f80ae7aac6a82d62a..108858d5ac65801739a3f022b07113eb
return parts.join(' ');
};
}
@@ -3868,4 +3871,13 @@ exports.regexp = regexp;
exports.relation = relation;
exports.startsWith = startsWith;
exports.xor = xor;
+exports.WhereOp = WhereOp;
+exports.stringCons = stringCons;
+exports.Precedence = Precedence;
+exports.Pattern = Pattern;
+exports.PatternClause = PatternClause;
+exports.Parameter = Parameter;
+exports.ParameterBag = ParameterBag;
+exports.ParameterContainer = ParameterContainer;
+exports.TermListClause = TermListClause;
//# sourceMappingURL=cjs5.js.map
diff --git a/dist/typings/builder.d.ts b/dist/typings/builder.d.ts
index 1c17b5c3f0127cd5aaf03185120e7f72321e2805..b91875030036d20f47d4dda0d9e3eeb22bb9949d 100644
--- a/dist/typings/builder.d.ts
Expand All @@ -44,6 +58,21 @@ index 1c17b5c3f0127cd5aaf03185120e7f72321e2805..b91875030036d20f47d4dda0d9e3eeb2
/**
* Adds a [skip]{@link https://neo4j.com/docs/developer-manual/current/cypher/clauses/skip}
* clause to the query.
diff --git a/dist/typings/clauses/index.d.ts b/dist/typings/clauses/index.d.ts
index 32d022ed1018f0c30d5033967facb17f4ea9ba6e..bc3b23b44359fb0cdad832da03ed1f11cc30ef54 100644
--- a/dist/typings/clauses/index.d.ts
+++ b/dist/typings/clauses/index.d.ts
@@ -20,6 +20,10 @@ export { OrderBy } from './order-by';
export { Merge } from './merge';
export { OnMatch } from './on-match';
export { OnCreate } from './on-create';
+export { Pattern } from './pattern';
+export { PatternClause } from './pattern-clause';
+export { TermListClause } from './term-list-clause';
+export {WhereOp, stringCons, Precedence} from './where-utils';
export { and, or, xor, not, operators } from './where-operators';
export { equals, greaterThan, greaterEqualTo, lessThan, lessEqualTo, startsWith, endsWith, contains, inArray, hasLabel, exists, between, isNull, regexp, comparisions, } from './where-comparators';
/**
diff --git a/dist/typings/clauses/where-comparators.d.ts b/dist/typings/clauses/where-comparators.d.ts
index 386c07740c1a24bf3194e5fc59c390b3da500266..1a3027df7872fecbe7df4bd0f6d5c11bac637df6 100644
--- a/dist/typings/clauses/where-comparators.d.ts
Expand Down Expand Up @@ -78,6 +107,16 @@ index ad53c58fc2a173403b0ad584ea95c1c26a579538..cc36d50cc8f577e2debe6043fa998e1e
- stream<R = any>(query: Query): Observable<Dictionary<R>>;
+ stream<R>(query: Query<R>): Observable<R>;
}
diff --git a/dist/typings/index.d.ts b/dist/typings/index.d.ts
index e048614470c60a98cb4f34fd2e34b441d9b3befa..713aa03f4814f4913152870eac1ca413ad1e0026 100644
--- a/dist/typings/index.d.ts
+++ b/dist/typings/index.d.ts
@@ -5,3 +5,5 @@ export * from './clause-collection';
export * from './clauses';
export * from './query';
export * from './transformer';
+export {Parameter, ParameterBag} from './parameter-bag';
+export {ParameterContainer} from './parameter-container';
diff --git a/dist/typings/query.d.ts b/dist/typings/query.d.ts
index b31088b60c221927a701e16f03e4a3ffcfddb19f..4e1d858d9b089f915238c955e532ddefd928a0db 100644
--- a/dist/typings/query.d.ts
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"clipanion": "^4.0.0-rc.3",
"common-tags": "^1.8.2",
"cookie-parser": "^1.4.6",
"cypher-query-builder": "patch:cypher-query-builder@6.0.4#.yarn/patches/cypher-query-builder.patch",
"cypher-query-builder": "patch:cypher-query-builder@npm%3A6.0.4#~/.yarn/patches/cypher-query-builder-npm-6.0.4-e8707a5e8e.patch",
"dotenv": "^16.3.1",
"dotenv-expand": "^10.0.0",
"edgedb": "^1.6.0-canary.20240506T235920",
Expand Down
24 changes: 4 additions & 20 deletions src/core/database/query-augmentation/condition-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,7 @@
* This file patches pattern conditions to support referencing existing variables.
* This is achieved by wrapping the variable name in a `variable()` call.
*/
import { Clause, NodePattern } from 'cypher-query-builder';
import type { Pattern as TSPattern } from 'cypher-query-builder/dist/typings/clauses/pattern';
import type {
Parameter as TSParameter,
ParameterBag as TSParameterBag,
} from 'cypher-query-builder/dist/typings/parameter-bag';
import type { ParameterContainer as TSParameterContainer } from 'cypher-query-builder/dist/typings/parameter-container';
import { Class } from 'type-fest';

// This class is not exported so grab it a hacky way
const ParameterContainer = Object.getPrototypeOf(
Clause,
) as Class<TSParameterContainer>;
const ParameterBag = new ParameterContainer().getParameterBag()
.constructor as Class<TSParameterBag>;
const Parameter = new ParameterBag().addParam('')
.constructor as Class<TSParameter>;
const Pattern = Object.getPrototypeOf(NodePattern) as Class<TSPattern>;
import { Parameter, ParameterBag, Pattern } from 'cypher-query-builder';

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

// eslint-disable-next-line @typescript-eslint/unbound-method
const origAddParam = ParameterBag.prototype.addParam;
ParameterBag.prototype.addParam = function addParam(
this: TSParameterBag,
this: ParameterBag,
value: any | Variable,
name?: string,
) {
Expand All @@ -48,7 +32,7 @@ ParameterBag.prototype.addParam = function addParam(
};

Pattern.prototype.setExpandedConditions = function (
this: TSPattern,
this: Pattern,
expanded: boolean,
) {
if (this.useExpandedConditions !== expanded) {
Expand Down
17 changes: 5 additions & 12 deletions src/core/database/query-augmentation/pattern-formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,19 @@ import {
Match,
Merge,
NodePattern,
PatternClause,
Raw,
Return,
TermListClause,
With,
} from 'cypher-query-builder';
import type { PatternClause as TSPatternClause } from 'cypher-query-builder/dist/typings/clauses/pattern-clause';
import type {
Term,
TermListClause as TSTermListClause,
} from 'cypher-query-builder/dist/typings/clauses/term-list-clause';
import type { Term } from 'cypher-query-builder/dist/typings/clauses/term-list-clause';
import type { Parameter } from 'cypher-query-builder/dist/typings/parameter-bag';
import { camelCase, isPlainObject } from 'lodash';
import { Class } from 'type-fest';
import { isExp } from '../query';

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

// This class is not exported so grab it a hacky way
const TermListClause = Object.getPrototypeOf(With) as Class<TSTermListClause>;

// Change With & Return clauses to not alias as same variable since cypher as
// problems with it sometimes.
// e.g. .with({ node: 'node' })
Expand Down Expand Up @@ -76,7 +69,7 @@ TermListClause.prototype.stringifyTerm = function stringifyTerm(term: Term) {
// If the term list clause has no terms render empty string instead of broken cypher
for (const Cls of [With, Return]) {
const origBuild = Cls.prototype.build;
Cls.prototype.build = function build(this: TSTermListClause) {
Cls.prototype.build = function build(this: TermListClause) {
if (TermListClause.prototype.build.call(this) === '') {
return '';
}
Expand All @@ -92,7 +85,7 @@ Raw.prototype.build = function build() {
// If the pattern clause has no patterns render empty string instead of broken cypher
for (const Cls of [Match, Create, Merge]) {
const origBuild = Cls.prototype.build;
Cls.prototype.build = function build(this: TSPatternClause) {
Cls.prototype.build = function build(this: PatternClause) {
if (PatternClause.prototype.build.call(this) === '') {
return '';
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/database/query/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
} from 'cypher-query-builder';
import { PatternCollection } from 'cypher-query-builder/dist/typings/clauses/pattern-clause';
import { Comparator } from 'cypher-query-builder/dist/typings/clauses/where-comparators';
import { AndConditions } from 'cypher-query-builder/src/clauses/where-utils';
import { identity, isFunction } from 'lodash';
import { AbstractClass, ConditionalKeys } from 'type-fest';
import { DateTimeFilter } from '~/common';
import { ACTIVE } from './matching';
import { WhereAndList } from './where-and-list';
import { path as pathPattern } from './where-path';

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

let conditions: AndConditions = {};
const conditions = [];
const afters: Array<(query: Query) => Query> = [];
for (const key of Object.keys(builders)) {
const value = filters[key];
Expand All @@ -70,11 +70,11 @@ export const builder =
afters.push(res);
continue;
}
conditions = { ...conditions, ...res };
conditions.push(res);
}

if (Object.keys(conditions).length > 0) {
query.where(conditions);
if (conditions.length > 0) {
query.where(new WhereAndList(conditions));
}
for (const after of afters) {
after(query);
Expand Down
26 changes: 26 additions & 0 deletions src/core/database/query/where-and-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
ParameterBag,
Precedence,
stringCons,
WhereOp,
} from 'cypher-query-builder';
import { AnyConditions } from 'cypher-query-builder/dist/typings/clauses/where-utils';

export class WhereAndList extends WhereOp {
constructor(public conditions: AnyConditions[]) {
super();
}

evaluate(params: ParameterBag, precedence = Precedence.None, name = '') {
// If this operator will not be used, precedence should not be altered
const newPrecedence =
this.conditions.length < 2 ? precedence : Precedence.And;
const strings = this.conditions.map((condition) =>
stringCons(params, condition, newPrecedence, name),
);

const string = strings.join(' AND ');
const braces = precedence !== 0 && precedence > newPrecedence;
return braces ? `(${string})` : string;
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5386,7 +5386,7 @@ __metadata:
clipanion: "npm:^4.0.0-rc.3"
common-tags: "npm:^1.8.2"
cookie-parser: "npm:^1.4.6"
cypher-query-builder: "patch:cypher-query-builder@6.0.4#.yarn/patches/cypher-query-builder.patch"
cypher-query-builder: "patch:cypher-query-builder@npm%3A6.0.4#~/.yarn/patches/cypher-query-builder-npm-6.0.4-e8707a5e8e.patch"
debugger-is-attached: "npm:^1.2.0"
dotenv: "npm:^16.3.1"
dotenv-expand: "npm:^10.0.0"
Expand Down Expand Up @@ -5589,9 +5589,9 @@ __metadata:
languageName: node
linkType: hard

"cypher-query-builder@patch:cypher-query-builder@6.0.4#.yarn/patches/cypher-query-builder.patch::locator=cord-api-v3%40workspace%3A.":
"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
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."
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"
dependencies:
"@types/lodash": "npm:^4.14.136"
"@types/node": "npm:^12.6.1"
Expand All @@ -5600,7 +5600,7 @@ __metadata:
node-cleanup: "npm:^2.1.2"
rxjs: "npm:^6.5.2"
tslib: "npm:^1.10.0"
checksum: 10c0/3d1e45fd19a40019426051f272a5455e8745a659dec80f0f0d64e39757249bf13f52c2a42f7525880af6c6327dd5fcfff55d9bb447f4e27c3360a3f71a23adf4
checksum: 10c0/018f61a14d85d2b82e403baa48ce6455cd7c7992cd1c70bbe6ad6c6b40b7a7fb8fd0acb78446513ef3e431e6f345ca41325425c324ca4263215bf9bbce7e01cb
languageName: node
linkType: hard

Expand Down

0 comments on commit cefb92f

Please sign in to comment.