UNPKG

baseflow-client

Version:

Official TypeScript/JavaScript client for BaseFlow - a powerful BaaS with OAuth authentication, RPC functions, database indexes, real-time features, and Supabase-compatible API

311 lines (310 loc) 10.9 kB
import { BaseFlowClient } from './client'; import { BaseFlowResponse, PostgrestBuilder as IPostgrestBuilder, PostgrestFilterBuilder as IPostgrestFilterBuilder, PostgrestQueryBuilder as IPostgrestQueryBuilder } from './types'; /** * A thenable class that represents a PostgREST request. */ export declare class PostgrestBuilder<T = any> implements IPostgrestBuilder<T> { protected client: BaseFlowClient; protected table: string; protected method: string; protected path: string; protected params: Record<string, any>; protected body: any; protected headers: Record<string, string>; constructor(client: BaseFlowClient, table: string); /** * Executes the request and returns a promise that resolves with the response. * * @param onfulfilled A function to be called when the promise is fulfilled. * @param onrejected A function to be called when the promise is rejected. * @returns A promise that resolves with the response. */ then<TResult1 = BaseFlowResponse<T>, TResult2 = never>(onfulfilled?: ((value: BaseFlowResponse<T>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>; } /** * A builder for creating PostgREST filters. */ export declare class PostgrestFilterBuilder<T = any> extends PostgrestBuilder<T> implements IPostgrestFilterBuilder<T> { /** * Adds an 'equals' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ eq(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'not equals' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ neq(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'greater than' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ gt(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'greater than or equal to' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ gte(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'less than' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ lt(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'less than or equal to' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ lte(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'like' filter to the query. * * @param column The column to filter on. * @param pattern The pattern to match. * @returns The filter builder. */ like(column: keyof T, pattern: string): PostgrestFilterBuilder<T>; /** * Adds a 'case-insensitive like' filter to the query. * * @param column The column to filter on. * @param pattern The pattern to match. * @returns The filter builder. */ ilike(column: keyof T, pattern: string): PostgrestFilterBuilder<T>; /** * Adds an 'is' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ is(column: keyof T, value: null | boolean): PostgrestFilterBuilder<T>; /** * Adds an 'in' filter to the query. * * @param column The column to filter on. * @param values The values to filter by. * @returns The filter builder. */ in(column: keyof T, values: any[]): PostgrestFilterBuilder<T>; /** * Adds a 'contains' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ contains(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'contained by' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ containedBy(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'range greater than' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ rangeGt(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'range greater than or equal to' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ rangeGte(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'range less than' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ rangeLt(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'range less than or equal to' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ rangeLte(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'range adjacent' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ rangeAdjacent(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds an 'overlaps' filter to the query. * * @param column The column to filter on. * @param value The value to filter by. * @returns The filter builder. */ overlaps(column: keyof T, value: any): PostgrestFilterBuilder<T>; /** * Adds a 'text search' filter to the query. * * @param column The column to filter on. * @param query The query to search for. * @returns The filter builder. */ textSearch(column: keyof T, query: string): PostgrestFilterBuilder<T>; /** * Adds a 'match' filter to the query. * * @param query The query to match. * @returns The filter builder. */ match(query: Record<keyof T, any>): PostgrestFilterBuilder<T>; /** * Adds a 'not' filter to the query. * * @param column The column to filter on. * @param operator The operator to use. * @param value The value to filter by. * @returns The filter builder. */ not(column: keyof T, operator: string, value: any): PostgrestFilterBuilder<T>; /** * Adds an 'or' filter to the query. * * @param filters The filters to apply. * @returns The filter builder. */ or(filters: string): PostgrestFilterBuilder<T>; /** * Adds a filter to the query. * * @param column The column to filter on. * @param operator The operator to use. * @param value The value to filter by. * @returns The filter builder. */ filter(column: keyof T, operator: string, value: any): PostgrestFilterBuilder<T>; protected addFilter(existing: string | undefined, newFilter: string, operator?: string): string; protected escapeValue(value: any): string; } /** * A builder for creating PostgREST queries. */ export declare class PostgrestQueryBuilder<T = any> extends PostgrestFilterBuilder<T> implements IPostgrestQueryBuilder<T> { /** * Specifies the columns to select. * Supports Supabase-like syntax with JOINs and nested selections. * * Examples: * - select('*') - Select all columns * - select('name, email') - Select specific columns * - select('title, author:users(name, email)') - JOIN with users table * - select('title, comments(count)') - Aggregate count of comments * * @param columns The columns to select. * @returns The query builder. */ select(columns?: string): PostgrestQueryBuilder<T>; /** * Inserts data into the table. * * @param data The data to insert. * @returns A promise that resolves with the response. */ insert(data: Partial<T> | Partial<T>[]): PostgrestBuilder<T>; /** * Updates data in the table. * * @param data The data to update. * @returns A filter builder for specifying which rows to update. */ update(data: Partial<T>): PostgrestFilterBuilder<T>; /** * Deletes data from the table. * * @returns A filter builder for specifying which rows to delete. */ delete(): PostgrestFilterBuilder<T>; /** * Adds an 'order by' clause to the query. * * @param column The column to order by. * @param options The ordering options. * @returns The query builder. */ order(column: keyof T, options?: { ascending?: boolean; nullsFirst?: boolean; }): PostgrestQueryBuilder<T>; /** * Limits the number of rows returned. * * @param count The maximum number of rows to return. * @returns The query builder. */ limit(count: number): PostgrestQueryBuilder<T>; /** * Specifies a range of rows to return. * * @param from The starting row index. * @param to The ending row index. * @returns The query builder. */ range(from: number, to: number): PostgrestQueryBuilder<T>; /** * Returns a single row from the query. * * @returns The filter builder. */ single(): PostgrestFilterBuilder<T>; /** * Returns a single row from the query, or null if no rows are found. * * @returns The filter builder. */ maybeSingle(): PostgrestFilterBuilder<T>; /** * Subscribe to real-time changes on this table * * @param event The event type to listen for * @param callback The callback function to execute when changes occur * @returns The subscription object */ on(event: 'INSERT' | 'UPDATE' | 'DELETE' | '*', callback: (payload: any) => void): RealtimeSubscriptionBuilder<T>; } /** * Real-time subscription builder */ export declare class RealtimeSubscriptionBuilder<T = any> { private client; private table; private event; private callback; private filters; constructor(client: BaseFlowClient, table: string, event: 'INSERT' | 'UPDATE' | 'DELETE' | '*', callback: (payload: any) => void, filters?: Record<string, any>); }