Skip to content

API reference > @kontsedal/olas-entities > defineEntity

defineEntity() function ​

Declare an entity type. name MUST be unique within a plugin instance; the plugin uses it as the partition key in the normalized store and the reverse index. idOf is a type-narrowing predicate masquerading as a partial function: return the id when the value matches this entity's shape, null (or undefined) when it doesn't.

Idiom: include a discriminating field check inside idOf so unrelated objects with an id field don't get falsely classified.

Signature:

typescript
export declare function defineEntity<T>(opts: EntityOptions<T>): EntityDef<T>;

Parameters ​

Parameter

Type

Description

opts

EntityOptions<T>

Returns:

EntityDef<T>

Example ​

ts
type Post = { id: string; title: string; likes: number }
const Post = defineEntity<Post>({
  name: 'Post',
  idOf: (v: any) => (v && typeof v.id === 'string' && typeof v.title === 'string')
    ? v.id
    : null,
})

Released under the MIT License.