Skip to content

Commit 117a320

Browse files
committed
listComprehension in method overrides the expression if called twice
1 parent fa74345 commit 117a320

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

.changeset/bright-meals-arrive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@neo4j/cypher-builder": major
3+
---
4+
5+
ListComprehension `.in` method no longer throws if called twice. It will instead override the expression

src/expressions/list/ListComprehension.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,24 @@ describe("List comprehension", () => {
9191
`);
9292
});
9393

94-
test("Fails to set a expression twice", () => {
94+
test("Overrides if a expression is set twice", () => {
9595
const variable = new Cypher.Variable();
9696
const exprVariable = new Cypher.Param([1, 2, 5]);
97+
const exprVariable2 = new Cypher.Param([1, 3]);
9798

98-
expect(() => {
99-
new Cypher.ListComprehension(variable).in(exprVariable).in(exprVariable);
100-
}).toThrow("Cannot set 2 lists in list comprehension IN");
99+
const listComprehension = new Cypher.ListComprehension(variable).in(exprVariable).in(exprVariable2);
100+
101+
const queryResult = new TestClause(listComprehension).build();
102+
103+
expect(queryResult.cypher).toMatchInlineSnapshot(`"[var0 IN $param0]"`);
104+
expect(queryResult.params).toMatchInlineSnapshot(`
105+
{
106+
"param0": [
107+
1,
108+
3,
109+
],
110+
}
111+
`);
101112
});
102113

103114
test("Fails to build if no expression is set", () => {

src/expressions/list/ListComprehension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export class ListComprehension extends CypherASTNode {
4343
this.variable = variable;
4444
}
4545

46+
/** Sets the list expression to be used for the comprehension. If called twice, the expression will be overriden */
4647
public in(listExpr: Expr): this {
47-
if (this.listExpr) throw new Error("Cannot set 2 lists in list comprehension IN");
4848
this.listExpr = listExpr;
4949
return this;
5050
}

0 commit comments

Comments
 (0)