RecordType
Table of contents
Public class
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'>,
> {}
packages/store/src/lib/RecordType.ts
Constructor
Public constructor
Constructs a new instance of the RecordType
class
Name | Description |
---|---|
|
|
|
|
Properties
createDefaultProperties
Public readonly property
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)
isInstance: (record?: UnknownRecord) => record is R
Name | Description |
---|---|
| The record to check. |
migrations
Public readonly property
readonly migrations: Migrations
scope
Public readonly property
readonly scope: RecordScope
typeName
Public readonly property
The unique type associated with this record.
readonly typeName: R['typeName']
validator
Public readonly property
readonly validator:
| {
validate: (r: unknown) => R
}
| StoreValidator<R>
Methods
clone()
Public method
Clone a record of this type.
clone(record: R): R
Name | Description |
---|---|
|
The record to clone. |
R
The cloned record.
create()
Public method
Create a new record of this type.
create(
properties: Pick<R, RequiredProperties> &
Omit<Partial<R>, RequiredProperties>
): R
Name | Description |
---|---|
|
The properties of the record. |
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')
createCustomId(id: string): IdOf<R>
Name | Description |
---|---|
|
The ID to base the new ID on. |
IdOf<R>
The new ID.
createId()
Public method
Create a new ID for this record type.
Example
const id = recordType.createId()
createId(customUniquePart?: string): IdOf<R>
Name | Description |
---|---|
|
|
IdOf<R>
The new ID.
isId()
Public method
Check whether an id is an id of this type.
Example
const result = recordType.isIn('someId')
isId(id?: string): id is IdOf<R>
Name | Description |
---|---|
|
The id to check. |
id is IdOf<R>
Whether the id is an id of this type.
parseId()
Public method
Takes an id like user:123
and returns the part after the colon 123
parseId(id: IdOf<R>): string
Name | Description |
---|---|
|
The id |
string
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.
validate(record: unknown): R
Name | Description |
---|---|
|
|
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 })
withDefaultProperties<
DefaultProps extends Omit<Partial<R>, 'id' | 'typeName'>,
>(
createDefaultProperties: () => DefaultProps
): RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>
Name | Description |
---|---|
|
|
RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>
The new RecordType.