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
| Option | Type | Default | Description |
|---|---|---|---|
pageSize | number | 20 | Records per page |
initialPage | number | 1 | Starting page |
sort | string | — | Field to sort by |
order | "asc" | "desc" | — | Sort direction |
filter | Record<string, unknown> | — | Filter criteria |
onPageChange | (page: number) => void | — | Page change callback |
UseRecordsResult<T>
| Property | Type | Description |
|---|---|---|
data | T[] | undefined | Records for the current page |
status | "pending" | "success" | "error" | Query status |
isFetching | boolean | Currently fetching |
error | Error | null | Query error |
page | number | Current page number |
hasNextPage | boolean | More pages available |
hasPreviousPage | boolean | Previous pages available |
nextPage | () => void | Go to next page |
previousPage | () => void | Go to previous page |
setPage | (page: number) => void | Jump to specific page |
mutate | (data, options?) => void | Update cache directly |
mutateOptimistic | (optimisticData, mutation) => Promise | Optimistic 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>
| Property | Type | Description |
|---|---|---|
data | T | undefined | The record |
status | "pending" | "success" | "error" | Query status |
isFetching | boolean | Currently fetching |
error | Error | null | Query error |
mutate | (data, options?) => void | Update cache directly |
mutateOptimistic | (optimisticData, mutation) => Promise | Optimistic 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>;
}