Skip to Content

Data API

Data hooks and methods from the Myco SDK.

Access

All data methods are on the data namespace of the SDK instance:

const myco = useMyco<EntityTypes>(); myco.data.useRecords(/* ... */); myco.data.createRecord(/* ... */);

Hooks

useRecords

Fetch a paginated list of records with automatic caching.

myco.data.useRecords<K>( entitySlug: K | null | undefined, options?: UseRecordsOptions ): UseRecordsResult<RecordEntry<TEntities[K]>>

Pass null or undefined for entitySlug to disable the query.

UseRecordsOptions

OptionTypeDefaultDescription
pageSizenumber20Records per page
initialPagenumber1Starting page
sortstringField to sort by
order"asc" | "desc"Sort direction
filterRecord<string, unknown>Filter criteria
onPageChange(page: number) => voidPage change callback

UseRecordsResult<T>

PropertyTypeDescription
dataT[] | undefinedRecords for the current page
status"pending" | "success" | "error"Query status
isFetchingbooleanCurrently fetching
errorError | nullQuery error
pagenumberCurrent page number
hasNextPagebooleanMore pages available
hasPreviousPagebooleanPrevious pages available
nextPage() => voidGo to next page
previousPage() => voidGo to previous page
setPage(page: number) => voidJump to specific page
mutate(data, options?) => voidUpdate cache directly
mutateOptimistic(optimisticData, mutation) => PromiseOptimistic update with rollback

useRecord

Fetch a single record by ID.

myco.data.useRecord<K>( entitySlug: K | null | undefined, recordId: string | null | undefined ): UseRecordResult<RecordEntry<TEntities[K]>>

Pass null or undefined for either parameter to disable the query.

UseRecordResult<T>

PropertyTypeDescription
dataT | undefinedThe record
status"pending" | "success" | "error"Query status
isFetchingbooleanCurrently fetching
errorError | nullQuery error
mutate(data, options?) => voidUpdate cache directly
mutateOptimistic(optimisticData, mutation) => PromiseOptimistic update with rollback

Async methods

For use outside of React components (event handlers, utilities, server-side).

getRecords

myco.data.getRecords<K>( entitySlug: K, options?: GetRecordsOptions ): Promise<PaginatedResponse<RecordEntry<TEntities[K]>>>

getRecord

myco.data.getRecord<K>( entitySlug: K, recordId: string ): Promise<RecordEntry<TEntities[K]>>

createRecord

myco.data.createRecord<K>( entitySlug: K, data: TEntities[K] ): Promise<RecordEntry<TEntities[K]>>

updateRecord

myco.data.updateRecord<K>( entitySlug: K, recordId: string, data: Partial<TEntities[K]> ): Promise<RecordEntry<TEntities[K]>>

deleteRecord

myco.data.deleteRecord<K>( entitySlug: K, recordId: string ): Promise<void>

batchGetRecords

myco.data.batchGetRecords<K>( entitySlug: K, ids: string[] ): Promise<RecordEntry<TEntities[K]>[]>

getEntities

myco.data.getEntities(): Promise<Entity[]>

getEntity

myco.data.getEntity<K>(entitySlug: K): Promise<Entity>

Types

import type { Entity, EntityField, RecordEntry, PaginatedResponse, GetRecordsOptions, } from "@myco-dev/sdk/data"; import type { UseRecordsOptions, UseRecordsResult, UseRecordResult, } from "@myco-dev/sdk/data";

Entity

interface Entity { id: string; slug: string; name: string; description?: string; fields: EntityField[]; }

EntityField

interface EntityField { id: string; name: string; slug: string; type: string; required?: boolean; options?: Record<string, unknown>; }

RecordEntry<T>

type RecordEntry<T> = T & { _id: string; _createdAt: string; _updatedAt: string; };

PaginatedResponse<T>

interface PaginatedResponse<T> { records: T[]; pagination: { total: number; limit: number; offset: number; hasMore: boolean; }; }

GetRecordsOptions

interface GetRecordsOptions { page?: number; pageSize?: number; sort?: string; order?: "asc" | "desc"; filter?: Record<string, unknown>; }