RecordType
See source codeTable 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
Name | Description |
---|---|
|
|
|
|
Properties
createDefaultProperties
readonly createDefaultProperties: () => Exclude<
Omit<R, 'id' | 'typeName'>,
RequiredProperties
>
ephemeralKeys
readonly ephemeralKeys?: {
readonly [K in Exclude<keyof R, 'id' | 'typeName'>]: boolean
}
ephemeralKeySet
readonly ephemeralKeySet: ReadonlySet<string>
scope
readonly scope: RecordScope
typeName
The unique type associated with this record.
readonly typeName: R['typeName']
validator
readonly validator: StoreValidator<R>
Methods
clone()
Clone a record of this type.
clone(record: R): R
Parameters
Name | Description |
---|---|
|
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
Name | Description |
---|---|
|
The properties of the record. |
Returns
R
The new record.
createCustomId()
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
Name | Description |
---|---|
|
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
Name | Description |
---|---|
|
|
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
Name | Description |
---|---|
|
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
Name | Description |
---|---|
| 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
Name | Description |
---|---|
|
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
Name | Description |
---|---|
|
|
|
|
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
Name | Description |
---|---|
|
|
Returns
RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>
The new RecordType.