Skip to content

Basic entities

The following are the steps to define a basic entity:

DecoratorPurpose
@Entity()Marks a class as a database table/collection.
@Id()Defines the Primary Key with support for onInsert generators (UUIDs, etc).
@Field()Standard column. Use { references: ... } for Foreign Keys.
@Index()Defines a composite or customized index on one or more columns.
@OneToOneDefines a one-to-one relationship.
@OneToManyDefines a one-to-many relationship.
@ManyToOneDefines a many-to-one relationship.
@ManyToManyDefines a many-to-many relationship.
import { v7 as uuidv7 } from 'uuid';
import { Entity, Id, Field, Index } from '@uql/core';
@Entity()
export class User {
@Id({
type: 'uuid',
onInsert: () => uuidv7()
})
id?: string;
@Field({ index: true })
name?: string;
@Field({
unique: true,
comment: 'User login email'
})
email?: string;
@Field({ type: 'text' })
bio?: string;
}

UQL provides two levels for specifying column types. Always prefer type for portability; use columnType only for precise SQL control.

PropertyLevelDescription
typeLogical (Recommended)Database-agnostic. UQL maps it to the correct SQL type for each dialect.
columnTypePhysicalDirect SQL type. Use only when you need exact control over the underlying type.
// ✅ RECOMMENDED: Use `type` for semantic, cross-database types
@Field({ type: 'uuid' })
externalId?: string;
@Field({ type: 'jsonb' })
metadata?: object;
@Field({ type: 'text' })
bio?: string;
// ⚠️ USE SPARINGLY: `columnType` for precise SQL control
@Field({
columnType: 'decimal',
precision: 10,
scale: 2
})
price?: number;
@Field({
columnType: 'varchar',
length: 500
})
longBio?: string;

The @Field and @Id decorators accept several options for both query validation and schema generation:

OptionTypeDescription
namestringCustom database column name.
typeType | stringLogical type: String, Number, Boolean, Date, BigInt, or strings like 'uuid', 'text', 'json', 'jsonb', 'timestamp', 'timestamptz', 'vector'.
columnTypeColumnTypeExplicit SQL column type (e.g., varchar, text, jsonb, vector). Takes highest priority.
lengthnumberColumn length. If unspecified, defaults to TEXT (Postgres/SQLite) or VARCHAR(255) (MySQL/Maria).
nullablebooleanWhether the column allows NULL values. Defaults to true.
uniquebooleanAdds a UNIQUE constraint.
indexboolean | stringAdds an index. Pass a string to name it.
defaultValueScalarDefault value at the database level.
commentstringAdds a comment to the column in the database.
onInsertfunctionGenerator function for new records (e.g., for UUIDs).
reference() => EntityMarks this field as a foreign key referencing another entity’s primary key.
foreignKeystring | falseCustom foreign key constraint name, or false to disable physical constraints while maintaining logical references.

The @Id decorator also supports:

OptionTypeDescription
autoIncrementbooleanExplicitly enable/disable auto-increment. Defaults to true for numeric types, false for strings/UUIDs.
// Auto-increment integer (simple, database-managed)
@Id()
id?: number;
// UUID (portable, client-generated)
@Id({
type: 'uuid',
onInsert: () => uuidv7()
})
id?: string;

Continue reading about Relationships or Indexes.