prorm API Reference
    Preparing search index...

    Class SqlCompiler

    SQL Compiler class Compiles query objects to SQL strings with proper escaping

    Index
    • Get the DB column name for an attribute

      Parameters

      • attributeName: string

      Returns string

    • Get the attribute name for a DB column

      Parameters

      • columnName: string

      Returns string

    • Escape an identifier (column, table name)

      Parameters

      • identifier: unknown

      Returns string

    • Convert an attribute expression to SQL Handles fn(), col(), literal(), and plain strings

      Parameters

      • attr: any

        The attribute value (string, FnExpression, ColExpression, LiteralExpression, or array)

      • OptionaltableAlias: string

        Optional table alias to prepend to column names

      Returns string

      The SQL representation of the attribute

    • Process an attribute value (which could be a column name, fn expression, col expression, or literal) into a SQL expression string

      Parameters

      • attr: any

        The attribute value to process

      • OptionaltableAlias: string

        Optional table alias to prefix column names with

      Returns string

      The SQL expression string

    • Convert an attribute to SQL with optional alias Handles formats like:

      • 'columnName' -> "columnName"
      • ['columnName', 'alias'] -> "columnName" AS "alias"
      • [fn('COUNT', col('id')), 'count'] -> COUNT("id") AS "count"
      • ['COUNT()', 'count'] -> COUNT() AS "count"

      Parameters

      • attr: any

        The attribute (string or array with alias)

      • OptionaltableAlias: string

        Optional table alias

      Returns string

      SQL fragment with optional alias

    • Compile WHERE clause

      Parameters

      • where: WhereOptions
      • Optionaloptions: { paramChar?: string }

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

    • Compile LIMIT/OFFSET clause Handles SQLite which requires LIMIT when using OFFSET

      Parameters

      • Optionallimit: string | number
      • Optionaloffset: string | number

      Returns string

    • Compile GROUP BY clause Supports:

      • Plain strings: 'role', 'userId'
      • Arrays of strings: ['role', 'status']
      • col() expressions: col('role'), col('table.column')
      • fn() expressions: fn('UPPER', col('name'))
      • literal expressions: literal('YEAR(created_at)')
      • Array of mixed expressions

      Parameters

      • Optionalgroup: string | string[]
      • OptionalgroupType: "rollup" | "cube" | "grouping" | "none"
      • OptionalgroupingSets: string[][]

      Returns string

      group: 'role'
      group: ['role', 'status']
      group: col('role')
      group: [col('role'), fn('COUNT', col('id'))]
    • Compile Common Table Expression (CTE) clause Supports both regular WITH and WITH RECURSIVE

      Parameters

      • Optionalctes: { name: string; columns?: string[]; query: string; recursive?: boolean }[]

      Returns string

    • Compile HAVING clause for aggregation filtering Similar to WHERE but specifically for aggregated results Supports all operators: Op.gt, Op.eq, Op.between, etc. Also supports direct aggregation functions: COUNT(), SUM(), AVG(), MAX(), MIN()

      Parameters

      • having: any
      • Optionaloptions: { paramChar?: string }

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

    • Compile a full SELECT query

      Parameters

      • options: {
            tableName: string;
            attributes?: string[] | { include?: string[]; exclude?: string[] };
            where?: WhereOptions;
            include?: IncludeOptions[];
            order?: Order;
            limit?: string | number;
            offset?: string | number;
            group?: string | string[];
            groupType?: "rollup" | "cube" | "grouping" | "none";
            groupingSets?: string[][];
            having?: WhereOptions;
            cte?: {
                name: string;
                columns?: string[];
                query: string;
                recursive?: boolean;
            }[];
            pivot?: PivotOptions;
            unpivot?: UnpivotOptions;
            pivotValues?: string[];
        }

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

    • Compile a PIVOT query - transform rows to columns

      Parameters

      • options: {
            tableName: string;
            pivot: PivotOptions;
            groupBy: string | string[];
            where?: WhereOptions;
            order?: Order;
            limit?: string | number;
            offset?: string | number;
        }

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

    • Compile a PIVOT query with specific pivot values (after they're resolved)

      Parameters

      • options: {
            tableName: string;
            pivot: PivotOptions;
            pivotValues: string[];
            groupBy: string | string[];
            where?: WhereOptions;
            order?: Order;
            limit?: string | number;
            offset?: string | number;
        }

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

    • Compile an UNPIVOT query - transform columns to rows

      Parameters

      • options: {
            tableName: string;
            unpivot: UnpivotOptions;
            where?: WhereOptions;
            order?: Order;
            limit?: string | number;
            offset?: string | number;
        }

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

    • Compile an INSERT query

      Parameters

      • options: {
            tableName: string;
            values: Record<string, any>;
            returning?: boolean | string[];
        }

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

    • Compile an UPDATE query

      Parameters

      • options: {
            tableName: string;
            values: Record<string, any>;
            where: WhereOptions;
            limit?: number;
            returning?: boolean | string[];
        }

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

    • Compile a DELETE query

      Parameters

      • options: {
            tableName: string;
            where: WhereOptions;
            limit?: number;
            returning?: boolean | string[];
        }

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

    • Compile a UNION/EXCEPT/INTERSECT query Supports combining multiple queries with UNION, UNION ALL, EXCEPT, or INTERSECT

      Parameters

      • options: {
            main: {
                tableName: string;
                attributes?: string[] | { include?: string[]; exclude?: string[] };
                where?: WhereOptions;
                include?: IncludeOptions[];
                order?: Order;
                limit?: string | number;
                offset?: string | number;
            };
            union: {
                type: "UNION"
                | "UNION ALL"
                | "EXCEPT"
                | "INTERSECT";
                model?: ModelStatic<any>;
                tableName?: string;
                where?: WhereOptions;
                attributes?: string[] | { include?: string[]; exclude?: string[] };
                order?: Order;
                limit?: string | number;
                offset?: string | number;
                include?: IncludeOptions[];
            }[];
            order?: Order;
            limit?: string
            | number;
            offset?: string | number;
        }
        • main: {
              tableName: string;
              attributes?: string[] | { include?: string[]; exclude?: string[] };
              where?: WhereOptions;
              include?: IncludeOptions[];
              order?: Order;
              limit?: string | number;
              offset?: string | number;
          }

          The main query (uses same structure as compileSelect)

        • union: {
              type: "UNION" | "UNION ALL" | "EXCEPT" | "INTERSECT";
              model?: ModelStatic<any>;
              tableName?: string;
              where?: WhereOptions;
              attributes?: string[] | { include?: string[]; exclude?: string[] };
              order?: Order;
              limit?: string | number;
              offset?: string | number;
              include?: IncludeOptions[];
          }[]

          Array of union queries

        • Optionalorder?: Order

          Order by for the combined result

        • Optionallimit?: string | number

          Limit for the combined result

        • Optionaloffset?: string | number

          Offset for the combined result

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

    • Compile an EXCEPT query - returns rows from main query that don't exist in except query

      Parameters

      • mainQuery: {
            tableName: string;
            attributes?: string[] | { include?: string[]; exclude?: string[] };
            where?: WhereOptions;
            include?: IncludeOptions[];
        }
      • exceptQuery: {
            tableName: string;
            attributes?: string[] | { include?: string[]; exclude?: string[] };
            where?: WhereOptions;
            include?: IncludeOptions[];
        }
      • Optionaloptions: { order?: Order; limit?: string | number; offset?: string | number }

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

    • Compile an INTERSECT query - returns rows that exist in both queries

      Parameters

      • mainQuery: {
            tableName: string;
            attributes?: string[] | { include?: string[]; exclude?: string[] };
            where?: WhereOptions;
            include?: IncludeOptions[];
        }
      • intersectQuery: {
            tableName: string;
            attributes?: string[] | { include?: string[]; exclude?: string[] };
            where?: WhereOptions;
            include?: IncludeOptions[];
        }
      • Optionaloptions: { order?: Order; limit?: string | number; offset?: string | number }

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

    • Format SQL for different dialects

      Parameters

      • sql: string
      • dialect: string

      Returns string

    • Compile PARTITION BY clause

      Parameters

      • partitionBy: string | string[] | undefined

      Returns string

    • Compile window frame specification Examples:

      • ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
      • RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
      • GROUPS 2 PRECEDING AND CURRENT ROW

      Parameters

      Returns string

    • Compile lock options for row-level locking.

      Delegates to the shared dialect lock compiler so this and each dialect's own buildSelectQuery() can never disagree about what a given lock shape means. Supports true, a level string, and the { level, nowait, skipLocked, of } object form; throws when the named dialect cannot express the requested lock.

      Parameters

      • lock: any

        Lock options

      • dialect: string = 'postgres'

        The database dialect

      Returns string

      The compiled lock SQL clause, or an empty string for no lock and for dialects with no trailing locking clause (SQLite, SQL Server, ...)

    • Compile a window function expression

      Parameters

      • functionName: string

        The window function name (ROW_NUMBER, RANK, etc.)

      • args: any[] = []

        Arguments to the window function

      • options: WindowFunctionOptions = {}

        Window function options

      Returns string

      The compiled SQL expression