Skip to content

Documentation / @warp-drive/schema-record / SchemaService

Defined in: schema.ts:190

A SchemaService designed to work with dynamically registered schemas.

SchemaService

Implements

Constructors

Constructor

ts
new SchemaService(): SchemaService;

Defined in: schema.ts:210

Returns

SchemaService

Properties

_derivations

ts
_derivations: Map<string, Derivation<any, any, any>>;

Defined in: schema.ts:207


_hashFns

ts
_hashFns: Map<string, HashFn>;

Defined in: schema.ts:205


_schemas

ts
_schemas: Map<string, InternalSchema>;

Defined in: schema.ts:203


_traits

ts
_traits: Set<string>;

Defined in: schema.ts:208


_transforms

ts
_transforms: Map<string, Transformation<Value, unknown>>;

Defined in: schema.ts:204

Methods

attributesDefinitionFor()

ts
attributesDefinitionFor(identifier): Record<string, LegacyAttributeField>;

Defined in: schema.ts:192

DEPRECATED - use fields instead

Returns definitions for all properties of the specified resource that are considered "attributes". Generally these are properties that are not related to book-keeping state on the client and do not represent a linkage to another resource.

The return value should be a dictionary of key:value pairs where the key is the attribute or property's name and value is an object with at least the property name which should also match key.

Optionally, this object may also specify type, which should be a string reference to a transform, and options which should be dictionary in which any key:value pairs are permissable.

For instance, when using @ember-data/model, the following attribute definition:

ts
class extends Model {
  @attr('string', { defaultValue: 'hello' }) greeting;
  @attr('date') birthday;
  @attr firstName;
}

Would be returned as:

js
{
  greeting: { name: 'greeting', type: 'string', options: { defaultValue: 'hello' } },
  birthday: { name: 'birthday', type: 'date' },
  firstName: { name: 'firstName' }
}

Parameters

identifier
type

string

Returns

Record<string, LegacyAttributeField>

Deprecated


derivation()

ts
derivation(field): Derivation;

Defined in: schema.ts:245

Returns the derivation registered with the name provided by field.type. Validates that the field is a valid DerivedField.

Parameters

field

DerivedField | { type: string; }

Returns

Derivation


doesTypeExist()

ts
doesTypeExist(type): boolean;

Defined in: schema.ts:191

DEPRECATED - use hasResource instead

Queries whether the SchemaService recognizes type as a resource type

Parameters

type

string

Returns

boolean

Deprecated


fields()

ts
fields(resource): Map<string, FieldSchema>;

Defined in: schema.ts:328

Queries for the fields of a given resource type or resource identity.

Should error if the resource type is not recognized.

Parameters

resource
type

string

Returns

Map<string, FieldSchema>


hashFn()

ts
hashFn(field): HashFn;

Defined in: schema.ts:262

Returns the hash function registered with the name provided by field.type. Validates that the field is a valid HashField.

Parameters

field

HashField | { type: string; }

Returns

HashFn


hasResource()

ts
hasResource(resource): boolean;

Defined in: schema.ts:338

Queries whether the SchemaService recognizes type as a resource type

Parameters

resource
type

string

Returns

boolean


hasTrait()

ts
hasTrait(type): boolean;

Defined in: schema.ts:222

Queries whether the SchemaService recognizes type as a resource trait

Parameters

type

string

Returns

boolean


registerDerivation()

ts
registerDerivation<R, T, FM>(derivation): void;

Defined in: schema.ts:320

Enables registration of a derivation.

The derivation can later be retrieved by the name attached to it's [Type] property.

Type Parameters

R

R

T

T

FM

FM extends | null | ObjectValue

Parameters

derivation

Derivation<R, T, FM>

Returns

void


registerHashFn()

ts
registerHashFn<T>(hashFn): void;

Defined in: schema.ts:324

Enables registration of a hashing function

The hashing function can later be retrieved by the name attached to it's [Type] property.

Type Parameters

T

T extends object

Parameters

hashFn

HashFn<T>

Returns

void


registerResource()

ts
registerResource(schema): void;

Defined in: schema.ts:288

Enables registration of a single Schema representing either a resource in PolarisMode or LegacyMode or an ObjectSchema representing an embedded structure in other schemas.

This can be useful for either pre-loading schema information or for registering schema information delivered by API calls or other sources just-in-time.

Parameters

schema

ResourceSchema | ObjectSchema

Returns

void


registerResources()

ts
registerResources(schemas): void;

Defined in: schema.ts:283

Enables registration of multiple Schemas at once.

This can be useful for either pre-loading schema information or for registering schema information delivered by API calls or other sources just-in-time.

Parameters

schemas

( | ResourceSchema | ObjectSchema)[]

Returns

void


registerTransformation()

ts
registerTransformation<T, PT>(transformation): void;

Defined in: schema.ts:316

Enables registration of a transformation.

The transformation can later be retrieved by the name attached to it's [Type] property.

Type Parameters

T

T extends Value = string

PT

PT = unknown

Parameters

transformation

Transformation<T, PT>

Returns

void


relationshipsDefinitionFor()

ts
relationshipsDefinitionFor(identifier): Record<string, LegacyRelationshipField>;

Defined in: schema.ts:193

DEPRECATED - use fields instead

Returns definitions for all properties of the specified resource that are considered "relationships". Generally these are properties that represent a linkage to another resource.

The return value should be a dictionary of key:value pairs where the key is the relationship or property's name and value is an object with at least the following properties:

  • name which should also match the key used in the dictionary.

  • kind which should be either belongsTo or hasMany

  • type which should be the related resource's string "type"

  • options which should be a dictionary allowing any key but with at least the below keys present.

  • options.async a boolean representing whether data for this relationship is typically loaded on-demand.

  • options.inverse a string or null representing the field name / key of the corresponding relationship on the inverse resource.

Additionally the following options properties are optional. See Polymorphic Relationships

  • options.polymorphic a boolean representing whether multiple resource types can be used to satisfy this relationship.
  • options.as a string representing the abstract type that the concrete side of a relationship must specify when fulfilling a polymorphic inverse.

For example, the following Model using @ember-data/model would generate this relationships definition by default:

js
class User extends Model {
  @belongsTo('user', { async: false, inverse: null }) bestFriend;
  @hasMany('user', { async: true, inverse: 'friends' }) friends;
  @hasMany('pet', { async: false, polymorphic: true, inverse: 'owner' }) pets;
}

Which would be returned as

js
{
  bestFriend: {
    name: 'bestFriend',
    kind: 'belongsTo',
    type: 'user',
    options: {
      async: false,
      inverse: null
    }
  },
  friends: {
    name: 'friends',
    kind: 'hasMany',
    type: 'user',
    options: {
      async: true,
      inverse: 'friends'
    }
  },
  pets: {
    name: 'pets',
    kind: 'hasMany',
    type: 'pet',
    options: {
      async: false,
      polymorphic: true,
      inverse: 'owner'
    }
  },
}

Parameters

identifier
type

string

Returns

Record<string, LegacyRelationshipField>

Deprecated


resource()

ts
resource(resource): 
  | ResourceSchema
  | ObjectSchema;

Defined in: schema.ts:279

Returns the schema for the provided resource type.

Parameters

resource

StableRecordIdentifier | { type: string; }

Returns

| ResourceSchema | ObjectSchema


resourceHasTrait()

ts
resourceHasTrait(resource, trait): boolean;

Defined in: schema.ts:225

Queries whether the given resource has the given trait

Parameters

resource

StableRecordIdentifier | { type: string; }

trait

string

Returns

boolean


resourceTypes()

ts
resourceTypes(): readonly string[];

Defined in: schema.ts:218

Returns all known resource types

Returns

readonly string[]


transformation()

ts
transformation(field): Transformation;

Defined in: schema.ts:228

Returns the transformation registered with the name provided by field.type. Validates that the field is a valid transformable.

Parameters

field

GenericField | ObjectField | ArrayField | { type: string; }

Returns

Transformation

Released under the MIT License.