UNPKG

@nuvix/pg

Version:

PostgreSQL integration for Nuvix

1,489 lines (1,384 loc) 105 kB
import { Client as Client$1, Submittable, QueryArrayConfig, QueryConfigValues, QueryArrayResult, QueryResultRow, QueryConfig, QueryResult, Pool } from 'pg'; import Redis from 'ioredis'; import events, { EventEmitter } from 'events'; import stream from 'stream'; import * as lodash from 'lodash'; interface GlobalConfig { enableRLS: boolean; enableCLS: boolean; computedFields: Record<string, unknown>; cacheEnabled: boolean; defaultRole: string; useNullAsDefault: boolean; } declare class Metadata { private static defaultConfig; private static globalConfig; private static eventEmitter; static get<K extends keyof GlobalConfig>(key: K): GlobalConfig[K]; static set<K extends keyof GlobalConfig>(key: K, value: GlobalConfig[K]): void; static onUpdate(listener: <K extends keyof GlobalConfig>(key: K, value: GlobalConfig[K]) => void): void; static createInstanceConfig(overrides: Partial<GlobalConfig>): GlobalConfig; static resetToDefaults(): void; } /** * Represents the shared application context. * * @remarks * This class can be used to maintain and access shared * state, configuration, or services across the application. * * @public */ /** * The `Context` class provides a mechanism to manage roles, authentication status, * and schema information. It supports role-based access control and allows for * enabling or disabling authentication status. */ declare class Context { /** * A record of roles assigned to the context. Each role is represented as a key * with a boolean value indicating its presence. By default, the `any` role is set to `true`. */ private roles; /** * The authentication status of the context. Defaults to `true` (enabled). */ private authStatus; /** * The schema associated with the context. Defaults to `"public"`. */ readonly schema: string; /** * Cache for permissions to improve performance */ private permissionCache; /** * Additional metadata for context */ private metadata; /** * Retrieves the current authentication status. * @returns `true` if authentication is enabled, otherwise `false`. */ getAuthStatus(): boolean; /** * Constructs a new `Context` instance. It can be initialized with another `Context` * instance, a partial `Context` object, or both. * * @param ctxOrObject - An optional `Context` instance or a partial `Context` object. * @param data - An optional partial `Context` object to override properties. */ constructor(ctx?: Context, data?: Partial<Context>); constructor(data?: Partial<Context>); /** * Sets multiple roles at once * @param roles - Array of roles to assign */ setRoles(roles: string[]): void; /** * Assigns a role to the context. * @param role - The name of the role to assign. */ setRole(role: string): void; /** * Removes a role from the context. * @param role - The name of the role to remove. */ unsetRole(role: string): void; /** * Retrieves all roles currently assigned to the context. * @returns An array of role names. */ getRoles(): string[]; /** * Clears all roles from the context. */ cleanRoles(): void; /** * Checks if a specific role is assigned to the context. * @param role - The name of the role to check. * @returns `true` if the role is assigned, otherwise `false`. */ isRole(role: string): boolean; /** * Check if the context has any of the specified roles * @param roles - Array of roles to check * @returns `true` if the context has any of the roles, otherwise `false` */ hasAnyRole(roles: string[]): boolean; /** * Check if the context has all of the specified roles * @param roles - Array of roles to check * @returns `true` if the context has all of the roles, otherwise `false` */ hasAllRoles(roles: string[]): boolean; /** * Enables authentication for the context. */ enable(): void; /** * Disables authentication for the context. */ disable(): void; /** * Sets a metadata value * @param key - The metadata key * @param value - The metadata value */ setMetadata(key: string, value: any): void; /** * Gets a metadata value * @param key - The metadata key * @param defaultValue - Default value if metadata doesn't exist * @returns The metadata value or default value */ getMetadata<T>(key: string, defaultValue?: T): T | undefined; /** * Clears the permission cache */ clearCache(): void; } type Schema = { name: string; type: "managed" | "unmanaged"; description?: string | undefined; metadata?: Record<string, any> | undefined; }; type SchemaOutput = { id: number; name: string; type: "managed" | "unmanaged" | "document"; created_at: string; updated_at: string; description?: string | undefined; metadata?: Record<string, any> | undefined; }; declare class EventSystem { private redis; private eventPrefix; constructor(client: Redis, eventPrefix?: string); publish(event: string, data: any): Promise<void>; subscribe(event: string, callback: (data: any) => void | Promise<void>): Promise<void>; } declare class Builder extends EventEmitter<[never]> { constructor(dataSource: any, context: any); client: any; context: any; and: this; _single: {}; _comments: any[]; _statements: any[]; _method: string; _debug: any; _joinFlag: string; _boolFlag: string; _notFlag: boolean; _asColumnFlag: boolean; toString(): any; toSQL(method: any, tz: any): any; clone(): any; timeout(ms: any, { cancel }?: {}): this; _timeout: number | undefined; _cancelOnTimeout: boolean | undefined; isValidStatementArg(statement: any): any; _validateWithArgs(alias: any, statementOrColumnList: any, nothingOrStatement: any, method: any): void; with(alias: any, statementOrColumnList: any, nothingOrStatement: any): this; updateFrom(name: any): this; using(tables: any): this; withMaterialized(alias: any, statementOrColumnList: any, nothingOrStatement: any): this; withNotMaterialized(alias: any, statementOrColumnList: any, nothingOrStatement: any): this; withWrapped(alias: any, statementOrColumnList: any, nothingOrStatement: any, materialized: any): this; withRecursive(alias: any, statementOrColumnList: any, nothingOrStatement: any): this; withRecursiveWrapped(alias: any, statementOrColumnList: any, nothingOrStatement: any): this; columns(column: any, ...args: any[]): this; comment(txt: any): this; as(column: any): this; hintComment(hints: any): this; withSchema(schemaName: any): this; table(tableName: any, options?: {}): this; distinct(...args: any[]): this; distinctOn(...args: any[]): this; join(table: any, first: any, ...args: any[]): this; innerJoin(...args: any[]): any; leftJoin(...args: any[]): any; leftOuterJoin(...args: any[]): any; rightJoin(...args: any[]): any; rightOuterJoin(...args: any[]): any; outerJoin(...args: any[]): any; fullOuterJoin(...args: any[]): any; crossJoin(...args: any[]): any; joinRaw(...args: any[]): any; get or(): any; get not(): any; where(column: any, operator: any, value: any, ...args: any[]): any; whereColumn(...args: any[]): this; orWhere(column: any, ...args: any[]): any; orWhereColumn(column: any, ...args: any[]): this; whereNot(column: any, ...args: any[]): any; whereNotColumn(...args: any[]): any; orWhereNot(...args: any[]): any; orWhereNotColumn(...args: any[]): any; _objectWhere(obj: any): this; whereRaw(sql: any, bindings: any): this; orWhereRaw(sql: any, bindings: any): any; whereWrapped(callback: any): this; whereExists(callback: any): this; orWhereExists(callback: any): any; whereNotExists(callback: any): any; orWhereNotExists(callback: any): any; whereIn(column: any, values: any): any; orWhereIn(column: any, values: any): any; whereNotIn(column: any, values: any): any; orWhereNotIn(column: any, values: any): any; whereNull(column: any): this; orWhereNull(column: any): any; whereNotNull(column: any): any; orWhereNotNull(column: any): any; whereBetween(column: any, values: any): this; whereNotBetween(column: any, values: any): any; orWhereBetween(column: any, values: any): any; orWhereNotBetween(column: any, values: any): any; _whereLike(type: any, column: any, value: any): this; whereLike(column: any, value: any): this; orWhereLike(column: any, value: any): any; whereILike(column: any, value: any): this; orWhereILike(column: any, value: any): any; groupBy(item: any, ...args: any[]): this; groupByRaw(sql: any, bindings: any): this; orderBy(column: any, direction: any, nulls?: string): this; _orderByArray(columnDefs: any): this; orderByRaw(sql: any, bindings: any): this; _union(clause: any, args: any): this; union(...args: any[]): this; unionAll(...args: any[]): this; intersect(...args: any[]): this; except(...args: any[]): this; having(column: any, operator: any, value: any, ...args: any[]): this; orHaving(column: any, ...args: any[]): this; havingWrapped(callback: any): this; havingNull(column: any): this; orHavingNull(callback: any): any; havingNotNull(callback: any): any; orHavingNotNull(callback: any): any; havingExists(callback: any): this; orHavingExists(callback: any): any; havingNotExists(callback: any): any; orHavingNotExists(callback: any): any; havingBetween(column: any, values: any): this; orHavingBetween(column: any, values: any): any; havingNotBetween(column: any, values: any): any; orHavingNotBetween(column: any, values: any): any; havingIn(column: any, values: any): any; orHavingIn(column: any, values: any): any; havingNotIn(column: any, values: any): any; orHavingNotIn(column: any, values: any): any; havingRaw(sql: any, bindings: any): this; orHavingRaw(sql: any, bindings: any): any; _setSkipBinding(attribute: any, options: any): void; offset(value: any, options: any): this; limit(value: any, options: any): this; count(column: any, options: any): this; min(column: any, options: any): this; max(column: any, options: any): this; sum(column: any, options: any): this; avg(column: any, options: any): this; countDistinct(...columns: any[]): this; sumDistinct(column: any, options: any): this; avgDistinct(column: any, options: any): this; increment(column: any, amount?: number): this; decrement(column: any, amount?: number): this; clearCounters(): this; first(...args: any[]): this; connection(_connection: any): this; _connection: any; pluck(column: any): this; /** * @deprecated */ clearSelect(): this; /** * @deprecated */ clearWhere(): this; /** * @deprecated */ clearGroup(): this; /** * @deprecated */ clearOrder(): this; /** * @deprecated */ clearHaving(): this; clear(statement: any): this; insert(values: any, returning: any, options: any): this; update(values: any, returning: any, options: any, ...args: any[]): this; returning(returning: any, options: any): this; onConflict(columns: any): OnConflictBuilder; delete(ret: any, options: any): this; truncate(tableName: any): this; columnInfo(column: any): this; forUpdate(...tables: any[]): this; forShare(...tables: any[]): this; forNoKeyUpdate(...tables: any[]): this; forKeyShare(...tables: any[]): this; skipLocked(): this; noWait(): this; fromJS(obj: any): this; fromRaw(sql: any, bindings: any): Builder; modify(callback: any, ...args: any[]): this; upsert(values: any, returning: any, options: any): void; _json(nameFunction: any, params: any): this; jsonExtract(...args: any[]): this; jsonSet(column: any, path: any, value: any, alias: any): this; jsonInsert(column: any, path: any, value: any, alias: any): this; jsonRemove(column: any, path: any, alias: any): this; _isJsonObject(jsonValue: any): boolean; _whereJsonWrappedValue(type: any, column: any, value: any, ...args: any[]): void; whereJsonObject(column: any, value: any): this; orWhereJsonObject(column: any, value: any): any; whereNotJsonObject(column: any, value: any): any; orWhereNotJsonObject(column: any, value: any): any; whereJsonPath(column: any, path: any, operator: any, value: any): this; orWhereJsonPath(column: any, path: any, operator: any, value: any): any; whereJsonSupersetOf(column: any, value: any): this; whereJsonNotSupersetOf(column: any, value: any): any; orWhereJsonSupersetOf(column: any, value: any): any; orWhereJsonNotSupersetOf(column: any, value: any): any; whereJsonSubsetOf(column: any, value: any): this; whereJsonNotSubsetOf(column: any, value: any): any; orWhereJsonSubsetOf(column: any, value: any): any; orWhereJsonNotSubsetOf(column: any, value: any): any; whereJsonHasNone(column: any, values: any): this; _analytic(alias: any, second: any, third: any): this; rank(...args: any[]): any; denseRank(...args: any[]): any; rowNumber(...args: any[]): any; _counter(column: any, amount: any): this; _bool(val: any, ...args: any[]): any; _not(val: any, ...args: any[]): any; _joinType(val: any, ...args: any[]): string | this; _analyticMethod(val: any, ...args: any[]): any; _analyticFlag: any; _aggregate(method: any, column: any, options?: {}): this; _clearGrouping(grouping: any): void; _isSelectQuery(): boolean; _hasLockMode(): boolean; select: (column: any, ...args: any[]) => Builder; column: (column: any, ...args: any[]) => Builder; andWhereNot: (column: any, ...args: any[]) => any; andWhereNotColumn: (...args: any[]) => any; andWhere: (column: any, operator: any, value: any, ...args: any[]) => any; andWhereColumn: (...args: any[]) => Builder; andWhereRaw: (sql: any, bindings: any) => Builder; andWhereBetween: (column: any, values: any) => Builder; andWhereNotBetween: (column: any, values: any) => any; andWhereJsonObject: (column: any, value: any) => Builder; andWhereNotJsonObject: (column: any, value: any) => any; andWhereJsonPath: (column: any, path: any, operator: any, value: any) => Builder; andWhereLike: (column: any, value: any) => Builder; andWhereILike: (column: any, value: any) => Builder; andHaving: (column: any, operator: any, value: any, ...args: any[]) => Builder; andHavingIn: (column: any, values: any) => any; andHavingNotIn: (column: any, values: any) => any; andHavingNull: (column: any) => Builder; andHavingNotNull: (callback: any) => any; andHavingExists: (callback: any) => Builder; andHavingNotExists: (callback: any) => any; andHavingBetween: (column: any, values: any) => Builder; andHavingNotBetween: (column: any, values: any) => any; from: (tableName: any, options?: {}) => Builder; into: (tableName: any, options?: {}) => Builder; del: (ret: any, options: any) => Builder; } declare namespace Builder { function extend(methodName: any, fn: any): void; } declare class OnConflictBuilder { constructor(builder: any, columns: any); builder: any; _columns: any; ignore(): any; merge(updates: any): any; then(): void; } interface Tables {} // # Core Types interface ResultTypes {} // # Generic Type Utilities /** Safe partial that handles unknown types gracefully */ type SafePartial<T> = Partial<AnyOrUnknownToOther<T, {}>>; /** Type that can be a single value or array */ type MaybeArray<T> = T | T[]; /** String keys of a type */ type StrKey<T> = string & keyof T; /** Convert unknown to any for backward compatibility */ type UnknownToAny<T> = unknown extends T ? any : T; /** Handle curly braces in type definitions */ type CurlyCurlyToAny<T> = T extends unknown ? (<U>() => U extends T ? 0 : 1) extends <U>() => U extends {} ? 0 : 1 ? any : T : never; /** Combined unknown and curly braces handler */ type UnknownOrCurlyCurlyToAny<T> = [UnknownToAny<T> | CurlyCurlyToAny<T>][0]; /** Convert any to unknown */ type AnyToUnknown<T> = unknown extends T ? unknown : T; /** Fallback type when first type is unknown */ type AnyOrUnknownToOther<T1, T2> = unknown extends T1 ? T2 : T1; /** Conditionally apply intersection when TParams is non-empty */ type AugmentParams<TTarget, TParams> = TParams extends {} ? keyof TParams extends never ? TTarget : {} & TTarget & TParams : TTarget; /** Union to intersection converter */ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ( k: infer I, ) => void ? I : never; /** SQL comparison operators */ type ComparisonOperator = "=" | ">" | ">=" | "<" | "<=" | "<>"; /** Extract array member type */ type ArrayMember<T> = T extends (infer M)[] ? M : never; /** Unwrap array to get member type or return original */ type UnwrapArrayMember<T> = T extends (infer M)[] ? M : T; /** Container to prevent TypeScript conditional type distribution */ interface Boxed<T> { _value: T; } /** Fallback to alternate type if incompatible */ type IncompatibleToAlt<T, TBase, TAlt> = T extends TBase ? T : TAlt; /** Convert to array if first type is already an array */ type ArrayIfAlready<T1, T2> = AnyToUnknown<T1> extends any[] ? T2[] : T2; /** Partial type with boxing to prevent distribution */ type PartialOrAny<TBase, TKeys> = Boxed<TKeys> extends Boxed<never> ? {} : Boxed<TKeys> extends Boxed<keyof TBase> ? SafePick<TBase, TKeys & keyof TBase> : any; /** Map alias types to their original types */ type MappedAliasType<TBase, TAliasMapping> = {} & { [K in keyof TAliasMapping]: TAliasMapping[K] extends keyof TBase ? TBase[TAliasMapping[K]] : any; }; // Container type for situations when we want a partial/intersection eventually // but the keys being selected or additional properties being augmented are not // all known at once and we would want to effectively build up a partial/intersection // over multiple steps. type DeferredKeySelection< // The base of selection. In intermediate stages this may be unknown. // If it remains unknown at the point of resolution, the selection will fall back to any TBase, // Union of keys to be selected // In intermediate stages this may be never. TKeys extends string, // Changes how the resolution should behave if TKeys is never. // If true, then we assume that some keys were selected, and if TKeys is never, we will fall back to any. // If false, and TKeys is never, then we select TBase in its entirety THasSelect extends true | false = false, // Mapping of aliases <key in result> -> <key in TBase> TAliasMapping extends {} = {}, // If enabled, then instead of extracting a partial, during resolution // we will pick just a single property. TSingle extends boolean = false, // Extra props which will be intersected with the result TIntersectProps extends {} = {}, // Extra props which will be unioned with the result TUnionProps = never, > = { // These properties are not actually used, but exist simply because // typescript doesn't end up happy when type parameters are unused _base: TBase; _hasSelection: THasSelect; _keys: TKeys; _aliases: TAliasMapping; _single: TSingle; _intersectProps: TIntersectProps; _unionProps: TUnionProps; }; // An companion namespace for DeferredKeySelection which provides type operators // to build up participants of intersection/partial over multiple invocations // and for final resolution. // // While the comments use wordings such as replacement and addition, it is important // to keep in mind that types are always immutable and all type operators return new altered types. declare namespace DeferredKeySelection { type Any = DeferredKeySelection<any, any, any, any, any, any, any>; // Replace the Base if already a deferred selection. // If not, create a new deferred selection with specified base. type SetBase<TSelection, TBase> = TSelection extends DeferredKeySelection< any, infer TKeys, infer THasSelect, infer TAliasMapping, infer TSingle, infer TIntersectProps, infer TUnionProps > ? DeferredKeySelection< TBase, TKeys, THasSelect, TAliasMapping, TSingle, TIntersectProps, TUnionProps > : DeferredKeySelection<TBase, never>; // If TSelection is already a deferred selection, then replace the base with TBase // If unknown, create a new deferred selection with TBase as the base // Else, retain original // // For practical reasons applicable to current context, we always return arrays of // deferred selections. So, this particular operator may not be useful in generic contexts. type ReplaceBase<TSelection, TBase> = UnwrapArrayMember<TSelection> extends DeferredKeySelection.Any ? ArrayIfAlready< TSelection, DeferredKeySelection.SetBase<UnwrapArrayMember<TSelection>, TBase> > : unknown extends UnwrapArrayMember<TSelection> ? ArrayIfAlready< TSelection, DeferredKeySelection.SetBase<unknown, TBase> > : TSelection; // Type operators to substitute individual type parameters: type SetSingle<TSelection, TSingle extends boolean> = TSelection extends DeferredKeySelection< infer TBase, infer TKeys, infer THasSelect, infer TAliasMapping, any, infer TIntersectProps, infer TUnionProps > ? DeferredKeySelection< TBase, TKeys, THasSelect, TAliasMapping, TSingle, TIntersectProps, TUnionProps > : never; type AddKey<TSelection, TKey extends string> = TSelection extends DeferredKeySelection< infer TBase, infer TKeys, any, infer TAliasMapping, infer TSingle, infer TIntersectProps, infer TUnionProps > ? DeferredKeySelection< TBase, TKeys | TKey, true, TAliasMapping, TSingle, TIntersectProps, TUnionProps > : DeferredKeySelection<unknown, TKey, true>; type AddAliases<TSelection, T extends {}> = TSelection extends DeferredKeySelection< infer TBase, infer TKeys, infer THasSelect, infer TAliasMapping, infer TSingle, infer TIntersectProps, infer TUnionProps > ? DeferredKeySelection< TBase, TKeys, THasSelect, TAliasMapping & T, TSingle, TIntersectProps, TUnionProps > : DeferredKeySelection<unknown, never, false, T>; type AddUnionMember<TSelection, T> = TSelection extends DeferredKeySelection< infer TBase, infer TKeys, infer THasSelect, infer TAliasMapping, infer TSingle, infer TIntersectProps, infer TUnionProps > ? DeferredKeySelection< TBase, TKeys, THasSelect, TAliasMapping, TSingle, TIntersectProps, TUnionProps | T > : DeferredKeySelection<TSelection, never, false, {}, false, {}, T>; // Convenience utility to set base, keys and aliases in a single type // application type Augment< T, TBase, TKey extends string, TAliasMapping extends {} = {}, > = AddAliases<AddKey<SetBase<T, TBase>, TKey>, TAliasMapping>; // Core resolution logic -- Refer to docs for DeferredKeySelection for specifics type ResolveOne<TSelection> = TSelection extends DeferredKeySelection< infer TBase, infer TKeys, infer THasSelect, infer TAliasMapping, infer TSingle, infer TIntersectProps, infer TUnionProps > ? UnknownOrCurlyCurlyToAny< // ^ We convert final result to any if it is unknown for backward compatibility. // Historically pg typings have been liberal with returning any and changing // default return type to unknown would be a major breaking change for users. // // So we compromise on type safety here and return any. | AugmentParams< AnyToUnknown<TBase> extends {} ? // ^ Conversion of any -> unknown is needed here to prevent distribution // of any over the conditional TSingle extends true ? TKeys extends keyof TBase ? TBase[TKeys] : any : AugmentParams< true extends THasSelect ? PartialOrAny<TBase, TKeys> : TBase, MappedAliasType<TBase, TAliasMapping> > : unknown, TIntersectProps > | TUnionProps > : TSelection; type Resolve<TSelection> = TSelection extends DeferredKeySelection.Any ? PG.ResolveTableType<ResolveOne<TSelection>> : TSelection extends DeferredKeySelection.Any[] ? PG.ResolveTableType<ResolveOne<TSelection[0]>>[] : TSelection extends (infer I)[] ? UnknownOrCurlyCurlyToAny<PG.ResolveTableType<I>>[] : UnknownOrCurlyCurlyToAny<PG.ResolveTableType<TSelection>>; } type AggregationQueryResult< TResult, TIntersectProps2 extends {}, > = ArrayIfAlready< TResult, UnwrapArrayMember<TResult> extends DeferredKeySelection< infer TBase, infer TKeys, infer THasSelect, infer TAliasMapping, infer TSingle, infer TIntersectProps, infer TUnionProps > ? true extends THasSelect ? DeferredKeySelection< TBase, TKeys, THasSelect, TAliasMapping, TSingle, TIntersectProps & TIntersectProps2, TUnionProps > : DeferredKeySelection<{}, never, true, {}, false, TIntersectProps2> : TIntersectProps2 >; // If we have more categories of deferred selection in future, // this will combine all of them type ResolveResult<S> = DeferredKeySelection.Resolve<S>; type Client = Function; type Dict<T = any> = { [k: string]: T }; type SafePick<T, K extends keyof T> = T extends {} ? Pick<T, K> : any; type TableOptions = PgTableOptions; interface PgTableOptions { only?: boolean; schema?: string; } interface DMLOptions { includeTriggerModifications?: boolean; } declare function pg1<TRecord extends {} = any, TResult = unknown[]>( client: string | DataSource, ): PG<TRecord, TResult>; declare namespace pg$1 { export { pg1, pg1 as default, PG }; export class QueryBuilder { static extend( methodName: string, fn: <TRecord extends {} = any, TResult extends {} = unknown[]>( this: PG.QueryBuilder<TRecord, TResult>, ...args: any[] ) => | PG.QueryBuilder<TRecord, TResult> | Promise< | PG.QueryBuilder<TRecord | TResult> | DeferredKeySelection.Resolve<TResult> >, ): void; } export class TimeoutError extends Error {} } interface PG<TRecord extends {} = any, TResult = any[]> extends PG.QueryInterface<TRecord, TResult>, events.EventEmitter { <TTable extends PG.TableNames>( tableName: TTable, options?: TableOptions, ): PG.QueryBuilder< PG.TableType<TTable>, DeferredKeySelection<PG.ResolveTableType<PG.TableType<TTable>>, never>[] >; < TRecord2 extends {} = TRecord, TResult2 = DeferredKeySelection<TRecord2, never>[], >( tableName?: PG.TableDescriptor | PG.AliasDict, options?: TableOptions, ): PG.QueryBuilder<TRecord2, TResult2>; VERSION: string; __pg__: string; raw: PG.RawBuilder<TRecord>; transactionProvider(config?: PG.TransactionConfig): PG.TransactionProvider; transaction(config?: PG.TransactionConfig): Promise<PG.Transaction>; transaction( transactionScope?: null, config?: PG.TransactionConfig, ): Promise<PG.Transaction>; transaction<T>( transactionScope: (trx: PG.Transaction) => Promise<T> | void, config?: PG.TransactionConfig, ): Promise<T>; initialize(config?: any): void; destroy(callback: Function): void; destroy(): Promise<void>; batchInsert<TRecord2 extends {} = TRecord, TResult2 = number[]>( tableName: PG.TableDescriptor, data: TRecord2 extends PG.CompositeTableType<unknown> ? ReadonlyArray<PG.ResolveTableType<TRecord2, "insert">> : ReadonlyArray<PG.DbRecordArr<TRecord2>>, chunkSize?: number, ): PG.BatchInsertBuilder<TRecord2, TResult2>; queryBuilder< TRecord2 extends {} = TRecord, TResult2 = TResult, >(): PG.QueryBuilder<TRecord2, TResult2>; client: any; fn: PG.FunctionHelper; ref: PG.RefBuilder; userParams: Record<string, any>; withUserParams(params: Record<string, any>): PG; isTransaction?: boolean; } declare namespace PG { // // Utility Types // type Value = | string | number | boolean | null | Date | Array<string> | Array<number> | Array<Date> | Array<boolean> | Buffer | object | PG.Raw; interface ValueDict extends Dict<Value | PG.QueryBuilder> {} interface AliasDict extends Dict<string> {} type ColumnDescriptor<TRecord extends {}, TResult> = | string | PG.Raw | PG.QueryBuilder<TRecord, TResult> | Dict<string>; type InferrableColumnDescriptor<TRecord extends {}> = | keyof TRecord | PG.Ref<any, any> | Dict<keyof TRecord>; type TableDescriptor = string | PG.Raw | PG.QueryBuilder; type Lookup< TRegistry extends {}, TKey extends string, TDefault = never, > = TKey extends keyof TRegistry ? TRegistry[TKey] : TDefault; type MaybeRawColumn<TColumn> = TColumn | Raw<TColumn>; type MaybeRawRecord<TRecord> = { [K in keyof TRecord]: MaybeRawColumn<TRecord[K]>; }; type DbColumn<TColumn> = Readonly<MaybeRawColumn<TColumn>>; type DbRecord<TRecord> = Readonly<SafePartial<MaybeRawRecord<TRecord>>>; type DbRecordArr<TRecord> = Readonly<MaybeArray<DbRecord<TRecord>>>; // // Table Types // export type CompositeTableType< TBase, TInsert = TBase, TUpdate = Partial<TInsert>, TUpsert = Partial<TInsert>, > = { base: TBase; insert: TInsert; update: TUpdate; upsert: TUpsert; }; type TableNames = keyof Tables; type TableInterfaceScope = keyof CompositeTableType<unknown>; type TableType<TTable extends keyof Tables> = Tables[TTable]; type ResolveTableType< TCompositeTableType, TScope extends TableInterfaceScope = "base", > = TCompositeTableType extends CompositeTableType<{}> ? TCompositeTableType[TScope] : TCompositeTableType; // // Query Building Types // interface OnConflictQueryBuilder<TRecord extends {}, TResult> { ignore(): QueryBuilder<TRecord, TResult>; merge( mergeColumns?: (keyof ResolveTableType<TRecord, "update">)[], ): QueryBuilder<TRecord, TResult>; merge( data?: Extract<DbRecord<ResolveTableType<TRecord, "update">>, object>, ): QueryBuilder<TRecord, TResult>; } // // QueryInterface // type ClearStatements = | "with" | "select" | "columns" | "hintComments" | "where" | "union" | "using" | "join" | "group" | "order" | "having" | "limit" | "offset" | "counter" | "counters"; interface QueryInterface<TRecord extends {} = any, TResult = any> { select: Select<TRecord, TResult>; as: As<TRecord, TResult>; columns: Select<TRecord, TResult>; column: Select<TRecord, TResult>; comment: Comment<TRecord, TResult>; hintComment: HintComment<TRecord, TResult>; from: Table<TRecord, TResult>; fromRaw: Table<TRecord, TResult>; into: Table<TRecord, TResult>; table: Table<TRecord, TResult>; distinct: Distinct<TRecord, TResult>; distinctOn: DistinctOn<TRecord, TResult>; // Joins join: Join<TRecord, TResult>; joinRaw: JoinRaw<TRecord, TResult>; innerJoin: Join<TRecord, TResult>; leftJoin: Join<TRecord, TResult>; leftOuterJoin: Join<TRecord, TResult>; rightJoin: Join<TRecord, TResult>; rightOuterJoin: Join<TRecord, TResult>; outerJoin: Join<TRecord, TResult>; fullOuterJoin: Join<TRecord, TResult>; crossJoin: Join<TRecord, TResult>; // Json manipulation jsonExtract: JsonExtract<TRecord, TResult>; jsonSet: JsonSet<TRecord, TResult>; jsonInsert: JsonInsert<TRecord, TResult>; jsonRemove: JsonRemove<TRecord, TResult>; // Using using: Using<TRecord, TResult>; // Withs with: With<TRecord, TResult>; withMaterialized: With<TRecord, TResult>; withNotMaterialized: With<TRecord, TResult>; withRecursive: With<TRecord, TResult>; withRaw: WithRaw<TRecord, TResult>; withSchema: WithSchema<TRecord, TResult>; withWrapped: WithWrapped<TRecord, TResult>; // Wheres where: Where<TRecord, TResult>; andWhere: Where<TRecord, TResult>; orWhere: Where<TRecord, TResult>; whereNot: Where<TRecord, TResult>; andWhereNot: Where<TRecord, TResult>; orWhereNot: Where<TRecord, TResult>; whereRaw: WhereRaw<TRecord, TResult>; orWhereRaw: WhereRaw<TRecord, TResult>; andWhereRaw: WhereRaw<TRecord, TResult>; whereWrapped: WhereWrapped<TRecord, TResult>; havingWrapped: WhereWrapped<TRecord, TResult>; whereExists: WhereExists<TRecord, TResult>; orWhereExists: WhereExists<TRecord, TResult>; whereNotExists: WhereExists<TRecord, TResult>; orWhereNotExists: WhereExists<TRecord, TResult>; whereIn: WhereIn<TRecord, TResult>; orWhereIn: WhereIn<TRecord, TResult>; whereNotIn: WhereIn<TRecord, TResult>; orWhereNotIn: WhereIn<TRecord, TResult>; whereLike: Where<TRecord, TResult>; andWhereLike: Where<TRecord, TResult>; orWhereLike: Where<TRecord, TResult>; whereILike: Where<TRecord, TResult>; andWhereILike: Where<TRecord, TResult>; orWhereILike: Where<TRecord, TResult>; whereNull: WhereNull<TRecord, TResult>; orWhereNull: WhereNull<TRecord, TResult>; whereNotNull: WhereNull<TRecord, TResult>; orWhereNotNull: WhereNull<TRecord, TResult>; whereBetween: WhereBetween<TRecord, TResult>; orWhereBetween: WhereBetween<TRecord, TResult>; andWhereBetween: WhereBetween<TRecord, TResult>; whereNotBetween: WhereBetween<TRecord, TResult>; orWhereNotBetween: WhereBetween<TRecord, TResult>; andWhereNotBetween: WhereBetween<TRecord, TResult>; whereJsonObject: WhereJsonObject<TRecord, TResult>; orWhereJsonObject: WhereJsonObject<TRecord, TResult>; andWhereJsonObject: WhereJsonObject<TRecord, TResult>; whereNotJsonObject: WhereJsonObject<TRecord, TResult>; orWhereNotJsonObject: WhereJsonObject<TRecord, TResult>; andWhereNotJsonObject: WhereJsonObject<TRecord, TResult>; whereJsonPath: WhereJsonPath<TRecord, TResult>; orWhereJsonPath: WhereJsonPath<TRecord, TResult>; andWhereJsonPath: WhereJsonPath<TRecord, TResult>; whereJsonSupersetOf: WhereJsonObject<TRecord, TResult>; orWhereJsonSupersetOf: WhereJsonObject<TRecord, TResult>; andWhereJsonSupersetOf: WhereJsonObject<TRecord, TResult>; whereJsonNotSupersetOf: WhereJsonObject<TRecord, TResult>; orWhereJsonNotSupersetOf: WhereJsonObject<TRecord, TResult>; andWhereJsonNotSupersetOf: WhereJsonObject<TRecord, TResult>; whereJsonSubsetOf: WhereJsonObject<TRecord, TResult>; orWhereJsonSubsetOf: WhereJsonObject<TRecord, TResult>; andWhereJsonSubsetOf: WhereJsonObject<TRecord, TResult>; whereJsonNotSubsetOf: WhereJsonObject<TRecord, TResult>; orWhereJsonNotSubsetOf: WhereJsonObject<TRecord, TResult>; andWhereJsonNotSubsetOf: WhereJsonObject<TRecord, TResult>; // Group by groupBy: GroupBy<TRecord, TResult>; groupByRaw: RawQueryBuilder<TRecord, TResult>; // Order by orderBy: OrderBy<TRecord, TResult>; orderByRaw: RawQueryBuilder<TRecord, TResult>; // Partition by partitionBy: PartitionBy<TRecord, TResult>; // Unions union: Union<TRecord, TResult>; unionAll: Union<TRecord, TResult>; intersect: Intersect<TRecord, TResult>; except: Except<TRecord, TResult>; // Having having: Having<TRecord, TResult>; andHaving: Having<TRecord, TResult>; havingRaw: RawQueryBuilder<TRecord, TResult>; orHaving: Having<TRecord, TResult>; orHavingRaw: RawQueryBuilder<TRecord, TResult>; havingIn: HavingRange<TRecord, TResult>; orHavingNotBetween: HavingRange<TRecord, TResult>; havingNotBetween: HavingRange<TRecord, TResult>; orHavingBetween: HavingRange<TRecord, TResult>; havingBetween: HavingRange<TRecord, TResult>; havingNotIn: HavingRange<TRecord, TResult>; andHavingNotIn: HavingRange<TRecord, TResult>; orHavingNotIn: HavingRange<TRecord, TResult>; havingNull: HavingNull<TRecord, TResult>; havingNotNull: HavingNull<TRecord, TResult>; orHavingNull: HavingNull<TRecord, TResult>; orHavingNotNull: HavingNull<TRecord, TResult>; // Clear clearSelect(): QueryBuilder< TRecord, UnwrapArrayMember<TResult> extends DeferredKeySelection< infer TBase, infer TKeys, true, any, any, any, any > ? DeferredKeySelection<TBase, never>[] : TResult >; clearWhere(): QueryBuilder<TRecord, TResult>; clearGroup(): QueryBuilder<TRecord, TResult>; clearOrder(): QueryBuilder<TRecord, TResult>; clearHaving(): QueryBuilder<TRecord, TResult>; clearCounters(): QueryBuilder<TRecord, TResult>; clear(statement: ClearStatements): QueryBuilder<TRecord, TResult>; // Paging offset( offset: number, options?: boolean | Readonly<{ skipBinding?: boolean }>, ): QueryBuilder<TRecord, TResult>; limit( limit: number, options?: string | Readonly<{ skipBinding?: boolean }>, ): QueryBuilder<TRecord, TResult>; // Aggregation count: AsymmetricAggregation< TRecord, TResult, Lookup<ResultTypes, "Count", number | string> >; countDistinct: AsymmetricAggregation< TRecord, TResult, Lookup<ResultTypes, "Count", number | string> >; min: TypePreservingAggregation<TRecord, TResult>; max: TypePreservingAggregation<TRecord, TResult>; sum: TypePreservingAggregation<TRecord, TResult>; sumDistinct: TypePreservingAggregation<TRecord, TResult>; avg: TypePreservingAggregation<TRecord, TResult>; avgDistinct: TypePreservingAggregation<TRecord, TResult>; increment( columnName: keyof TRecord, amount?: number, ): QueryBuilder<TRecord, number>; increment( columnName: string, amount?: number, ): QueryBuilder<TRecord, number>; increment(columns: { [column in keyof TRecord]: number; }): QueryBuilder<TRecord, number>; decrement( columnName: keyof TRecord, amount?: number, ): QueryBuilder<TRecord, number>; decrement( columnName: string, amount?: number, ): QueryBuilder<TRecord, number>; decrement(columns: { [column in keyof TRecord]: number; }): QueryBuilder<TRecord, number>; // Analytics rank: AnalyticFunction<TRecord, TResult>; denseRank: AnalyticFunction<TRecord, TResult>; rowNumber: AnalyticFunction<TRecord, TResult>; // Others first: Select< TRecord, DeferredKeySelection.AddUnionMember<UnwrapArrayMember<TResult>, undefined> >; pluck<K extends keyof TRecord>( column: K, ): QueryBuilder<TRecord, TRecord[K][]>; pluck<TResult2 extends {}>(column: string): QueryBuilder<TRecord, TResult2>; insert( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "insert"> | ReadonlyArray<ResolveTableType<TRecord, "insert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: "*", options?: DMLOptions, ): QueryBuilder<TRecord, DeferredKeySelection<TRecord, never>[]>; insert< TKey extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "insert"> | ReadonlyArray<ResolveTableType<TRecord, "insert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: TKey, options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; insert< TKey extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "insert"> | ReadonlyArray<ResolveTableType<TRecord, "insert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: readonly TKey[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; insert< TKey extends string, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, TRecord, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "insert"> | ReadonlyArray<ResolveTableType<TRecord, "insert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: TKey, options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; insert< TKey extends string, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, TRecord, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "insert"> | ReadonlyArray<ResolveTableType<TRecord, "insert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: readonly TKey[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; insert<TResult2 = number[]>( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "insert"> | ReadonlyArray<ResolveTableType<TRecord, "insert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, ): QueryBuilder<TRecord, TResult2>; upsert( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "upsert"> | ReadonlyArray<ResolveTableType<TRecord, "upsert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: "*", options?: DMLOptions, ): QueryBuilder<TRecord, DeferredKeySelection<TRecord, never>[]>; upsert< TKey extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "upsert"> | ReadonlyArray<ResolveTableType<TRecord, "upsert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: TKey, options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; upsert< TKey extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "upsert"> | ReadonlyArray<ResolveTableType<TRecord, "upsert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: readonly TKey[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; upsert< TKey extends string, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, TRecord, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "upsert"> | ReadonlyArray<ResolveTableType<TRecord, "upsert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: TKey, options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; upsert< TKey extends string, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, TRecord, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "upsert"> | ReadonlyArray<ResolveTableType<TRecord, "upsert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, returning: readonly TKey[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; upsert<TResult2 = number[]>( data: TRecord extends CompositeTableType<unknown> ? | ResolveTableType<TRecord, "upsert"> | ReadonlyArray<ResolveTableType<TRecord, "upsert">> : DbRecordArr<TRecord> | ReadonlyArray<DbRecordArr<TRecord>>, ): QueryBuilder<TRecord, TResult2>; modify<TRecord2 extends {} = any, TResult2 extends {} = any>( callback: QueryCallbackWithArgs<TRecord, any>, ...args: any[] ): QueryBuilder<TRecord2, TResult2>; update< K1 extends StrKey<ResolveTableType<TRecord, "update">>, K2 extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, K2 >[], >( columnName: K1, value: DbColumn<ResolveTableType<TRecord, "update">[K1]>, returning: K2, options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; update< K1 extends StrKey<ResolveTableType<TRecord, "update">>, K2 extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, K2 >[], >( columnName: K1, value: DbColumn<ResolveTableType<TRecord, "update">[K1]>, returning: readonly K2[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; update<K extends keyof TRecord>( columnName: K, value: DbColumn<TRecord[K]>, ): QueryBuilder<TRecord, number>; update<TResult2 = SafePartial<TRecord>[]>( columnName: string, value: Value, returning: string | readonly string[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; update( data: DbRecordArr<TRecord>, returning: "*", options?: DMLOptions, ): QueryBuilder<TRecord, DeferredKeySelection<TRecord, never>[]>; update< TKey extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? ResolveTableType<TRecord, "update"> : DbRecordArr<TRecord>, returning: TKey, options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; update< TKey extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, ResolveTableType<TRecord>, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? ResolveTableType<TRecord, "update"> : DbRecordArr<TRecord>, returning: readonly TKey[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; update< TKey extends string = string, TResult2 extends {}[] = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, TRecord, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? ResolveTableType<TRecord, "update"> : DbRecordArr<TRecord>, returning: TKey | readonly TKey[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; update< TKey extends string, TResult2 extends {}[] = DeferredKeySelection.Augment< UnwrapArrayMember<TResult>, TRecord, TKey >[], >( data: TRecord extends CompositeTableType<unknown> ? ResolveTableType<TRecord, "update"> : DbRecordArr<TRecord>, returning: readonly TKey[], options?: DMLOptions, ): QueryBuilder<TRecord, TResult2>; update<TResult2 = number>( data: TRecord extends CompositeTableType<unknown> ? ResolveTableType<TRecord, "update"> : DbRecordArr<TRecord>, ): QueryBuilder<TRecord, TResult2>; update<TResult2 = number>( columnName: string, value: Value, ): QueryBuilder<TRecord, TResult2>; returning( column: "*", options?: DMLOptions, ): QueryBuilder<TRecord, DeferredKeySelection<TRecord, never>[]>; returning< TKey extends StrKey<ResolveTableType<TRecord>>, TResult2 = DeferredKeySelection.Augment<