UNPKG

supabase-typed-query

Version:

Type-safe query builder and entity pattern for Supabase with TypeScript

80 lines 3.8 kB
import { MultiExecution, Query, SingleExecution, WhereConditions } from '../query/Query'; import { EmptyObject, SupabaseClientType, TableInsert, TableNames, TableRow, TableUpdate } from '../types'; import { FPromise, List, TaskOutcome } from 'functype'; export type TypedRecord<T, V> = Partial<Record<keyof T, V>>; export type EntityConfig = { /** Soft delete filtering. true = exclude deleted items, false = include deleted items */ softDelete: boolean; /** Partition key for multi-tenant isolation. e.g., { tenant_id: "123" } */ partitionKey?: Record<string, unknown>; }; export type WhereParams<T extends object = EmptyObject> = { where?: WhereConditions<T>; }; export type IsParams<T extends object = EmptyObject> = { is?: TypedRecord<T, null | boolean>; }; export type WhereinParams<T extends object = EmptyObject> = { wherein?: TypedRecord<T, unknown[]>; }; export type OrderParams<T extends object = EmptyObject> = { order?: [keyof T & string, { ascending?: boolean; nullsFirst?: boolean; }]; }; export type IdParam = { id: string; }; export type GetItemParams<T extends object = EmptyObject> = IdParam & WhereParams<T> & IsParams<T>; export type GetItemsParams<T extends object = EmptyObject> = WhereParams<T> & IsParams<T> & WhereinParams<T> & OrderParams<T>; export type AddItemsParams<T extends TableNames> = { items: TableInsert<T>[]; }; export type UpdateItemParams<T extends TableNames, Row extends object = EmptyObject> = IdParam & { item: TableUpdate<T>; } & WhereParams<Row> & IsParams<Row> & WhereinParams<Row>; export type UpdateItemsParams<T extends TableNames, Row extends object = EmptyObject> = { items: TableUpdate<T>[]; identity?: (keyof Row & string) | (keyof Row & string)[]; } & WhereParams<Row> & IsParams<Row> & WhereinParams<Row>; /** * Wrapper type for multi-result mutation operations that implements standard execution interface */ export type MutationMultiExecution<T> = FPromise<TaskOutcome<List<T>>> & MultiExecution<T>; /** * Wrapper type for single-result mutation operations that implements standard execution interface */ export type MutationSingleExecution<T> = FPromise<TaskOutcome<T>> & SingleExecution<T>; /** * Creates a multi-result mutation query that implements the standard execution interface */ export declare function MultiMutationQuery<T>(promise: FPromise<TaskOutcome<List<T>>>): MutationMultiExecution<T>; /** * Creates a single-result mutation query that implements the standard execution interface */ export declare function SingleMutationQuery<T>(promise: FPromise<TaskOutcome<T>>): MutationSingleExecution<T>; /** * Base interface for Entity instances */ export type IEntity<T extends TableNames> = { getItem(params: GetItemParams<TableRow<T>>): Query<T>; getItems(params?: GetItemsParams<TableRow<T>>): Query<T>; addItems(params: AddItemsParams<T>): MutationMultiExecution<TableRow<T>>; updateItem(params: UpdateItemParams<T, TableRow<T>>): MutationSingleExecution<TableRow<T>>; updateItems(params: UpdateItemsParams<T, TableRow<T>>): MutationMultiExecution<TableRow<T>>; }; /** * Creates an entity interface with methods for interacting with the given table. * @param client The Supabase client instance to use for queries. * @param name The name of the table to interact with. * @param config Configuration for entity behavior (required). * @returns An object with methods for interacting with the table. */ export declare const Entity: <T extends TableNames>(client: SupabaseClientType, name: T, config: EntityConfig) => IEntity<T>; /** * Type for an entity instance for a specific table * @deprecated Use IEntity<T> instead */ export type EntityType<T extends TableNames> = IEntity<T>; //# sourceMappingURL=Entity.d.ts.map