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.36 kB
TypeScript
import type { DB, PrimaryFieldType } from "../../../core";
import type { EntityData, RepoQueryIn, RepositoryResponse } from "../../../data";
import type { Insertable, Selectable, Updateable } from "kysely";
import type { ModuleApi, ResponseObject } from "../../../modules/ModuleApi";
import { type SWRConfiguration, type SWRResponse } from "swr";
import { type Api } from "../../../ui/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, Response = ResponseObject<RepositoryResponse<Selectable<Data>>>> {
create: (input: Insertable<Data>) => Promise<Response>;
read: (query?: RepoQueryIn) => Promise<ResponseObject<RepositoryResponse<Id extends undefined ? Selectable<Data>[] : Selectable<Data>>>>;
update: Id extends undefined ? (input: Updateable<Data>, id: Id) => Promise<Response> : (input: Updateable<Data>) => Promise<Response>;
_delete: Id extends undefined ? (id: Id) => 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;
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 {};