Data & Database Decorators
Define TypeScript database operations, transactions, isolation levels, consistency models, read-only constraints, ORM entity mappings, relationships, and multi-tenant data isolation with AxilJS.
Data & Database Decorators
AxilJS data decorators describe how application code interacts with persistent storage. They express database operations, transaction boundaries, consistency requirements, entity mappings, and tenant isolation as semantic metadata.
The decorators describe intent; database and ORM consumers interpret that metadata to implement the required behavior without coupling application code to a specific database driver.
@tDatabase
Declares the database operation semantics for a method.
Options
| Option | Type | Description |
|---|---|---|
operation | 'read' | 'write' | 'readwrite' | Operation type |
replica | 'primary' | 'replica' | 'auto' | Connection pool routing |
timeout | number | Query timeout in milliseconds |
@tDatabase makes the intended database access pattern explicit. A runtime or ORM consumer can use this metadata to select an appropriate connection pool and enforce operation constraints.
@tTransaction
Defines a database transaction boundary around a method.
Transaction configuration can include isolation, timeout, and propagation behavior:
Options
| Option | Type | Description |
|---|---|---|
isolation | 'read-uncommitted' | 'read-committed' | 'repeatable-read' | 'serializable' | Transaction isolation level |
timeout | number | Transaction timeout in milliseconds |
propagation | 'required' | 'requires-new' | 'nested' | 'never' | Transaction propagation behavior |
The transaction decorator expresses the boundary and requirements. The underlying transaction manager is responsible for implementing the actual database transaction.
@tIsolation
Declares a minimum transaction isolation requirement.
Use @tIsolation when the isolation requirement needs to be expressed independently from the transaction boundary.
This allows infrastructure consumers to reason about isolation requirements separately from transaction configuration.
@tConsistent
Declares the consistency guarantee required by an operation.
This is particularly useful in distributed systems where data may be replicated across multiple database nodes or read replicas.
Consistency Values
| Value | Description |
|---|---|
'strong' | Requires strong, read-your-writes consistency |
'eventual' | Allows stale reads in exchange for lower latency |
'snapshot' | Uses a point-in-time snapshot |
'bounded' | Allows bounded staleness |
The semantic requirement can be consumed by database routing or distributed data infrastructure to determine an appropriate read source.
@tReadOnly
Declares that a method must not perform write operations.
An ORM or database consumer can use this metadata to reject write queries executed within the operation.
This is useful for reporting, analytics, query services, and other operations where mutation should be explicitly prohibited.
@tTenantIsolated
Declares that database access must be scoped to the current tenant.
A tenant-aware database consumer can apply the current tenant boundary to generated queries:
This makes tenant isolation an explicit data-access requirement rather than relying entirely on individual repository implementations.
@tTenantIsolateddescribes the isolation requirement. The database, ORM, or infrastructure consumer is responsible for enforcing it.
Entity Decorators
AxilJS also provides semantic decorators for describing database entities and their mappings.
@tEntity
Declares a class as a database entity.
The entity metadata describes how the application model maps to persistent storage.
@tColumn
Maps a class property to a database column.
Column metadata can describe storage types and constraints required by the database consumer.
@tPrimaryKey
Marks a property as the entity's primary key.
The resulting metadata can be used by an ORM or schema-management consumer when generating queries and persistence mappings.
@tRelation
Declares a relationship between entities.
Relation Types
| Type | Description |
|---|---|
'one-to-one' | A single related entity |
'one-to-many' | A collection of related entities |
'many-to-one' | Multiple entities reference one entity |
'many-to-many' | A collection with a many-to-many relationship |
Relations remain semantic metadata, allowing the persistence layer to determine how relationships are represented and queried.
Full Entity Example
Entity decorators produce metadata that a persistence consumer can use for queries, migrations, and type-safe repositories. The same metadata can also be consumed by tooling such as database browsers, schema generators, and compliance systems.
Composing Data Semantics
Data decorators can be combined when an operation has multiple storage requirements.
This separates the business operation from infrastructure implementation while making its data requirements explicit:
- The operation performs a write.
- The write is routed to the primary database.
- A transaction boundary is required.
- Serializable isolation is required.
- Strong consistency is required.
- Data access must remain tenant-isolated.
The consumer layer can interpret these semantics and translate them into the appropriate database, ORM, transaction, and routing behavior.
Data Decorator Reference
| Decorator | Purpose |
|---|---|
@tDatabase | Declares database operation and routing semantics |
@tTransaction | Defines a transaction boundary |
@tIsolation | Declares a minimum transaction isolation level |
@tConsistent | Declares a required consistency guarantee |
@tReadOnly | Prevents write operations within a method |
@tTenantIsolated | Enforces tenant-scoped data access |
@tEntity | Declares a database entity |
@tColumn | Maps a property to a database column |
@tPrimaryKey | Declares an entity primary key |
@tRelation | Declares an entity relationship |