Skip to content
🚀 This documentation is for unreal-orm 1.0.0 alpha which requires SurrealDB 2.0 SDK. For use with version 1.x, see here.

Schema Application

After you define tables, fields, indexes, and buckets, you turn them into SurrealQL and apply them to a database.

import { applySchema } from 'unreal-orm';
import { User, UserEmailIndex, Post, Follow } from './tables';
await applySchema(db, [User, Post, Follow, UserEmailIndex]);

applySchema walks the definitions and emits DEFINE TABLE, DEFINE FIELD, DEFINE INDEX, and DEFINE BUCKET statements in dependency order. The db argument is any SurrealDB-like object, such as a Surreal client or a transaction.

Both applySchema and the generate* functions accept an optional method argument:

  • 'error' (default) — throw if the table already exists.
  • 'IF NOT EXISTS' — emit DEFINE ... IF NOT EXISTS.
  • 'OVERWRITE' — emit DEFINE ... OVERWRITE.
await applySchema(db, [User], 'IF NOT EXISTS');
import { generateFullSchemaQl, generateTableSchemaQl } from 'unreal-orm';
// All definables
const fullDdl = generateFullSchemaQl([User, UserEmailIndex]);
// One table
const tableDdl = generateTableSchemaQl(User);

This is useful for inspecting the generated SurrealQL, writing manual migrations, or running schema changes through a CI gate.

Unreal bundles the most common schema utilities into one import.

import { Unreal } from 'unreal-orm';
await Unreal.applySchema(db, [User, Post]);
const ddl = Unreal.generateSchema([User, Post]);

For programmatic schema work the Unreal.ast object exposes parsing, extraction, comparison, and migration helpers.

import { Unreal } from 'unreal-orm';
const ast = Unreal.ast.extractSchema([User, Post]);
const changes = Unreal.ast.compareSchemas(localAst, remoteAst);

See Migrations for the full AST workflow.