Skip to content

Commit f8dd463

Browse files
authored
Merge pull request #600 from neo4j/set-remove-in-with
Add support for set and remove after with
2 parents c65f51d + 074ea4a commit f8dd463

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

.changeset/rare-mails-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@neo4j/cypher-builder": minor
3+
---
4+
5+
Add support for `.set` and `.remove` in Cypher.With

src/clauses/With.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,65 @@ RETURN this1"
317317
expect(params).toMatchInlineSnapshot(`{}`);
318318
});
319319
});
320+
321+
describe("With update", () => {
322+
test("Match and update movie", () => {
323+
const nameParam = new Cypher.Param("Keanu Reeves");
324+
const evilKeanu = new Cypher.Param("Seveer unaeK");
325+
326+
const personNode = new Cypher.Node();
327+
328+
const withQuery = new Cypher.Match(new Cypher.Pattern(personNode, { labels: ["Person"] }))
329+
.with(personNode)
330+
.where(personNode, { name: nameParam })
331+
.set([personNode.property("name"), evilKeanu])
332+
.return(personNode);
333+
334+
const queryResult = withQuery.build();
335+
expect(queryResult.cypher).toMatchInlineSnapshot(`
336+
"MATCH (this0:Person)
337+
WITH this0
338+
WHERE this0.name = $param0
339+
SET
340+
this0.name = $param1
341+
RETURN this0"
342+
`);
343+
344+
expect(queryResult.params).toMatchInlineSnapshot(`
345+
{
346+
"param0": "Keanu Reeves",
347+
"param1": "Seveer unaeK",
348+
}
349+
`);
350+
});
351+
352+
test("Match with remove", () => {
353+
const idParam = new Cypher.Param("my-id");
354+
const nameParam = new Cypher.Param("my-name");
355+
356+
const movieNode = new Cypher.Node();
357+
358+
const matchQuery = new Cypher.Match(new Cypher.Pattern(movieNode, { labels: ["Movie"] }))
359+
.with("*")
360+
.where(movieNode, { id: idParam, name: nameParam })
361+
.remove(movieNode.property("name"))
362+
.return(movieNode.property("id"));
363+
364+
const queryResult = matchQuery.build();
365+
expect(queryResult.cypher).toMatchInlineSnapshot(`
366+
"MATCH (this0:Movie)
367+
WITH *
368+
WHERE (this0.id = $param0 AND this0.name = $param1)
369+
REMOVE this0.name
370+
RETURN this0.id"
371+
`);
372+
373+
expect(queryResult.params).toMatchInlineSnapshot(`
374+
{
375+
"param0": "my-id",
376+
"param1": "my-name",
377+
}
378+
`);
379+
});
380+
});
320381
});

src/clauses/With.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { WithReturn } from "./mixins/clauses/WithReturn";
3232
import { WithUnwind } from "./mixins/clauses/WithUnwind";
3333
import { WithDelete } from "./mixins/sub-clauses/WithDelete";
3434
import { WithOrder } from "./mixins/sub-clauses/WithOrder";
35+
import { WithSetRemove } from "./mixins/sub-clauses/WithSetRemove";
3536
import { WithWhere } from "./mixins/sub-clauses/WithWhere";
3637
import { Projection } from "./sub-clauses/Projection";
3738
import { mixin } from "./utils/mixin";
@@ -44,6 +45,7 @@ export interface With
4445
extends WithOrder,
4546
WithReturn,
4647
WithWhere,
48+
WithSetRemove,
4749
WithDelete,
4850
WithMatch,
4951
WithUnwind,
@@ -60,6 +62,7 @@ export interface With
6062
WithOrder,
6163
WithReturn,
6264
WithWhere,
65+
WithSetRemove,
6366
WithDelete,
6467
WithMatch,
6568
WithUnwind,
@@ -106,12 +109,13 @@ export class With extends Clause {
106109
const orderByStr = compileCypherIfExists(this.orderByStatement, env, { prefix: "\n" });
107110
const withStr = compileCypherIfExists(this.withStatement, env, { prefix: "\n" });
108111
const whereStr = compileCypherIfExists(this.whereSubClause, env, { prefix: "\n" });
112+
const setCypher = this.compileSetCypher(env);
109113
const deleteStr = compileCypherIfExists(this.deleteClause, env, { prefix: "\n" });
110114
const distinctStr = this.isDistinct ? " DISTINCT" : "";
111115

112116
const nextClause = this.compileNextClause(env);
113117

114-
return `WITH${distinctStr} ${projectionStr}${whereStr}${orderByStr}${deleteStr}${withStr}${nextClause}`;
118+
return `WITH${distinctStr} ${projectionStr}${whereStr}${setCypher}${orderByStr}${deleteStr}${withStr}${nextClause}`;
115119
}
116120

117121
private getWithClause(clauseOrColumn: With | "*" | WithProjection, columns: Array<"*" | WithProjection>): With {

tests/clause-chaining.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ describe("Clause chaining", () => {
188188
"delete",
189189
"detachDelete",
190190
"with",
191+
"set",
192+
"remove",
191193
"unwind",
192194
"match",
193195
"optionalMatch",

0 commit comments

Comments
 (0)