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

Call Signature

ts
function Resource(target): void;

Defined in: entities/resource.ts:119

Class Decorator

Marks a class as a resource schema — a primary resource with its own unique identity — compiling it to a PolarisResourceSchema, or a LegacyResourceSchema when ResourceOptions.legacy is set.

The resource's type is derived from the class name (dasherized, e.g. UserProfile compiles to 'user-profile') unless a type string is passed explicitly. Its identity defaults to { kind: '@id', name: 'id' } unless a property is decorated with id, or ResourceOptions.identityField names a different property.

Each decorated property on the class contributes one entry to the compiled fields array, in declaration order. Unless ResourceOptions.legacy is set, a $type and a constructorDerivedField are appended automatically: the first before any declared fields, the second after.

Parameters

target

AnyConstructor

Returns

void

Example

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

@Resource
export class User {
  @field declare firstName: string;
  @field declare lastName: string;
}
json
{
  "type": "user",
  "identity": { "kind": "@id", "name": "id" },
  "fields": [
    { "kind": "derived", "name": "$type", "type": "@identity", "options": { "key": "type" } },
    { "kind": "field", "name": "firstName" },
    { "kind": "field", "name": "lastName" },
    { "kind": "derived", "name": "constructor", "type": "@constructor" }
  ]
}

Passing a type overrides the derived name, and { legacy: true } compiles to a LegacyResourceSchema instead:

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

@Resource('blog-post', { legacy: true })
export class Post {
  @field declare title: string;
}
json
{
  "type": "blog-post",
  "identity": { "kind": "@id", "name": "id" },
  "fields": [
    { "kind": "field", "name": "title" }
  ],
  "legacy": true
}

Since

5.9.0

Call Signature

ts
function Resource(type, options?): (target) => void;

Defined in: entities/resource.ts:120

Class Decorator

Marks a class as a resource schema — a primary resource with its own unique identity — compiling it to a PolarisResourceSchema, or a LegacyResourceSchema when ResourceOptions.legacy is set.

The resource's type is derived from the class name (dasherized, e.g. UserProfile compiles to 'user-profile') unless a type string is passed explicitly. Its identity defaults to { kind: '@id', name: 'id' } unless a property is decorated with id, or ResourceOptions.identityField names a different property.

Each decorated property on the class contributes one entry to the compiled fields array, in declaration order. Unless ResourceOptions.legacy is set, a $type and a constructorDerivedField are appended automatically: the first before any declared fields, the second after.

Parameters

type

string

options?

ResourceOptions

Returns

(target) => void

Example

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

@Resource
export class User {
  @field declare firstName: string;
  @field declare lastName: string;
}
json
{
  "type": "user",
  "identity": { "kind": "@id", "name": "id" },
  "fields": [
    { "kind": "derived", "name": "$type", "type": "@identity", "options": { "key": "type" } },
    { "kind": "field", "name": "firstName" },
    { "kind": "field", "name": "lastName" },
    { "kind": "derived", "name": "constructor", "type": "@constructor" }
  ]
}

Passing a type overrides the derived name, and { legacy: true } compiles to a LegacyResourceSchema instead:

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

@Resource('blog-post', { legacy: true })
export class Post {
  @field declare title: string;
}
json
{
  "type": "blog-post",
  "identity": { "kind": "@id", "name": "id" },
  "fields": [
    { "kind": "field", "name": "title" }
  ],
  "legacy": true
}

Since

5.9.0

Call Signature

ts
function Resource(options): (target) => void;

Defined in: entities/resource.ts:121

Class Decorator

Marks a class as a resource schema — a primary resource with its own unique identity — compiling it to a PolarisResourceSchema, or a LegacyResourceSchema when ResourceOptions.legacy is set.

The resource's type is derived from the class name (dasherized, e.g. UserProfile compiles to 'user-profile') unless a type string is passed explicitly. Its identity defaults to { kind: '@id', name: 'id' } unless a property is decorated with id, or ResourceOptions.identityField names a different property.

Each decorated property on the class contributes one entry to the compiled fields array, in declaration order. Unless ResourceOptions.legacy is set, a $type and a constructorDerivedField are appended automatically: the first before any declared fields, the second after.

Parameters

options

ResourceOptions

Returns

(target) => void

Example

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

@Resource
export class User {
  @field declare firstName: string;
  @field declare lastName: string;
}
json
{
  "type": "user",
  "identity": { "kind": "@id", "name": "id" },
  "fields": [
    { "kind": "derived", "name": "$type", "type": "@identity", "options": { "key": "type" } },
    { "kind": "field", "name": "firstName" },
    { "kind": "field", "name": "lastName" },
    { "kind": "derived", "name": "constructor", "type": "@constructor" }
  ]
}

Passing a type overrides the derived name, and { legacy: true } compiles to a LegacyResourceSchema instead:

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

@Resource('blog-post', { legacy: true })
export class Post {
  @field declare title: string;
}
json
{
  "type": "blog-post",
  "identity": { "kind": "@id", "name": "id" },
  "fields": [
    { "kind": "field", "name": "title" }
  ],
  "legacy": true
}

Since

5.9.0

Released under the MIT License.