Skip to content

Commit

Permalink
Merge pull request #437 from neo4j/436-cyphersize-does-not-support-pa…
Browse files Browse the repository at this point in the history
…ttern

Add support for patterns in cypher.size
  • Loading branch information
angrykoala authored Oct 29, 2024
2 parents ae77d59 + fa520b8 commit ea8c44f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .changeset/silver-pugs-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@neo4j/cypher-builder": patch
---

Add support for patterns in `size()` for Neo4j 4

```js
const pattern = new Cypher.Pattern(new Cypher.Node()).related().to();
const cypherFunction = Cypher.size(pattern);
```

```cypher
size((this0)-[this1]->(this2))
```
8 changes: 8 additions & 0 deletions src/expressions/functions/scalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,12 @@ describe("Scalar Functions", () => {

expect(queryResult.cypher).toMatchInlineSnapshot(`"size(\\"Hello\\")"`);
});

test("size() applied to a pattern", () => {
const pattern = new Cypher.Pattern(new Cypher.Node()).related().to();
const cypherFunction = Cypher.size(pattern);
const queryResult = new TestClause(cypherFunction).build();

expect(queryResult.cypher).toMatchInlineSnapshot(`"size((this0)-[this1]->(this2))"`);
});
});
14 changes: 12 additions & 2 deletions src/expressions/functions/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* limitations under the License.
*/

import { Raw, type Pattern } from "../..";
import type { Expr } from "../../types";
import { CypherFunction } from "./CypherFunctions";

Expand Down Expand Up @@ -107,8 +108,17 @@ export function randomUUID(): CypherFunction {
* @group Cypher Functions
* @category Scalar
*/
export function size(expr: Expr): CypherFunction {
return new CypherFunction("size", [expr]);
export function size(expr: Expr): CypherFunction;
/** @deprecated size() with pattern is deprecated in Neo4j 5 */
export function size(expr: Pattern): CypherFunction;
export function size(expr: Expr | Pattern): CypherFunction {
// Support for patterns in size() in Neo4j 4
// Using Raw to avoid adding Patterns to CypherFunction
const sizeParam = new Raw((env) => {
return env.compile(expr);
});

return new CypherFunction("size", [sizeParam]);
}

/**
Expand Down

0 comments on commit ea8c44f

Please sign in to comment.