Skip to content

Commit

Permalink
Merge pull request #199 from neo4j/fix-rawCypher-types
Browse files Browse the repository at this point in the history
Fix RawCypher types
  • Loading branch information
angrykoala committed Oct 20, 2023
2 parents 3648e40 + 58dfee6 commit bcd5065
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-files-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@neo4j/cypher-builder": patch
---

Fix RawCypher types
31 changes: 30 additions & 1 deletion src/clauses/Raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,33 @@ export class Raw extends Clause {
* @group Other
* @deprecated use {@link Raw} instead
*/
export const RawCypher = Raw;
export class RawCypher extends Clause {
private callback: RawCypherCallback;

constructor(callback: RawCypherCallback | string) {
super();
if (typeof callback === "string") {
this.callback = this.stringToCallback(callback);
} else this.callback = callback;
}

public getCypher(env: CypherEnvironment): string {
const cbResult = this.callback(env);
if (!cbResult) return "";
let query: string;
let params = {};
if (typeof cbResult === "string") query = cbResult;
else {
[query, params] = cbResult;
}

const cypherParams = toCypherParams(params);
env.addExtraParams(cypherParams);

return query;
}

private stringToCallback(str: string): RawCypherCallback {
return () => str;
}
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/

import type { Raw } from ".";
import type { Raw, RawCypher } from ".";
import type { CypherEnvironment } from "./Environment";
import type { Case } from "./expressions/Case";
import type { HasLabel } from "./expressions/HasLabel";
Expand Down Expand Up @@ -63,6 +63,7 @@ export type Predicate =
| BooleanOp
| ComparisonOp
| Raw
| RawCypher
| Exists
| Count
| PredicateFunction
Expand Down

0 comments on commit bcd5065

Please sign in to comment.