UNPKG

slonik-trpc

Version:
67 lines (66 loc) 2.65 kB
import { CommonQueryMethods } from 'slonik'; import { Fragment, Query } from './types'; import { makeQueryLoader, InferArgs } from '../core/makeQueryLoader'; import { GenerateMockOptions } from './zod'; export declare type QueryLoader = Pick<ReturnType<typeof makeQueryLoader>, "getLoadArgs" | "getSelectableFields" | "load">; export declare type PlanOutput = { 'Actual Loops'?: number; 'Actual Rows'?: number; 'Actual Startup Time'?: number; 'Actual Total Time'?: number; Alias?: string; 'Node Type'?: string; 'Parallel Aware'?: boolean; 'Plan Rows'?: number; 'Plan Width'?: number; 'Parent Relationship'?: string; 'Relation Name'?: string; 'Startup Cost'?: number; 'Total Cost'?: number; }; export declare type Plan = PlanOutput & { Plans?: Plan[]; }; export declare type QueryPlanOutput = { 'Execution Time': number; 'Planning Time': number; Plan: Plan; Triggers: any[]; }; export declare type ExplainOutput = { 'QUERY PLAN': QueryPlanOutput[]; }; export declare function makeQueryAnalyzer(db: Pick<CommonQueryMethods, "any">): { analyzeQuery: (query: Fragment | Query) => Promise<{ execution: number; planning: number; plan: Plan; }>; /** Runs each field as a separate query, to try and find which fields take more time to resolve. * This is useful when you have sub-queries as fields, and you want to know which sub-queries are heavy. * @returns A number in milliseonds for each field * */ benchmarkQueryLoaderFields: <TLoader extends QueryLoader>(queryLoader: TLoader, options?: { /** How many times to run a query for each field */ iterations?: number | undefined; /** How many fields queries to run concurrently */ concurrency?: number | undefined; args?: (Omit<InferArgs<TLoader, TLoader extends { loadPagination: (args_0: infer A) => any; } ? A : any>, "select"> & { ctx?: any; }) | undefined; } | undefined) => Promise<{ [key in ReturnType<TLoader["getSelectableFields"]>[number]]: number; }>; /** * Runs the query with mock filters, to see if all the filters are valid. * */ testAllFilters: <TLoader_1 extends QueryLoader>(queryLoader: TLoader_1, options?: GenerateMockOptions<TLoader_1, InferArgs<TLoader_1, TLoader_1 extends { loadPagination: (args_0: infer A) => any; } ? A : any> extends { where?: infer TWhere | undefined; } ? Omit<TWhere, "AND" | "OR" | "NOT"> : never> | undefined) => Promise<{ execution: number; planning: number; plan: Plan; }>; };