UNPKG

@supabase-cache-helpers/postgrest-server

Version:

A collection of server-side caching utilities for working with Supabase.

73 lines (69 loc) 2.89 kB
import { Store } from './stores.cjs'; export { Entry, MemoryStore, MemoryStoreConfig, RedisStore, RedisStoreConfig, Value } from './stores.cjs'; import { PostgrestSingleResponse, PostgrestMaybeSingleResponse, PostgrestResponse } from '@supabase/postgrest-js'; import '@supabase-cache-helpers/postgrest-core'; import 'ioredis'; interface Context { waitUntil: (p: Promise<unknown>) => void; } declare class DefaultStatefulContext implements Context { waitUntil<TPromise = unknown>(_p: Promise<TPromise>): void; } type QueryCacheOpts = { stores: Store[]; fresh: number; stale: number; }; type OperationOpts = Pick<QueryCacheOpts, 'fresh' | 'stale'>; declare class QueryCache { private readonly inner; /** * To prevent concurrent requests of the same data, all queries are deduplicated using * this map. */ private readonly runningQueries; constructor(ctx: Context, opts: QueryCacheOpts); /** * Invalidate all cache entries for a given table */ invalidateQueries({ schema, table, }: { schema: string; table: string; }): Promise<void>; /** * Perform a cached postgrest query */ query<Result>(query: PromiseLike<PostgrestSingleResponse<Result>>, opts?: Partial<OperationOpts> & { store?: (result: PostgrestSingleResponse<Result>) => boolean; }): Promise<PostgrestSingleResponse<Result>>; /** * Perform a cached postgrest query */ query<Result>(query: PromiseLike<PostgrestMaybeSingleResponse<Result>>, opts?: Partial<OperationOpts> & { store?: (result: PostgrestMaybeSingleResponse<Result>) => boolean; }): Promise<PostgrestMaybeSingleResponse<Result>>; /** * Perform a cached postgrest query */ query<Result>(query: PromiseLike<PostgrestResponse<Result>>, opts?: Partial<OperationOpts> & { store?: (result: PostgrestResponse<Result>) => boolean; }): Promise<PostgrestResponse<Result>>; /** * Perform a cached postgrest query */ swr<Result>(query: PromiseLike<PostgrestSingleResponse<Result>>, opts?: OperationOpts): Promise<PostgrestSingleResponse<Result>>; /** * Perform a cached postgrest query */ swr<Result>(query: PromiseLike<PostgrestMaybeSingleResponse<Result>>, opts?: OperationOpts): Promise<PostgrestMaybeSingleResponse<Result>>; /** * Perform a cached postgrest query */ swr<Result>(query: PromiseLike<PostgrestResponse<Result>>, opts?: OperationOpts): Promise<PostgrestResponse<Result>>; /** * Deduplicating the origin load helps when the same value is requested many times at once and is * not yet in the cache. If we don't deduplicate, we'd create a lot of unnecessary load on the db. */ private dedupeQuery; } export { type Context, DefaultStatefulContext, type OperationOpts, QueryCache, type QueryCacheOpts, Store };