Skip to Content
Query builder

Queries

When a query is easier to say in Cypher, write Cypher: a tagged template for raw queries and a small builder for composed ones. Both are exported from cyphra, or directly from @cyphra/query.

Tagged Cypher

Values are always passed as parameters, never string-concatenated into Cypher:

import { cypher } from "@cyphra/query"; const { text, params } = cypher` MATCH (u:User) WHERE u.id = ${userId} RETURN u `;

Builder

import { node, rel, prop, eq, select } from "@cyphra/query"; const u = node("User", "u"); const o = node("Organization", "o"); const m = rel("MEMBER_OF", "m"); select() .match(u.out(m, o)) .where(eq(prop(m.alias, "role"), "admin")) .returnFields({ id: prop(u.alias, "id") }) .orderBy({ prop: prop(u.alias, "createdAt"), direction: "DESC" }) .skip(0) .limit(20) .toCypher();

Use u.in(m, from) or u.both(m, other) for incoming or undirected patterns. After match(...), call optionalMatch(pattern) one or more times to append OPTIONAL MATCH clauses in order (then where / return… apply to the full read shape).

where() accepts multiple predicates (AND-combined): eq, neq, gt, gte, lt, lte, between (inclusive (prop >= $lo AND prop <= $hi)), isNull, isNotNull, inList, startsWith, endsWith, contains, matches (=~, Java-style regex; pattern is always a parameter), not(inner), and grouping via and(…) / or(…) (parentheses follow Cypher precedence: AND binds tighter than OR). Example: .where(or(eq(prop(u.alias, "role"), "a"), eq(prop(u.alias, "role"), "b")), eq(prop(u.alias, "active"), true)) → (u.role = $p0 OR u.role = $p1) AND u.active = $p2.

Every value is a Neo4j parameter—only IS NULL / IS NOT NULL have no bound value.

Call returnFields({ … }) or returnStar() before toCypher() (a bare MATCH without RETURN is rejected). Chain returnDistinct() after either to emit RETURN DISTINCT … (deduplicate rows before ORDER BY / SKIP / LIMIT).

Traversals and paths

For multi-hop queries on your schema (friends of friends, everything within three hops), the typed client has traverse: it compiles to one parameterized Cypher statement with a quantified path pattern and can return each path as { nodes, edges }, with edge properties typed from your relationship { … } models.

const rows = await db.user.traverse({ from: { email: "alice@acme.io" }, via: "following", depth: { max: 2 }, returnPaths: true, }); rows[0]?.path.edges[0]?.properties.since; // Date

Use the tagged template or the builder when the traversal crosses several models or needs a filter on the intermediate nodes.

Runtime shortcuts

CyphraClient provides queryRecords / queryRecord (from any CompiledCypher) and selectRecords / selectRecord (from a SelectQuery) so you rarely need to wire run + toPlainRecords by hand. See the Runtime page for sessions, transactions, and options.

Codegen

Run cyphra generate to produce cyphra.gen.ts with NodeLabel, RelType, and RelationshipSpec constants aligned to your .cyphra file, then import them alongside the builder for fewer string typos.

Result mapping

@cyphra/runtime exports toPlainRecords and toPlainRecord to turn a session.run / runCompiled result into plain objects (record.toObject()), without an OGM layer.

Last updated on