RecordType

See source code
Table of contents

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

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

Constructor

Constructs a new instance of the RecordType class

Parameters

NameDescription

typeName

R['typeName']

config

{
  readonly createDefaultProperties: () => Exclude<
    Omit<R, 'id' | 'typeName'>,
    RequiredProperties
  >
  readonly ephemeralKeys?: {
    readonly [K in Exclude<
      keyof R,
      'id' | 'typeName'
    >]: boolean
  }
  readonly scope?: RecordScope
  readonly validator?: StoreValidator<R>
}

Properties

createDefaultProperties

readonly
readonly createDefaultProperties: () => Exclude<
  Omit<R, 'id' | 'typeName'>,
  RequiredProperties
>

ephemeralKeys

readonlyoptional
readonly ephemeralKeys?: {
  readonly [K in Exclude<keyof R, 'id' | 'typeName'>]: boolean
}

ephemeralKeySet

readonly
readonly ephemeralKeySet: ReadonlySet<string>

scope

readonly
readonly scope: RecordScope

typeName

readonly

The unique type associated with this record.

readonly typeName: R['typeName']

validator

readonly
readonly validator: StoreValidator<R>

Methods

clone()

Clone a record of this type.

clone(record: R): R

Parameters

NameDescription

record

R

The record to clone.

Returns

R

The cloned record.


create()

Create a new record of this type.

create(
  properties: Pick<R, RequiredProperties> &
    Omit<Partial<R>, RequiredProperties>
): R

Parameters

NameDescription

properties

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

The properties of the record.

Returns

R

The new record.


createCustomId()

Deprecated:

  • Use createId instead.

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

createCustomId(id: string): IdOf<R>

Example

const id = recordType.createCustomId('myId')

Parameters

NameDescription

id

string

The ID to base the new ID on.

Returns

IdOf<R>

The new ID.


createId()

Create a new ID for this record type.

createId(customUniquePart?: string): IdOf<R>

Example

const id = recordType.createId()

Parameters

NameDescription

customUniquePart

string

Returns

IdOf<R>

The new ID.


isId()

Check whether an id is an id of this type.

isId(id?: string): id is IdOf<R>

Example

const result = recordType.isIn('someId')

Parameters

NameDescription

id

string

The id to check.

Returns

id is IdOf<R>

Whether the id is an id of this type.


isInstance()

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

isInstance(record?: UnknownRecord): record is R

Example

const result = recordType.isInstance(someRecord)

Parameters

NameDescription

record

The record to check.

Returns

record is R

Whether the record is an instance of this record type.


parseId()

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

parseId(id: IdOf<R>): string

Parameters

NameDescription

id

IdOf<R>

The id

Returns

string

validate()

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.

validate(record: unknown, recordBefore?: R): R

Parameters

NameDescription

record

unknown

recordBefore

R

Returns

R

withDefaultProperties()

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

withDefaultProperties<
  DefaultProps extends Omit<Partial<R>, 'id' | 'typeName'>,
>(
  createDefaultProperties: () => DefaultProps
): RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>

Example

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

Parameters

NameDescription

createDefaultProperties

() => DefaultProps

A function that returns the default properties of the new RecordType.

Returns

RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>

The new RecordType.


Prev
Vec
Next
Store