@daveyplate/supabase-swr-entities
Version:
An entity management library for Supabase and SWR
71 lines (70 loc) • 3.06 kB
TypeScript
import { SWRConfiguration, SWRResponse } from "swr";
import { SWRInfiniteResponse } from "swr/infinite";
interface EntityResponse<T> extends SWRResponse {
entity: T;
updateEntity: (fields: Record<string, any>) => Promise<{
entity?: Record<string, any>;
error?: Error;
}>;
deleteEntity: () => Promise<{
success?: boolean;
error?: Error;
}>;
}
/**
* Hook for fetching an entity by `id` or params
*/
export declare function useEntity<T = Record<string, any>>(table?: string | null, id?: string | null, params?: Record<string, any> | null, swrConfig?: SWRConfiguration | null): EntityResponse<T>;
interface SharedEntitiesResponse<T> {
entities: T[];
count: number;
limit: number;
offset: number;
hasMore: boolean;
createEntity: (entity: object, optimisticFields?: object) => Promise<{
entity?: object;
error?: Error;
}>;
updateEntity: (id: string, fields: object) => Promise<{
entity?: object;
error?: Error;
}>;
deleteEntity: (id: string) => Promise<{
error?: Error;
}>;
mutateEntity: (entity: object) => void;
}
interface EntitiesResponse<T> extends SharedEntitiesResponse<T>, SWRResponse {
}
interface InfiniteEntitiesResponse<T> extends SharedEntitiesResponse<T>, SWRInfiniteResponse {
}
interface RealtimeOptions {
enabled?: boolean;
provider?: "peerjs" | "supabase";
room?: string;
listenOnly?: boolean;
}
/**
* Hook for fetching entities
* @param {string} table - The table name
* @param {Record<string, any>} [params] - The query parameters
* @param {SWRConfiguration} [swrConfig] - The SWR config
* @param {RealtimeOptions} [realtimeOptions] - The Realtime options
* @param {boolean} [realtimeOptions.enabled] - Whether Realtime is enabled
* @param {string} [realtimeOptions.provider] - The Realtime provider
* @param {string} [realtimeOptions.room] - The Realtime room
* @param {boolean} [realtimeOptions.listenOnly=false] - Whether to only listen for Realtime data
*/
export declare function useEntities<T = Record<string, any>>(table?: string | null, params?: Record<string, any> | null, swrConfig?: SWRConfiguration | null, realtimeOptions?: RealtimeOptions | null): EntitiesResponse<T>;
/**
* Hook for fetching entities with infinite scrolling support
* @param {string} table - The table name
* @param {SWRConfiguration} [swrConfig] - The SWR config
* @param {RealtimeOptions} [realtimeOptions] - The Realtime options
* @param {boolean} [realtimeOptions.enabled] - Whether Realtime is enabled
* @param {string} [realtimeOptions.provider] - The Realtime provider
* @param {string} [realtimeOptions.room] - The Realtime room
* @param {boolean} [realtimeOptions.listenOnly=false] - Whether to only listen for Realtime data
*/
export declare function useInfiniteEntities<T = Record<string, any>>(table?: string | null, params?: Record<string, any> | null, swrConfig?: SWRConfiguration | null, realtimeOptions?: RealtimeOptions | null): InfiniteEntitiesResponse<T>;
export {};