Skip to content
module@warp-drive/schema-dsl

Class Decorator Trait() ​

ts
function Trait(target: AnyConstructor): void;
function Trait(name: string, options?: TraitOptions): (target: AnyConstructor) => void;
function Trait(options: TraitOptions): (target: AnyConstructor) => void;

Call Signature ​

sincev5.9.0
ts
function Trait(target: AnyConstructor): void;

Defined in: entities/trait.ts:75

Class Decorator

Marks a class as a trait — a reusable collection of fields that can be composed onto a Resource (or another Trait) via trait.

The trait's name is derived from the class name (dasherized) unless a name string is passed explicitly. Its mode defaults to 'polaris'.

Each decorated property on the class contributes one entry to the compiled fields array, in declaration order. Unlike Resource, no $type or constructor DerivedField is ever added, since a trait's fields are merged into whichever resource composes it.

Parameters ​

target ​

AnyConstructor

Returns ​

void

Example ​

ts
import { Trait, field } from '@warp-drive/schema-dsl';

@Trait
export class Timestamped {
  @field({ type: 'date-time' }) declare createdAt: string;
  @field({ type: 'date-time' }) declare updatedAt: string;
}
json
{
  "name": "timestamped",
  "mode": "polaris",
  "fields": [
    { "kind": "field", "name": "createdAt", "type": "date-time" },
    { "kind": "field", "name": "updatedAt", "type": "date-time" }
  ]
}

Call Signature ​

sincev5.9.0
ts
function Trait(name: string, options?: TraitOptions): (target: AnyConstructor) => void;

Defined in: entities/trait.ts:76

Class Decorator

Marks a class as a trait — a reusable collection of fields that can be composed onto a Resource (or another Trait) via trait.

The trait's name is derived from the class name (dasherized) unless a name string is passed explicitly. Its mode defaults to 'polaris'.

Each decorated property on the class contributes one entry to the compiled fields array, in declaration order. Unlike Resource, no $type or constructor DerivedField is ever added, since a trait's fields are merged into whichever resource composes it.

Parameters ​

name ​

string

options? ​

TraitOptions

Returns ​

(target: AnyConstructor) => void

Example ​

ts
import { Trait, field } from '@warp-drive/schema-dsl';

@Trait
export class Timestamped {
  @field({ type: 'date-time' }) declare createdAt: string;
  @field({ type: 'date-time' }) declare updatedAt: string;
}
json
{
  "name": "timestamped",
  "mode": "polaris",
  "fields": [
    { "kind": "field", "name": "createdAt", "type": "date-time" },
    { "kind": "field", "name": "updatedAt", "type": "date-time" }
  ]
}

Call Signature ​

sincev5.9.0
ts
function Trait(options: TraitOptions): (target: AnyConstructor) => void;

Defined in: entities/trait.ts:77

Class Decorator

Marks a class as a trait — a reusable collection of fields that can be composed onto a Resource (or another Trait) via trait.

The trait's name is derived from the class name (dasherized) unless a name string is passed explicitly. Its mode defaults to 'polaris'.

Each decorated property on the class contributes one entry to the compiled fields array, in declaration order. Unlike Resource, no $type or constructor DerivedField is ever added, since a trait's fields are merged into whichever resource composes it.

Parameters ​

options ​

TraitOptions

Returns ​

(target: AnyConstructor) => void

Example ​

ts
import { Trait, field } from '@warp-drive/schema-dsl';

@Trait
export class Timestamped {
  @field({ type: 'date-time' }) declare createdAt: string;
  @field({ type: 'date-time' }) declare updatedAt: string;
}
json
{
  "name": "timestamped",
  "mode": "polaris",
  "fields": [
    { "kind": "field", "name": "createdAt", "type": "date-time" },
    { "kind": "field", "name": "updatedAt", "type": "date-time" }
  ]
}

Released under the MIT License.