UNPKG

bknd

Version:

Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.

35 lines (34 loc) 3.38 kB
import type { DB, PrimaryFieldType, EntityData, RepoQueryIn, RepositoryResult, ResponseObject, ModuleApi } from "../../.."; import type { Insertable, Selectable, Updateable, Generated } from "kysely"; import { type SWRConfiguration, type SWRResponse } from "swr"; import { type Api } from "bknd/client"; export declare class UseEntityApiError<Payload = any> extends Error { response: ResponseObject<Payload>; constructor(response: ResponseObject<Payload>, fallback?: string); } interface UseEntityReturn<Entity extends keyof DB | string, Id extends PrimaryFieldType | undefined, Data = Entity extends keyof DB ? DB[Entity] : EntityData, ActualId = Data extends { id: infer I; } ? (I extends Generated<infer T> ? T : I) : never, Response = ResponseObject<RepositoryResult<Selectable<Data>>>> { create: (input: Insertable<Data>) => Promise<Response>; read: (query?: RepoQueryIn) => Promise<ResponseObject<RepositoryResult<Id extends undefined ? Selectable<Data>[] : Selectable<Data>>>>; update: Id extends undefined ? (input: Updateable<Data>, id: ActualId) => Promise<Response> : (input: Updateable<Data>) => Promise<Response>; _delete: Id extends undefined ? (id: PrimaryFieldType) => Promise<Response> : () => Promise<Response>; } export declare const useEntity: <Entity extends keyof DB | string, Id extends PrimaryFieldType | undefined = undefined, Data = Entity extends keyof DB ? DB[Entity] : EntityData>(entity: Entity, id?: Id) => UseEntityReturn<Entity, Id, Data>; export declare function makeKey(api: ModuleApi, entity: string, id?: PrimaryFieldType, query?: RepoQueryIn): string; export interface UseEntityQueryReturn<Entity extends keyof DB | string, Id extends PrimaryFieldType | undefined = undefined, Data = Entity extends keyof DB ? Selectable<DB[Entity]> : EntityData, Return = Id extends undefined ? ResponseObject<Data[]> : ResponseObject<Data>> extends Omit<SWRResponse<Return>, "mutate">, Omit<ReturnType<typeof useEntity<Entity, Id>>, "read"> { mutate: (id?: PrimaryFieldType) => Promise<any>; mutateRaw: SWRResponse<Return>["mutate"]; api: Api["data"]; key: string; } export declare const useEntityQuery: <Entity extends keyof DB | string, Id extends PrimaryFieldType | undefined = undefined>(entity: Entity, id?: Id, query?: RepoQueryIn, options?: SWRConfiguration & { enabled?: boolean; revalidateOnMutate?: boolean; }) => UseEntityQueryReturn<Entity, Id>; export declare function mutateEntityCache<Entity extends keyof DB | string, Data = Entity extends keyof DB ? DB[Entity] : EntityData>(api: Api["data"], entity: Entity, id: PrimaryFieldType, partialData: Partial<Selectable<Data>>): Promise<any[]>; interface UseEntityMutateReturn<Entity extends keyof DB | string, Id extends PrimaryFieldType | undefined = undefined, Data = Entity extends keyof DB ? DB[Entity] : EntityData> extends Omit<ReturnType<typeof useEntityQuery<Entity, Id>>, "mutate"> { mutate: Id extends undefined ? (id: PrimaryFieldType, data: Partial<Selectable<Data>>) => Promise<void> : (data: Partial<Selectable<Data>>) => Promise<void>; } export declare const useEntityMutate: <Entity extends keyof DB | string, Id extends PrimaryFieldType | undefined = undefined, Data = Entity extends keyof DB ? DB[Entity] : EntityData>(entity: Entity, id?: Id, options?: SWRConfiguration) => UseEntityMutateReturn<Entity, Id, Data>; export {};