From fa520b81d8e9723fe34482616f44ef7624dcf978 Mon Sep 17 00:00:00 2001 From: angrykoala Date: Tue, 29 Oct 2024 15:34:36 +0000 Subject: [PATCH] Add support for patterns in cypher.size --- .changeset/silver-pugs-greet.md | 14 ++++++++++++++ src/expressions/functions/scalar.test.ts | 8 ++++++++ src/expressions/functions/scalar.ts | 14 ++++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 .changeset/silver-pugs-greet.md diff --git a/.changeset/silver-pugs-greet.md b/.changeset/silver-pugs-greet.md new file mode 100644 index 00000000..5d2b524e --- /dev/null +++ b/.changeset/silver-pugs-greet.md @@ -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)) +``` diff --git a/src/expressions/functions/scalar.test.ts b/src/expressions/functions/scalar.test.ts index 2f740dcc..e53192b8 100644 --- a/src/expressions/functions/scalar.test.ts +++ b/src/expressions/functions/scalar.test.ts @@ -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))"`); + }); }); diff --git a/src/expressions/functions/scalar.ts b/src/expressions/functions/scalar.ts index b9cc8a9f..f97e57f5 100644 --- a/src/expressions/functions/scalar.ts +++ b/src/expressions/functions/scalar.ts @@ -17,6 +17,7 @@ * limitations under the License. */ +import { Raw, type Pattern } from "../.."; import type { Expr } from "../../types"; import { CypherFunction } from "./CypherFunctions"; @@ -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]); } /**