Class QueryBuilder<TData, TVirtual, TInstance, TRelations>

Fluent query builder for the PostgreSQL DAL.

Produces parameterized SQL for SELECT/COUNT/DELETE with support for predicates, ordering, pagination, and both simple and complex joins. Instances are model-aware (leveraging table/column metadata) and implement Promise-like behavior so they can be awaited directly.

Type Parameters

Implements

Constructors

Properties

_complexJoins?: Record<string, ComplexJoinSpec>
_includeSensitive: string[]
_joins: string[]
_joinSpecs?: Record<string, unknown>[]
_limit: number
_offset: number
_orderBy: string[]
_paramIndex: number
_params: unknown[]
_select: string[]
_simpleJoins?: Record<string, RelationJoinInfo>
_where: Predicate[]
modelClass: QueryModel<TData, TVirtual, TInstance, TRelations>
tableName: string

Methods

  • Compute an aggregate for the active query.

    Parameters

    • func: "AVG" | "SUM" | "MIN" | "MAX"

      SQL aggregate function identifier (e.g., AVG).

    • field: string

      Resolved field/column name to aggregate.

    Returns Promise<number>

    Numeric aggregate result or null when no rows match.

  • Parameters

    • applyFn: (qb: unknown) => unknown

    Returns JoinApplyAnalysis

  • Parameters

    • field: string | symbol

    Returns string | symbol

  • Parameters

    • mainRows: Record<string, unknown>[]
    • relationName: string
    • joinInfo: RelationJoinInfo
    • groupedRows: Map<unknown, Record<string, unknown>[]>

    Returns void

  • Build an aggregate query (AVG, COUNT, etc.) for the current predicates.

    Parameters

    • func: string

      Aggregate function keyword.

    • column: string

      Fully qualified column expression.

    Returns { params: any[]; sql: string }

    SQL fragment plus bound params.

  • Parameters

    • relationName: any
    • joinInfo: any
    • sourceValues: any
    • analysis: any = null

    Returns { joinSourceAlias: string; params: unknown[]; query: string }

  • Parameters

    • __namedParameters: { qualifyColumns?: boolean } = {}

    Returns { params: any[]; sql: string }

  • Parameters

    • mainRows: Record<string, unknown>[]
    • sourceColumn: string

    Returns unknown[]

  • Parameters

    • rows: Record<string, unknown>[]
    • joinSourceAlias: string = '_join_source_id'

    Returns Map<unknown, Record<string, unknown>[]>

  • Parameters

    • predicate: any
    • context: any

    Returns string

  • Parameters

    • tableReference: any
    • column: any

    Returns any

  • Parameters

    • field: any

    Returns { column: any; table: any } | { column: string; table: string }

  • Calculate the average for a column.

    Parameters

    • field: string

      Column to aggregate.

    Returns Promise<number>

    Numeric average or null when no rows satisfy the predicates.

  • Filter by date range

    Parameters

    • startDate: string | number | Date

      Start date

    • endDate: string | number | Date

      End date

    • options: BetweenOptions = {}

      Options (leftBound, rightBound)

    Returns this

    This instance for chaining

  • Filter by array contains (for PostgreSQL arrays)

    Parameters

    • field: string

      Field name

    • value: unknown

      Value to check for

    Returns this

    This instance for chaining

  • Delete a record by ID

    Parameters

    • id: string

      Record ID

    Returns Promise<number>

    Delete result

  • Finally handler for Promise-like usage

    Parameters

    • OptionalonFinally: () => void

      Finally handler

    Returns Promise<TInstance[]>

    Promise resolving to query results

  • Filter by field existence in JSONB

    Parameters

    • field: string

      JSONB field name

    • key: string

      Key to check for existence

    Returns this

    This instance for chaining

  • Include sensitive fields in query results

    Parameters

    • fields: string | string[]

      Array of sensitive field names to include

    Returns this

    This instance for chaining

  • Atomically increment a numeric column on rows matching the current predicate.

    Type Parameters

    Parameters

    • field: string

      Model field (resolved to a DB column) to increment

    • amount: number = 1

      Increment step (default: 1)

    • options: { returning?: string[] } = {}

      Optional returning clause

    Returns Promise<IncrementResult<TRow>>

    Rows matched/returned plus affected row count.

  • Filter by membership in a list of values using PostgreSQL ANY

    Parameters

    • field: string

      Field name

    • values: unknown[]

      Values to match

    • Optionaloptions: { cast?: string } = {}

      Additional options

      • Optionalcast?: string

        Optional cast to apply to the parameter (e.g., 'uuid[]')

    Returns this

    This instance for chaining

  • Add a predicate against a column on a joined relation. This will ensure the relation is joined and apply a WHERE clause to the related table using camelCase field names.

    Parameters

    • relationName: string

      Relation key from the manifest

    • field: string

      CamelCase field on the related model

    • operator: string

      Comparison operator (defaults to '=')

    • value: unknown

      Comparison value

    Returns this