Table of contents

Public class

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

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

UnknownRecord

Source

packages/store/src/lib/RecordType.ts


Constructor

Public constructor

Constructs a new instance of the RecordType class

Parameters
NameDescription

typeName

R['typeName']

config

{
  readonly createDefaultProperties: () => Exclude<
    OmitMeta<R>,
    RequiredProperties
  >
  readonly scope?: RecordScope
  readonly validator?: StoreValidator<R>
}
References

StoreValidator


Properties

createDefaultProperties

Public readonly property

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

isInstance

Public property

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

Example
const result = recordType.isInstance(someRecord)
Signature
isInstance: (record?: UnknownRecord) => record is R
Parameters
NameDescription

record

The record to check.

References

UnknownRecord


scope

Public readonly property

Signature
readonly scope: RecordScope

typeName

Public readonly property

The unique type associated with this record.

Signature
readonly typeName: R['typeName']

validator

Public readonly property

Signature
readonly validator: StoreValidator<R>
References

StoreValidator


Methods

clone()

Public method

Clone a record of this type.

Signature
clone(record: R): R
Parameters
NameDescription

record

R

The record to clone.

Returns
R

The cloned record.


create()

Public method

Create a new record of this type.

Signature
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()

Public method

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

Example
const id = recordType.createCustomId('myId')
Signature
createCustomId(id: string): IdOf<R>
Parameters
NameDescription

id

string

The ID to base the new ID on.

Returns
IdOf<R>

The new ID.

References

IdOf


createId()

Public method

Create a new ID for this record type.

Example
const id = recordType.createId()
Signature
createId(customUniquePart?: string): IdOf<R>
Parameters
NameDescription

customUniquePart

string
Returns
IdOf<R>

The new ID.

References

IdOf


isId()

Public method

Check whether an id is an id of this type.

Example
const result = recordType.isIn('someId')
Signature
isId(id?: string): id is IdOf<R>
Parameters
NameDescription

id

string

The id to check.

Returns
id is IdOf<R>

Whether the id is an id of this type.

References

IdOf


parseId()

Public method

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

Signature
parseId(id: IdOf<R>): string
Parameters
NameDescription

id

IdOf<R>

The id

Returns
string
References

IdOf


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.

Signature
validate(record: unknown, recordBefore?: R): R
Parameters
NameDescription

record

unknown

recordBefore

R
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 })
Signature
withDefaultProperties<
  DefaultProps extends Omit<Partial<R>, 'id' | 'typeName'>,
>(
  createDefaultProperties: () => DefaultProps
): RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>
Parameters
NameDescription

createDefaultProperties

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

The new RecordType.

References

RecordType


RecordsDiffreverseRecordsDiff