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: RecordScopetypeName
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): RParameters
| Name | Description |
|---|---|
| The record to clone. |
Returns
RThe cloned record.
create()
Create a new record of this type.
create(
properties: Expand<
Pick<R, RequiredProperties> & Omit<Partial<R>, RequiredProperties>
>
): RParameters
| Name | Description |
|---|---|
| The properties of the record. |
Returns
RThe new record.
createCustomId()
Deprecated:
- Use
createIdinstead.
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 RExample
const result = recordType.isInstance(someRecord)Parameters
| Name | Description |
|---|---|
| The record to check. |
Returns
record is RWhether 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>): stringParameters
| Name | Description |
|---|---|
| The id |
Returns
stringvalidate()
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): RParameters
| Name | Description |
|---|---|
| |
| |
Returns
RwithDefaultProperties()
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 |
|---|---|
| A function that returns the default properties of the new RecordType. |
Returns
RecordType<R, Exclude<RequiredProperties, keyof DefaultProps>>The new RecordType.