RecordType

Public Class

Table of Contents

A record type is a type that can be stored in a record store. It is created with createRecordType.

Signature
class RecordType<
  R extends BaseRecord,
  RequiredProperties extends keyof Omit<R, 'id' | 'typeName'>
> {}
References

BaseRecord, Omit


Constructor

Public Constructor

Constructs a new instance of the RecordType class

Parameters
NameDescription

typeName

R['typeName']

config

{
  readonly createDefaultProperties: () => Exclude<
    OmitMeta<R>,
    RequiredProperties
  >
  readonly migrations: Migrations
  readonly validator?:
    | {
        validate: (r: unknown) => R
      }
    | StoreValidator<R>
  readonly scope?: Scope
}
References

Exclude, OmitMeta, Migrations, StoreValidator, Scope


Properties

createDefaultProperties

Public Readonly Property

Signature
readonly createDefaultProperties: () => Exclude<
  OmitMeta<R>,
  RequiredProperties
>
References

Exclude, OmitMeta


isInstance

Public Property

Check whether a record is an instance of this record type.

Example
const result = recordType.isInstance(someRecord)
Parameters
NameDescription

record

The record to check.

Signature
isInstance: (record?: BaseRecord) => record is R
References

BaseRecord, record


migrations

Public Readonly Property

Signature
readonly migrations: Migrations
References

Migrations


scope

Public Readonly Property

Signature
readonly scope: Scope
References

Scope


typeName

Public Readonly Property

The unique type associated with this record.

Signature
readonly typeName: R['typeName']

validator

Public Readonly Property

Signature
readonly validator:
  | {
      validate: (r: unknown) => R
    }
  | StoreValidator<R>
References

StoreValidator


Methods

clone()

Public Method

Clone a record of this type.

Parameters
NameDescription

record

R

The record to clone.

Returns
R

The cloned record.


create()

Public Method

Create a new record of this type.

Parameters
NameDescription

properties

Pick<R, RequiredProperties> &
  Omit<Partial<R>, RequiredProperties>

The properties of the record.

Returns
R

The new record.

References

Pick, Omit, Partial


createCustomId()

Public Method

Create a new ID for this record type based on the given ID.

Example
const id = recordType.createCustomId('myId')
Parameters
NameDescription

id

string

The ID to base the new ID on.

Returns
ID<R>

The new ID.

References

ID


createId()

Public Method

Create a new ID for this record type.

Example
const id = recordType.createId()
Parameters

None

Returns
ID<R>

The new ID.

References

ID


isId()

Public Method

Check whether an id is an id of this type.

Example
const result = recordType.isIn('someId')
Parameters
NameDescription

id

(optional)

string

The id to check.

Returns
id is ID<R>

Whether the id is an id of this type.

References

id, ID


parseId()

Public Method

Takes an id like user:123 and returns the part after the colon 123

Parameters
NameDescription

id

string

The id

Returns
ID<R>
References

ID


validate()

Public Method

Check that the passed in record passes the validations for this type. Returns its input correctly typed if it does, but throws an error otherwise.

Parameters
NameDescription

record

unknown
Returns
R

withDefaultProperties()

Public Method

Create a new RecordType that has the same type name as this RecordType and includes the given default properties.

Example
const authorType = createRecordType('author', () => ({ living: true }))
const deadAuthorType = authorType.withDefaultProperties({ living: false })
Parameters
NameDescription

createDefaultProperties

() => DefaultProps
Returns
RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>

The new RecordType.

References

Omit, Partial, RecordType, Exclude


Edit this page
Last edited on 11 May 2023
RecordsDiffRecordVersion