Decorators
Overview
Module
- package:
@aerokit/sdk/db - source: db/decorators.ts
- last updated:
This module provides a set of decorators for defining database entities and their properties in a Dirigible application. The decorators are designed to be compatible with both legacy JavaScript environments (like Mozilla Rhino or older GraalJS) and modern JavaScript environments that support the latest decorator specifications.
Key Features
- Entity Decorator: Marks a class as a database entity, allowing it to be registered and managed by the ORM layer.
- Table Decorator: Specifies the database table name for the entity, with a default naming convention based on the class name.
- Column Decorator: Marks a class property as a database column, with options for column type, length, nullability, and default values.
- ID Decorator: Marks a property as the primary key of the entity.
- Generated Decorator: Indicates that a property is generated by the database (e.g., auto-increment).
- Relationship Decorators: Defines one-to-many and many-to-one relationships between entities.
- Documentation Decorator: Adds documentation metadata to classes and properties for better maintainability and clarity.
Use Cases
- Defining database entities in a structured and consistent manner across different tables and relationships.
- Abstracting database interactions to allow for easier maintenance and potential database engine changes in the future.
- Handling complex entity relationships and ensuring data integrity through validation and association management.
Example Usage
ts
import { Entity, Table, Column, Id, Generated, OneToMany, ManyToOne, Documentation } from "@aerokit/sdk/db/decorators";
@Entity('User')
@Table('USERS')
@Documentation('Represents a user in the system')
class User {
@Id()
@Generated('IDENTITY')
@Column({ type: 'integer' })
id!: number;
@Column({ type: 'string', length: 100, nullable: false })
name!: string;
@Column({ type: 'string', length: 255, nullable: false })
email!: string;
@OneToMany(() => Post, { joinColumn: 'user_id', cascade: 'all' })
posts!: Post[];
}
@Entity('Post')
@Table('POSTS')
class Post {
@Id()
@Generated('IDENTITY')
@Column({ type: 'integer' })
id!: number;
}
::: info ECMAScript 2025-compliant ORM decorator implementation
Compatible with GraalJS runtime.
- Uses context.addInitializer for stable decorator timing
- Stores metadata in a global WeakMap cache
- Finalizes entity metadata once per class
- Defers finalization via microtask (Promise.resolve().then)
:::
## Classes