Skip to content

Commit

Permalink
Merge pull request #289 from neo4j/unify-comprehensions
Browse files Browse the repository at this point in the history
Unify comprehensions
  • Loading branch information
angrykoala committed Feb 1, 2024
2 parents 80f707c + fc03970 commit 23b8c11
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 5 deletions.
17 changes: 17 additions & 0 deletions .changeset/sixty-moose-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@neo4j/cypher-builder": patch
---

Deprecates the second parameter of patternComprehensions in favor of new `.map` method:

_old_

```javascript
new Cypher.PatternComprehension(pattern, expr);
```

_new_

```javascript
new Cypher.PatternComprehension(pattern).map(expr);
```
10 changes: 5 additions & 5 deletions src/expressions/list/PatternComprehension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("Pattern comprehension", () => {
const node = new Cypher.Node({ labels: ["Movie"] });
const andExpr = Cypher.eq(node.property("released"), new Cypher.Param(1999));

const comprehension = new Cypher.PatternComprehension(node, andExpr);
const comprehension = new Cypher.PatternComprehension(node).map(andExpr);

const queryResult = new TestClause(comprehension).build();

Expand Down Expand Up @@ -61,7 +61,7 @@ describe("Pattern comprehension", () => {

const andExpr = Cypher.eq(rel.property("released"), new Cypher.Param(1999));

const comprehension = new Cypher.PatternComprehension(pattern, andExpr);
const comprehension = new Cypher.PatternComprehension(pattern).map(andExpr);

const queryResult = new TestClause(comprehension).build();

Expand All @@ -85,9 +85,9 @@ describe("Pattern comprehension", () => {

const pattern = new Cypher.Pattern(movie).related(rel).to(actor);

const comprehension = new Cypher.PatternComprehension(pattern, actor.property("name")).where(
Cypher.contains(movie.property("title"), new Cypher.Literal("Matrix"))
);
const comprehension = new Cypher.PatternComprehension(pattern)
.map(actor.property("name"))
.where(Cypher.contains(movie.property("title"), new Cypher.Literal("Matrix")));

const queryResult = new TestClause(comprehension).build();

Expand Down
6 changes: 6 additions & 0 deletions src/expressions/list/PatternComprehension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class PatternComprehension extends CypherASTNode {
private pattern: Pattern;
private mapExpr: Expr | undefined;

// NOTE: mapExpr parameter is deprecated in favour of `.map`
constructor(pattern: Pattern | NodeRef, mapExpr?: Expr) {
super();
if (pattern instanceof Pattern) {
Expand All @@ -48,6 +49,11 @@ export class PatternComprehension extends CypherASTNode {
this.mapExpr = mapExpr;
}

public map(mapExpr: Expr): this {
this.mapExpr = mapExpr;
return this;
}

/**
* @internal
*/
Expand Down
88 changes: 88 additions & 0 deletions tests/deprecated/pattern-comprehension-map.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Cypher from "../../src";
import { TestClause } from "../../src/utils/TestClause";

describe("Pattern comprehension", () => {
test("comprehension with map", () => {
const node = new Cypher.Node({ labels: ["Movie"] });
const andExpr = Cypher.eq(node.property("released"), new Cypher.Param(1999));

const comprehension = new Cypher.PatternComprehension(node, andExpr);

const queryResult = new TestClause(comprehension).build();

expect(queryResult.cypher).toMatchInlineSnapshot(`"[(this0:Movie) | this0.released = $param0]"`);

expect(queryResult.params).toMatchInlineSnapshot(`
{
"param0": 1999,
}
`);
});

test("comprehension from relationship pattern", () => {
const a = new Cypher.Node();
const b = new Cypher.Node();
const rel = new Cypher.Relationship({
type: "ACTED_IN",
});

const pattern = new Cypher.Pattern(a).related(rel).to(b);

const andExpr = Cypher.eq(rel.property("released"), new Cypher.Param(1999));

const comprehension = new Cypher.PatternComprehension(pattern, andExpr);

const queryResult = new TestClause(comprehension).build();

expect(queryResult.cypher).toMatchInlineSnapshot(
`"[(this1)-[this0:ACTED_IN]->(this2) | this0.released = $param0]"`
);

expect(queryResult.params).toMatchInlineSnapshot(`
{
"param0": 1999,
}
`);
});

test("comprehension with filter", () => {
const movie = new Cypher.Node({ labels: ["Movie"] });
const rel = new Cypher.Relationship({
type: "ACTED_IN",
});
const actor = new Cypher.Node({ labels: ["Actor"] });

const pattern = new Cypher.Pattern(movie).related(rel).to(actor);

const comprehension = new Cypher.PatternComprehension(pattern, actor.property("name")).where(
Cypher.contains(movie.property("title"), new Cypher.Literal("Matrix"))
);

const queryResult = new TestClause(comprehension).build();

expect(queryResult.cypher).toMatchInlineSnapshot(
`"[(this0:Movie)-[this2:ACTED_IN]->(this1:Actor) WHERE this0.title CONTAINS \\"Matrix\\" | this1.name]"`
);

expect(queryResult.params).toMatchInlineSnapshot(`{}`);
});
});

0 comments on commit 23b8c11

Please sign in to comment.