# neogma

> Object-Graph-Mapping neo4j framework, Fully-typed with TypeScript, for easy and flexible node and relationship creation

Latest version **2.0.5** (published 2026-11-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install neogma
pnpm add neogma
yarn add neogma
bun add neogma
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2026-11-14 |
| First published | 2020-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22.13.0 |
| Dependencies | 4 |
| Unpacked size | 1021.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 160 |
| Author | Jason Athanasoglou |
| Maintainers | themetalfleece |
| Keywords | neo4j, ogm, graph, databases, cypher |

## Links

- npm: https://www.npmjs.com/package/neogma
- Repository: https://github.com/themetalfleece/neogma
- Homepage: https://github.com/themetalfleece/neogma#readme
- Issues: https://github.com/themetalfleece/neogma/issues
- npm.io page: https://npm.io/package/neogma

## Dependencies (4)

- [clone](https://npm.io/package/clone.md) ^2.1.2
- [typebox](https://npm.io/package/typebox.md) ^1.4.6
- [revalidator](https://npm.io/package/revalidator.md) ^0.3.1
- [neo4j-driver](https://npm.io/package/neo4j-driver.md) ^6.3.0

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 2.0.5 (latest) — 2026-11-14
- 2.0.4 — 2026-10-24
- 2.0.3 — 2026-10-10
- 2.0.2 — 2026-09-19
- 2.0.1 — 2026-07-18
- 2.0.0 — 2026-07-18
- 1.16.1 — 2026-02-08
- 1.16.0 — 2026-02-08
- 1.15.2 — 2025-12-01
- 1.15.1 — 2025-10-17
- 1.15.0 — 2025-10-12
- 1.14.1 — 2024-06-07
- 1.14.0 — 2024-04-04
- 1.13.1 — 2024-03-17
- 1.13.0 — 2024-01-15
- … 40 more at https://npm.io/package/neogma/versions

## README

![neogma logo](https://themetalfleece.github.io/neogma/assets/logo-text-horizontal.svg)

<h3 align="center">The Neo4j OGM that feels like writing plain TypeScript</h3>

<p align="center">
  Define models with decorators, run fully type-safe queries against them, eager load relationships in a single trip, and let TypeScript catch your mistakes before Neo4j does
</p>

<p align="center">
  <a href="https://www.npmjs.com/package/neogma"><img src="https://badgen.net/npm/v/neogma" alt="npm version"></a>
  <a href="https://www.npmjs.com/package/neogma"><img src="https://badgen.net/npm/dm/neogma" alt="npm downloads"></a>
  <a href="https://www.typescriptlang.org/"><img src="https://badgen.net/npm/types/tslib" alt="TypeScript"></a>
  <a href="https://github.com/themetalfleece/neogma/blob/main/LICENSE"><img src="https://badgen.net/github/license/themetalfleece/neogma" alt="License"></a>
  <a href="https://github.com/themetalfleece/neogma/actions/workflows/run-tests.yml"><img src="https://github.com/themetalfleece/neogma/actions/workflows/run-tests.yml/badge.svg?branch=main" alt="Tests"></a>
</p>

---

## What's new in v2

v2 replaces verbose factory configs with **decorators you can read at a glance**:

- **`@Node`, `@PrimaryKey`, `@Property`, `@Relationship`** -- define models as plain classes
- **TypeBox validation** built in -- powerful schemas, no extra dependencies
- **Zero boilerplate** -- no more separate `*Properties`, `*Instance`, or `*RelatedNodes` interfaces
- **Both decorator styles** -- TC39 standard (recommended) and legacy experimental
- **[`@neogma/nest`](./packages/nest)** -- first-class NestJS module with `forRoot`/`forRootAsync`, model DI via `forFeature`, and automatic connection lifecycle

v2 is **fully backwards compatible**. Existing v1 code keeps working, but we recommend migrating for the improved DX. See the [migration guide](https://neogma.themetalfleece.dev/docs/migration) for step-by-step instructions.

> Looking for v1? Source is on the [`release/v1` branch](https://github.com/themetalfleece/neogma/tree/release/v1), docs at [themetalfleece.github.io/neogma](https://themetalfleece.github.io/neogma/).

---

## Why Neogma?

Most Neo4j libraries make you choose between convenience and control. Neogma gives you both:

- 🔷 **Type-safe end to end** - define a model once, get compile-time checks on creates, queries, and relationships
- 🎯 **Decorator-based models** - plain classes with `@Node`, `@Property`, `@Relationship` -- no boilerplate interfaces
- ⚡ **Flexible by design** - use the OGM for CRUD, the query builder for complex traversals, or drop to raw Cypher when you need to
- 🔗 **Automatic relationships** - create deeply nested graph structures in a single call
- 🌿 **Eager loading** - fetch related nodes in one query, no N+1 surprises
- ✅ **Built-in [TypeBox](https://github.com/sinclairzx81/typebox) validation** - schema validation included, nothing extra to install
- 🛡️ **Injection-safe** - every user value is a query parameter, never interpolated into Cypher
- 🔄 **Session and transaction helpers** - automatic cleanup, rollback on error, minimal ceremony
- 🐈 **NestJS ready** - first-class integration via `@neogma/nest`
- 🚀 **Battle-tested** - comprehensive test suite, used in production

## Documentation

Full documentation, guides, and API reference at **[neogma.themetalfleece.dev](https://neogma.themetalfleece.dev)**

For AI/LLM agents, the docs are also available in machine-readable formats:

- [neogma.themetalfleece.dev/llms.txt](https://neogma.themetalfleece.dev/llms.txt) - page index
- [neogma.themetalfleece.dev/llms-full.txt](https://neogma.themetalfleece.dev/llms-full.txt) - full documentation as markdown

## Installation

```bash
npm install neogma
# or
pnpm add neogma
# or
yarn add neogma
```

## Quick Start

```typescript
import {
  Neogma,
  Node,
  PrimaryKey,
  Property,
  Relationship,
  NodeEntity,
  Type,
} from 'neogma';
import type { Related } from 'neogma';

// Connect to Neo4j
const neogma = new Neogma({
  url: 'bolt://localhost:7687',
  username: 'neo4j',
  password: 'password',
});

// Define models with decorators
@Node({ label: 'Order' })
class OrderNode extends NodeEntity {
  @PrimaryKey(Type.String()) id!: string;
  @Property(Type.String()) status!: string;
}

@Node({ label: 'User' })
class UserNode extends NodeEntity {
  @PrimaryKey(Type.String()) id!: string;

  @Property(Type.String({ minLength: 1 }))
  name!: string;

  @Property(Type.Optional(Type.Number({ minimum: 0 })))
  age?: number;

  @Relationship({ name: 'PLACED', direction: 'out', model: () => OrderNode })
  Orders!: Related<typeof OrderNode>;
}

// Register models (dependency order - referenced models first)
const Orders = neogma.model(OrderNode);
const Users = neogma.model(UserNode);

// Create - TypeScript validates properties at compile time
const user = await Users.createOne({
  id: '1',
  name: 'Alice',
  age: 30,
  Orders: {
    attributes: [{ id: 'order-1', status: 'confirmed' }],
  },
});

// Eager-load relationships in one query
const found = await Users.findOne({
  where: { name: 'Alice' },
  relationships: { Orders: { where: { target: { status: 'confirmed' } } } },
});
console.log(found?.name); // string
console.log(found?.Orders[0].node.status); // string
// console.log(found?.bogusValue);           // TypeScript error
```

---

## Decorator styles

Neogma supports both TC39 standard decorators (recommended) and legacy experimental decorators.

### TC39 decorators (default)

No tsconfig changes needed. Import from `neogma`:

```typescript
import { Node, PrimaryKey, Property, Relationship, NodeEntity } from 'neogma';
```

### Legacy experimental decorators

Enable in `tsconfig.json`:

```json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}
```

Import from `neogma/legacy`:

```typescript
import {
  Node,
  PrimaryKey,
  Property,
  Relationship,
  NodeEntity,
} from 'neogma/legacy';
```

---

## Query Builder

Build complex Cypher queries programmatically:

```typescript
import { QueryBuilder, BindParam } from 'neogma';

const qb = new QueryBuilder()
  .match({ identifier: 'u', model: Users })
  .where('u.age >= $minAge')
  .match({
    related: [
      { identifier: 'u' },
      { direction: 'out', name: 'PLACED' },
      { identifier: 'o', model: Orders },
    ],
  })
  .return('u.name AS name, count(o) AS orderCount')
  .orderBy('orderCount DESC')
  .limit(10);

const bp = new BindParam({ minAge: 18 });
const { records } = await qb.run(neogma.queryRunner, bp);
```

---

## Eager Loading

Load related nodes in a single query:

```typescript
const users = await Users.findMany({
  where: { status: 'active' },
  relationships: {
    Orders: {
      where: { target: { status: 'confirmed' } },
      order: [{ on: 'target', property: 'createdAt', direction: 'DESC' }],
      limit: 5,
      relationships: {
        Items: { limit: 10 },
      },
    },
  },
});

users[0].Orders[0].node.status; // Order property
users[0].Orders[0].relationship; // Relationship properties
```

---

## NestJS Integration

[`@neogma/nest`](./packages/nest) provides a dedicated NestJS module with dependency injection, connection lifecycle management, and model registration.

```bash
npm install @neogma/nest neogma
# or
pnpm add @neogma/nest neogma
```

**Features:**
- `NeogmaModule.forRoot()` / `forRootAsync()` -- configure connection with static options or from `ConfigService`
- `NeogmaModule.forFeature([...models])` -- register decorated model classes as injectable providers
- `NeogmaService` -- injectable service exposing the `Neogma` instance for raw queries
- `getModelToken()` / `ModelOf<T>` -- type-safe DI token helpers
- Automatic connection lifecycle (connect on init, close on shutdown)

```typescript
import { Module } from '@nestjs/common';
import { NeogmaModule } from '@neogma/nest';

@Module({
  imports: [
    NeogmaModule.forRoot({
      connection: {
        url: 'bolt://localhost:7687',
        username: 'neo4j',
        password: 'password',
      },
    }),
    NeogmaModule.forFeature([UserNode, OrderNode]),
  ],
})
export class AppModule {}
```

```typescript
import { Injectable, Inject } from '@nestjs/common';
import { getModelToken } from '@neogma/nest';
import type { ModelOf } from '@neogma/nest';

type UserModel = ModelOf<typeof UserNode>;

@Injectable()
export class UserService {
  constructor(@Inject(getModelToken(UserNode)) private Users: UserModel) {}

  async findAll() {
    return this.Users.findMany({});
  }
}
```

See the [NestJS integration docs](https://neogma.themetalfleece.dev/docs/integrations/nestjs) and the [`nestjs-app` example](./examples/nestjs-app) for full details.

---

## Example Applications

Runnable examples are in the [`/examples`](./examples) directory:

| Example                                                                 | Description                                  |
| ----------------------------------------------------------------------- | -------------------------------------------- |
| [`basic-app-decorators`](./examples/basic-app-decorators)               | TC39 decorator-based models (recommended)    |
| [`basic-app-legacy-decorators`](./examples/basic-app-legacy-decorators) | Experimental (legacy) decorator-based models |
| [`basic-app-js`](./examples/basic-app-js)                               | JavaScript with ModelFactory                 |
| [`nestjs-app`](./examples/nestjs-app)                                   | NestJS integration with `@neogma/nest`       |

---

## Contributing

See [DEVELOPMENT.md](DEVELOPMENT.md) for setup instructions and development workflow.

## Acknowledgements

- Neogma logo created by [Greg Magkos](https://github.com/grigmag)
- Development was made possible thanks to the open source libraries which can be found in package.json

---
_Source: https://npm.io/package/neogma · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
