Skip to Content
Getting started

Getting started

Cyphra is a TypeScript ORM for Neo4j. You describe your graph in a .cyphra schema, and Cyphra gives you:

  • a generated, fully typed client: db.user.findMany({ include: { posts: true } }), nested writes, $transaction, typed edge properties (Typed client)
  • migrations generated from schema changes: constraints, indexes, backfills and guards (Migrations)
  • a CLI to start a local Neo4j (cyphra dev), introspect an existing database (cyphra pull) and more (CLI)
  • Cypher that stays visible: a parameterized tagged template and a small builder (Queries)

Cyphra needs Neo4j 5.6+ for the typed client.

Install

The cyphra package is everything your app imports. The CLI is a separate dev dependency, and tsx runs the TypeScript examples below.

npm i cyphra npm i -D @cyphra/cli tsx

You can also install @cyphra/schema, @cyphra/query, @cyphra/runtime and @cyphra/migrator individually if you want explicit package boundaries.

Quick start

Describe your graph

npx cyphra init scaffolds cyphra.json, schema.cyphra and migrations/. Then edit the schema:

schema.cyphra
node User { id String @id @default(cuid()) email String @unique posts Post[] @relationship(type: "AUTHORED", direction: OUT) } node Post { id String @id @default(uuid()) title String author User? @relationship(type: "AUTHORED", direction: IN) }

Start a database and migrate

npx cyphra dev # local Neo4j 5 in Docker, writes .env npx cyphra migrate dev --name init # constraints and indexes, as a reviewable migration

Already have a database? npx cyphra pull writes schema.cyphra from it.

Generate the client

npx cyphra generate # writes cyphra.gen.ts

Run it again after each schema change, or keep npx cyphra generate --watch running to regenerate on save (see Watch mode).

Query

main.ts
import { createClient } from "./cyphra.gen.js"; const db = createClient({ uri: process.env.NEO4J_URI!, user: process.env.NEO4J_USERNAME!, password: process.env.NEO4J_PASSWORD!, }); const alice = await db.user.create({ data: { email: "alice@acme.io", posts: { create: [{ title: "Hello graph" }] } }, include: { posts: true }, }); // alice.posts: { id: string; title: string }[] await db.$disconnect();

Run it with the database settings from .env:

node --env-file=.env --import tsx main.ts

Cyphra is ESM-only, and the example uses top-level await. In a project created with npm init, set "type": "module" in package.json first (npm pkg set type=module).

Prefer raw Cypher? It is one call away, still parameterized:

const rows = await db.$cypher`MATCH (u:User {email: ${email}}) RETURN u.id AS id`;

Next steps

Last updated on