import type { ColumnType, ComparisonOperatorExpression, SelectQueryBuilder } from 'kysely'; import type { ZodObject } from 'zod'; import type { Fetcher } from '@cloudflare/workers-types'; import type { IsAny } from 'type-fest'; import type { z } from 'zod'; import type { Logger } from 'loglevel'; export type ApiConfig = { dialect?: string; paramPlaceholder?: string; timeStamp?: { create: string; update: string; }; options?: ApiOptions; database?: string; server?: string; logger?: Logger; hooks?: PHooks[]; autoIdFnc?: () => string; analyzeFnc?: (query: { sql: string; meta: any; time: number; }) => void; }; export type DbDriverConfig = ApiConfig; export type FetchDriverConfig = ApiConfig & { apiUrl: string; apiKey: string; binding?: Fetcher; }; export type BettterDriverConfig = { logger?: Logger; analyzeFnc?: (query: { sql: string; meta: string; time: number; }) => void; }; export type ApiOptions = { requestHeader?: (body: any) => Record; }; export type PActionBody = { action: 'selectFirst' | 'run' | 'selectAll'; sql: string; parameters: readonly any[]; } | { action: 'batchOneSmt'; sql: string; parameters: Array; } | { action: 'batchAllSmt'; batch: { sql: string; table: string; parameters: readonly any[]; action: 'selectFirst' | 'run' | 'selectAll'; }[]; }; export type DataBody = PActionBody | { action: 'bulks'; isTransaction: boolean; operations: Array; }; export type TableRelation = { ref: string; table: string; refTarget: string; select?: string[]; schema?: ZodObject; type?: 'many' | 'one'; }; export type TableDefinition = { schema?: ZodObject; table: keyof T & string; relations?: { [key: string]: TableRelation; }; }; export type QueryWhere = { [k in keyof V]?: V[k] | { [key in ComparisonOperatorExpression & string]?: any; }; }; export type Query = { select?: { [k in keyof V]?: boolean; }; where?: QueryWhere; skip?: number; take?: number; orderBy?: { [k in keyof V]?: 'asc' | 'desc'; }; }; export type ZodSchemaToKysely> = { [table in keyof z.output]: { [column in keyof z.output[table]]: ColumnType[table][column], z.input[table][column], z.input[table][column]>; }; }; export type ExtractFieldsWithRelations = { [K in keyof T as NonNullable extends { __relations?: any; } | { __relations?: any; }[] ? IsAny extends true ? never : K : never]: T[K]; }; export type QueryRelations = Query & { include?: { [k in keyof ExtractFieldsWithRelations]?: boolean | (V[k] extends Array | undefined ? { select: { [field in keyof Omit]?: boolean; }; } : { select: k extends keyof V ? { [field in keyof Omit, '__relations'>]?: boolean; } : never; }); }; }; export type PHookContext = { table: string; schema: any; autoId: boolean; }; export type PHooks = { onInsert?: (value: any, ctx: PHookContext) => void; onUpdate?: (value: any, ctx: PHookContext) => void; }; export type BatchResult = { rows: { key: string; results: any[]; table: string; }[]; error?: any; }; export type InsertTable = { id?: T['id'] | undefined; } & { [K in keyof T as NonNullable extends { __relations?: any; } | { __relations?: any; }[] ? never : K]: T[K]; }; export type ExtractResultFromQuery = T extends SelectQueryBuilder ? Z : never;