Skip to content

Commit 6a1efbb

Browse files
committed
Simplify imports to use patched exports
1 parent beda779 commit 6a1efbb

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

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
}

0 commit comments

Comments
 (0)