@neo4j/cypher-builder
Version:
A programmatic API for building Cypher queries for Neo4j
150 lines (149 loc) • 3.94 kB
JavaScript
;
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.IsType = exports.ListType = exports.CypherTypes = void 0;
exports.isType = isType;
exports.isNotType = isNotType;
const CypherASTNode_1 = require("../CypherASTNode");
const as_array_1 = require("../utils/as-array");
const BaseTypes = {
ANY: "ANY",
BOOLEAN: "BOOLEAN",
DATE: "DATE",
DURATION: "DURATION",
FLOAT: "FLOAT",
INTEGER: "INTEGER",
LOCAL_DATETIME: "LOCAL DATETIME",
LOCAL_TIME: "LOCAL_TIME",
MAP: "MAP",
NODE: "NODE",
NOTHING: "NOTHING",
NULL: "NULL",
PATH: "PATH",
POINT: "POINT",
PROPERTY_VALUE: "PROPERTY VALUE",
RELATIONSHIP: "RELATIONSHIP",
STRING: "STRING",
ZONED_DATETIME: "ZONED DATETIME",
ZONED_TIME: "ZONED TIME",
TIMESTAMP_WITHOUT_TIME_ZONE: "TIMESTAMP WITHOUT TIME ZONE",
TIME_WITHOUT_TIME_ZONE: "TIME WITHOUT TIME ZONE",
TIMESTAMP_WITH_TIME_ZONE: "TIMESTAMP WITH TIME ZONE",
TIME_WITH_TIME_ZONE: "TIME WITH TIME ZONE",
};
/**
* Generates a cypher `LIST<...>` type
* @example
* ```cypher
* LIST<STRING>
* ```
*/
function list(type) {
return new ListType((0, as_array_1.asArray)(type));
}
/**
* Types supported by Neo4j
* @see {@link https://neo4j.com/docs/cypher-manual/current/values-and-types/property-structural-constructed/#types-synonyms | Cypher Documentation}
*/
exports.CypherTypes = {
...BaseTypes,
list,
};
/**
* Type predicate expression
* @group Expressions
* @see {@link https://neo4j.com/docs/cypher-manual/current/values-and-types/type-predicate/ | Cypher Documentation}
* @example
* ```cypher
* val IS :: INTEGER
* ```
*/
function isType(expr, type) {
return new IsType(expr, (0, as_array_1.asArray)(type));
}
/**
* Type predicate expression with NOT
* @group Expressions
* @see {@link https://neo4j.com/docs/cypher-manual/current/values-and-types/type-predicate/#type-predicate-not | Cypher Documentation}
* @example
* ```cypher
* val IS NOT :: INTEGER
* ```
*/
function isNotType(expr, type) {
return new IsType(expr, (0, as_array_1.asArray)(type), true);
}
/**
* @group Expressions
*/
class ListType {
types;
_notNull = false;
/** @internal */
constructor(type) {
this.types = type;
}
notNull() {
this._notNull = true;
return this;
}
/** @internal */
getCypher(env) {
// Note that all types must be nullable or non nullable
const notNullStr = this._notNull ? " NOT NULL" : "";
const typesStr = this.types
.map((type) => {
const typeStr = compileType(type, env);
return `${typeStr}${notNullStr}`;
})
.join(" | ");
return `LIST<${typesStr}>`;
}
}
exports.ListType = ListType;
/**
* @group Expressions
*/
class IsType extends CypherASTNode_1.CypherASTNode {
expr;
types;
not;
_notNull = false;
/** @internal */
constructor(expr, type, not = false) {
super();
this.expr = expr;
this.types = type;
this.not = not;
}
notNull() {
this._notNull = true;
return this;
}
/** @internal */
getCypher(env) {
const exprCypher = this.expr.getCypher(env);
const isStr = this.not ? "IS NOT" : "IS";
// Note that all types must be nullable or non nullable
const notNullStr = this._notNull ? " NOT NULL" : "";
const typesStr = this.types
.map((type) => {
const typeStr = compileType(type, env);
return `${typeStr}${notNullStr}`;
})
.join(" | ");
return `${exprCypher} ${isStr} :: ${typesStr}`;
}
}
exports.IsType = IsType;
function compileType(type, env) {
if (type instanceof ListType) {
return type.getCypher(env);
}
else {
return type;
}
}