Skip to content

Introduction

Unreal ORM Logo

GitHub Stars npm version License: ISC npm downloads npm bundle size TypeScript Buy Me a Coffee



UnrealORM is a modern, type-safe ORM for SurrealDB that gives you native SurrealDB power, full TypeScript safety, and zero abstraction—no decorators, no magic, just classes and functions. Designed for developers who want direct control, advanced schema features, and a frictionless experience.

Why UnrealORM? I created this package because I wanted something like it for my own projects. Building UnrealORM gave me the perfect excuse to spend more time crafting an even better, reusable solution for the community.

Note: UnrealORM is not at version 1.0 yet. While the API is stabilizing and major breaking changes are unlikely, please be aware that some changes may still occur before the 1.0 release.

UnrealORM aims to provide a powerful and intuitive way to interact with SurrealDB in TypeScript projects. Our goal is to leverage TypeScript’s type safety and developer ergonomics without abstracting away the underlying power and flexibility of SurrealDB. We believe in a “native first” approach, ensuring that you can always access SurrealDB’s full feature set.

  • Native First: Exposes SurrealDB’s native features and SurrealQL directly. No heavy abstractions.
  • Type Safety: Comprehensive TypeScript types for SurrealDB features, tables, fields, and query results.
  • Modern JavaScript: Designed with ESNext features and module systems in mind.
  • Schema Definition: Intuitive API for defining tables, fields, indexes, and relationships that map directly to SurrealDB’s schema capabilities.
  • Query Building (Lightweight): Supports direct SurrealQL with type-safe parameters.
  • Developer Experience: Aims to make working with SurrealDB in TypeScript a pleasant and productive experience.
  • SurrealDB Feature Support: For a detailed breakdown of supported SurrealDB 2.x schema features, please see our CAPABILITIES.md document.

A direct comparison between unreal-orm and the official surrealdb Node.js driver:

unreal-ormsurrealdb package
Type Safety✅ Strong TypeScript types for models/queries⚠️ Partial (manual, not enforced)
Schema Sync✅ Models and schema stay in sync❌ No schema management
Model Logic✅ Add type-safe instance/static methods to models❌ No model abstraction
Migrations✅ Easy schema DDL/apply from code❌ Manual DDL, risk of drift
Relations✅ Fully typed, hydrated relations⚠️ Supported, manual type-safety
Query Ergonomics✅ Type-safe, object-oriented, and SurrealQL support⚠️ Raw SurrealQL or data objects—not type-safe
Raw SurrealQL Support✅ Use raw SurrealQL when needed✅ Full SurrealQL access
Error Handling✅ Native SurrealDB errors, no ORM wrappers✅ Native SurrealDB errors

UnrealORM builds on top of the official surrealdb package, providing a modern TypeScript ORM experience while preserving full access to SurrealDB’s native features.

Install unreal-orm using your favorite package manager:

Terminal window
# Using pnpm
pnpm add unreal-orm surrealdb typescript
# Using npm
npm install unreal-orm surrealdb typescript
# Using yarn
yarn add unreal-orm surrealdb typescript
# Using bun
bun add unreal-orm surrealdb typescript

Note: surrealdb and typescript are peer dependencies.

import { Table, Field } from 'unreal-orm';
class User extends Table.normal({
name: 'user',
fields: {
name: Field.string({ assert: '$value.length > 2' }),
email: Field.string({ value: 'string::lowercase($value)' }),
age: Field.option(Field.number()),
}
}) {
isAdult() {
return (this.age ?? 0) >= 18;
}
}
import { Surreal } from 'surrealdb';
async function main() {
const db = new Surreal();
await db.connect({ ... });
await db.use({ ns: 'test', db: 'test' });
// Create a user
const user = await User.create(db, { name: 'Jane', email: 'Jane@Email.com', age: 30 });
// Query users
const users = await User.select(db);
console.log(users[0].isAdult()); // true
await db.close();
}
main();

See below for field patterns, relationships, schema generation, and advanced usage.

const User = Table.normal({
name: 'user',
fields: {
name: Field.string({ assert: '$value.length > 2', comment: 'User name' }),
tags: Field.array(Field.string(), { max: 10 }),
posts: Field.array(Field.record((): any => Post)),
createdAt: Field.datetime({ value: 'time::now()' }),
customField: Field.custom<number>('duration'),
}
});
// Generate and apply schema
import { applySchema, generateTableSchemaQl } from 'unreal-orm';
// Generate
const ddl = generateTableSchemaQl(User);
// Or automatically generate & apply
await applySchema(db, [User, Post]);

See Capabilities for all supported field options, validation, and SurrealDB mappings.

For more detailed information, API reference, and advanced usage, visit the UnrealORM documentation.

We welcome contributions of all kinds!
Please read our Contributing Guide for how to get started, coding standards, and guidelines.

If you have questions, ideas, or want to discuss improvements, join our GitHub Discussions.

unreal-orm is created and maintained by Jimpex.

If you find this project useful, please consider:

  • ⭐ Starring the repository on GitHub
  • ☕ Supporting the development via Ko-fi

Your support helps keep this project active and improving!

This project is licensed under the ISC License.