Skip to content

Commit

Permalink
Yield support for procedures (#3025)
Browse files Browse the repository at this point in the history
* Add yield to call statements

* db procedures

* Improve procedures by making those callable in cypher builder

* Fix procedures yields

* changeset on cypher builder procedures

* Fix changes in procedures from cypher builder

* Fix eslint issues on cypher builder

* Fix typings

* Update convertFormat as a cypher function
  • Loading branch information
angrykoala authored Mar 15, 2023
1 parent 8a69b30 commit 507f9f7
Show file tree
Hide file tree
Showing 34 changed files with 428 additions and 445 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-swans-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/cypher-builder": minor
---

CallProcedure clause deprecated and improvements on Procedures API
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@
* limitations under the License.
*/

export { convertFormat } from "./convert-format";
import Cypher from "../src";

// CALL db.labels() yield label as this0
// RETURN this0

const label = new Cypher.NamedVariable("label");

const labelVar = new Cypher.Variable();
const labelsCall = Cypher.db.labels().yield(["label", labelVar]).return(label);

const { cypher, params } = labelsCall.build();

console.log("Cypher");
console.log(cypher);
console.log("----");
console.log("Params", params);
13 changes: 7 additions & 6 deletions packages/cypher-builder/src/Cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export { Match, OptionalMatch } from "./clauses/Match";
export { Create } from "./clauses/Create";
export { Merge } from "./clauses/Merge";
export { Call } from "./clauses/Call";
export { CallProcedure } from "./clauses/CallProcedure";
export { Use } from "./clauses/Use";
export { Return } from "./clauses/Return";
export { RawCypher } from "./clauses/RawCypher";
Expand All @@ -48,9 +47,6 @@ export { Path, NamedPath } from "./references/Path";
export { Exists } from "./expressions/Exists";
export { Case } from "./expressions/Case";

// --Procedures
export * as db from "./procedures/db";

// --Apoc
export * as apoc from "./apoc/apoc";

Expand Down Expand Up @@ -112,14 +108,20 @@ export * from "./expressions/functions/PathFunctions";

export { any, all, exists, single } from "./expressions/functions/PredicateFunctions";

// Procedures
export { CypherProcedure as Procedure, VoidCypherProcedure as VoidProcedure } from "./procedures/CypherProcedure";

export * as db from "./procedures/db";

// Types
export type { CypherResult } from "./types";
export type { PropertyRef } from "./references/PropertyRef";
export type { Clause } from "./clauses/Clause";
export type { CypherEnvironment as Environment } from "./Environment";
export type { ComparisonOp } from "./expressions/operations/comparison";
export type { BooleanOp } from "./expressions/operations/boolean";
export type { Expr, Predicate, Operation, Procedure } from "./types";
export type { Expr, Predicate, Operation } from "./types";
export type { Yield } from "./procedures/Yield";
export type { ProjectionColumn } from "./clauses/sub-clauses/Projection";
export type { SetParam } from "./clauses/sub-clauses/Set";
export type { PredicateFunction } from "./expressions/functions/PredicateFunctions";
Expand All @@ -128,5 +130,4 @@ export type { CompositeClause } from "./clauses/utils/concat";
export type { CypherAggregationFunction as AggregationFunction } from "./expressions/functions/AggregationFunctions";

// utils
// --Procedures
export * as utils from "./utils/utils";
6 changes: 3 additions & 3 deletions packages/cypher-builder/src/apoc/apoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
*/

export { RunFirstColumn } from "./functions/RunFirstColumn";
export { ValidatePredicate } from "./functions/ValidatePredicate";
export { Validate } from "./procedures/Validate";
export * as date from "./functions/date";

export * as date from "./date";
export * as util from "./util";
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,34 @@
* limitations under the License.
*/

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

describe("apoc.date", () => {
test("convertFormat", () => {
const converFormat = Cypher.apoc.date.convertFormat(
const convertFormat = Cypher.apoc.date.convertFormat(
new Cypher.Variable(),
"iso_zoned_date_time",
"iso_offset_date_time"
);

const queryResult = new TestClause(converFormat).build();
const queryResult = new TestClause(convertFormat).build();

expect(queryResult.cypher).toMatchInlineSnapshot(
`"apoc.date.convertFormat(toString(var0), \\"iso_zoned_date_time\\", \\"iso_offset_date_time\\")"`
);

expect(queryResult.params).toMatchInlineSnapshot(`Object {}`);
});
test("convertFormat with default convertTo", () => {
const convertFormat = Cypher.apoc.date.convertFormat(new Cypher.Variable(), "iso_zoned_date_time");

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

expect(queryResult.cypher).toMatchInlineSnapshot(
`"apoc.date.convertFormat(toString(var0), \\"iso_zoned_date_time\\", \\"yyyy-MM-dd\\")"`
);

expect(queryResult.params).toMatchInlineSnapshot(`Object {}`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,23 @@
* limitations under the License.
*/

import type { CypherEnvironment } from "../Environment";
import { Clause } from "./Clause";
import type { Procedure } from "../types";
import Cypher from "..";
import { CypherFunction } from "../expressions/functions/CypherFunctions";
import type { PropertyRef } from "../references/PropertyRef";
import type { Variable } from "../references/Variable";

// TODO: ADD yield, where and return
/**
* @see [Cypher Documentation](https://neo4j.com/docs/cypher-manual/current/clauses/call/)
* @group Clauses
* @group Expressions
* @category Cypher Functions
*/
export class CallProcedure extends Clause {
private procedure: Procedure;

constructor(procedure: Procedure) {
super();
this.procedure = procedure;
}

/** @internal */
public getCypher(env: CypherEnvironment): string {
const procedureCypher = this.procedure.getCypher(env);
return `CALL ${procedureCypher}`;
}
export function convertFormat(
temporalParam: Variable | PropertyRef,
currentFormat: string,
convertTo = "yyyy-MM-dd"
): CypherFunction {
return new CypherFunction("apoc.date.convertFormat", [
Cypher.toString(temporalParam),
new Cypher.Literal(currentFormat),
new Cypher.Literal(convertTo),
]);
}
2 changes: 2 additions & 0 deletions packages/cypher-builder/src/apoc/functions/RunFirstColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import type { Variable } from "../../references/Variable";
import type { CypherEnvironment } from "../../Environment";
import type { MapExpr } from "../../expressions/map/MapExpr";

// TODO: convert into proper function

/**
* @group Expressions
* @category Cypher Functions
Expand Down

This file was deleted.

44 changes: 0 additions & 44 deletions packages/cypher-builder/src/apoc/functions/ValidatePredicate.ts

This file was deleted.

53 changes: 0 additions & 53 deletions packages/cypher-builder/src/apoc/functions/convert-format.ts

This file was deleted.

47 changes: 0 additions & 47 deletions packages/cypher-builder/src/apoc/procedures/Validate.test.ts

This file was deleted.

Loading

0 comments on commit 507f9f7

Please sign in to comment.