import { IMigrate } from './migrate/migrate-interfaces.ts';import { TableConstraint, CreateColumnDef, NodeLocation, DataTypeDef, FunctionArgumentMode, BinaryOperator, Statement } from 'https://deno.land/x/pgsql_ast_parser@10.5.2/mod.ts';
export type nil = undefined | null;
export type Schema = { name: string; fields: SchemaField[]; constraints?: TableConstraint[];}
export interface SchemaField extends Omit<CreateColumnDef, 'dataType' | 'kind' | 'name'> { type: IType | DataType; name: string; serial?: boolean;}
export interface IType { readonly primary: DataType; readonly name: string; toString(): string;
asArray(): IType;}
export enum DataType {
inet = 'inet', record = 'record', uuid = 'uuid', text = 'text', citext = 'citext', array = 'array', list = 'list', bigint = 'bigint', float = 'float', decimal = 'decimal', integer = 'integer', jsonb = 'jsonb', regtype = 'regtype', regclass = 'regclass', json = 'json', bytea = 'bytea', interval = 'interval', timestamp = 'timestamp', timestamptz = 'timestamptz', date = 'date', time = 'time', timetz = 'timetz', null = 'null', bool = 'bool',
point = 'point', line = 'line', lseg = 'lseg', box = 'box', path = 'path', polygon = 'polygon', circle = 'circle',}
export interface MemoryDbOptions { noErrorDiagnostic?: boolean; noAstCoverageCheck?: boolean; noIgnoreUnsupportedIndices?: boolean; readonly autoCreateForeignKeyIndices?: boolean;}
export interface IMemoryDb { readonly adapters: LibAdapters; readonly public: ISchema; getSchema(db?: string | null): ISchema; createSchema(name: string): ISchema; getTable<T = any>(table: string): IMemoryTable<T>; getTable<T = any>(table: string, nullIfNotFound?: boolean): IMemoryTable<T> | null;
on(event: 'query', handler: (query: string) => any): ISubscription; on(event: GlobalEvent, handler: () => any): ISubscription; on(event: GlobalEvent, handler: () => any): ISubscription; on(event: TableEvent, handler: (table: string) => any): ISubscription;
backup(): IBackup;
registerExtension(name: string, install: (schema: ISchema) => void): this;
registerLanguage(languageName: string, compiler: LanguageCompiler): this;}
export type QueryInterceptor = (query: string) => any[] | nil;
export type ArgDef = DataType | IType | ArgDefDetails;
export interface ArgDefDetails { type: IType; name?: string; mode?: FunctionArgumentMode;}
export type LanguageCompiler = (options: ToCompile) => CompiledFunction;
export interface ToCompile { functioName?: string | nil; code: string; schema: ISchema; args: ArgDefDetails[]; returns?: IType | nil;}
export class AdvancedResult { constructor(readonly result: any, outArgs: any[]) { }}
export type CompiledFunction = (...inArguments: any[]) => AdvancedResult | PlainResult;
export type PlainResult = Object | number | Date | null | void;
export interface CompiledFunctionResult { result?: any; outArgs?: any[];}
export interface IBackup { restore(): void;}
export interface LibAdapters { createPg(queryLatency?: number): { Pool: any; Client: any };
createPgPromise(queryLatency?: number): any;
createSlonik(queryLatency?: number): any;
createPgNative(queryLatency?: number): any;
createTypeormConnection(typeOrmConnection: any, queryLatency?: number): any;
createTypeormDataSource(typeOrmConnection: any, queryLatency?: number): any;
createKnex(queryLatency?: number, knexConfig?: object): any;
createMikroOrm(mikroOrmOptions: any, queryLatency?: number): Promise<any>}
export type QueryOrAst = string | Statement | Statement[];
export interface ISchema { many(query: QueryOrAst): any[]; none(query: QueryOrAst): void; one(query: QueryOrAst): any; declareTable(table: Schema): IMemoryTable; query(text: QueryOrAst): QueryResult;
queries(text: QueryOrAst): Iterable<QueryResult>;
getTable(table: string): IMemoryTable; getTable(table: string, nullIfNotFound?: boolean): IMemoryTable | null;
registerFunction(fn: FunctionDefinition, orReplace?: boolean): this;
registerOperator(fn: OperatorDefinition): this;
registerEquivalentType(type: IEquivalentType): IType;
getType(name: DataType): IType;
registerEnum(name: string, values: string[]): void;
migrate(config?: IMigrate.MigrationParams): Promise<void>;
interceptQueries(interceptor: QueryInterceptor): ISubscription;}
export interface FunctionDefinition { name: string;
args?: ArgDef[] | nil;
argsVariadic?: DataType | IType | nil;
returns?: DataType | IType | nil;
impure?: boolean;
allowNullArguments?: boolean;
implementation: CompiledFunction;}
export interface OperatorDefinition { operator: BinaryOperator;
left: DataType | IType;
right: DataType | IType;
commutative?: boolean;
returns: DataType | IType;
impure?: boolean;
allowNullArguments?: boolean;
implementation: CompiledFunction;}
export interface QueryResult { command: string; rowCount: number; fields: Array<FieldInfo>; rows: any[];
location: NodeLocation;}
export interface FieldInfo { name: string; type: DataType;}
export type TableEvent = 'seq-scan';export type GlobalEvent = 'query' | 'query-failed' | 'catastrophic-join-optimization' | 'schema-change' | 'create-extension';
export interface IMemoryTable<T = any> { on(event: TableEvent, handler: () => any): ISubscription; listIndices(): IndexDef[];
insert(item: Partial<T>): T;
find(template?: Partial<T> | nil, columns?: (keyof T)[]): Iterable<T>;}
export interface ISubscription { unsubscribe(): void;}
export interface IndexDef { name: string; expressions: string[];}
export class NotSupported extends Error { constructor(what?: string) { super('🔨 Not supported 🔨 ' + (what ? ': ' + what : '')); }
static never(value: never, msg?: string) { return new NotSupported(`${msg ?? ''} ${JSON.stringify(value)}`); }}
interface ErrorData { readonly error: string; readonly details?: string; readonly hint?: string; readonly code?: string;}export class QueryError extends Error { readonly data: ErrorData; constructor(err: string | ErrorData, code?: string) { super(typeof err === 'string' ? err : errDataToStr(err)); this.data = typeof err === 'string' ? { error: err, code } : err; }}
function errDataToStr(data: ErrorData) { const ret = ['ERROR: ' + data.error]; if (data.details) { ret.push('DETAIL: ' + data.details); } if (data.hint) { ret.push('HINT: ' + data.hint) } return ret.join('\n');}
export class CastError extends QueryError { constructor(from: string | DataType | IType, to: string | DataType | IType, inWhat?: string) { super(`cannot cast type ${typeof from === 'string' ? from : from.name} to ${typeof to === 'string' ? to : to.name}` + (inWhat ? ' in ' + inWhat : '')); }}
export class ColumnNotFound extends QueryError { constructor(col: string) { super(`column "${col}" does not exist`); }}
export class AmbiguousColumn extends QueryError { constructor(col: string) { super(`column reference "${col}" is ambiguous`); }}
export class RelationNotFound extends QueryError { constructor(tableName: string) { super(`relation "${tableName}" does not exist`); }}export class TypeNotFound extends QueryError { constructor(t: string | number | DataTypeDef) { super(`type "${typeof t !== 'object' ? t : typeDefToStr(t)}" does not exist`); }}
export class RecordExists extends QueryError { constructor() { super('Records already exists'); }}
export class PermissionDeniedError extends QueryError { constructor(what?: string) { super(what ? `permission denied: "${what}" is a system catalog` : 'permission denied'); }}
export function typeDefToStr(t: DataTypeDef): string { if (t.kind === 'array') { return typeDefToStr(t.arrayOf) + '[]'; } let ret = t.name; if (t.schema) { ret = t.schema + '.' + ret; } if (t.config?.length) { ret = ret + '(' + t.config.join(',') + ')'; } return ret;}
export interface IEquivalentType { readonly name: string; readonly equivalentTo: DataType | IType; isValid(value: any): boolean;}