ORM
  • Introduction
  • Basics of Usage
    • Connecting to the Database
    • Creating a Model
    • Simple Queries
  • Configuration
  • Core Features
    • Models
    • CRUD Operations (Create, Read, Update, Delete): examples
    • Relationships
    • Transactions
  • Advanced Usage
    • Data Definition Language
    • Indexes
    • Triggers
    • Caching
  • Examples and templates
  • Security
    • Query parameterization and protection against SQL injections
    • Logging
    • Monitoring
    • Sanitization
  • ORM-CLI
    • Installing
    • Commands and usage
  • System architecture and implementation
    • Support of various databases
      • Implementation using a pattern strategy
      • Implementation using generics
      • Implementation using metaprogramming
    • Decorators
    • Migrations
      • Assignment of id in Tables and Columns
      • Assigning IDs to indexes and triggers
      • Working with migrations in ORM-CLI
        • Implementation of a pattern command for migration
    • QueryBuilder
    • Caching
    • File structure
  • API
    • Decorators
      • Column
      • ComputedColumn
      • Index
      • ForeignKey
      • PrimaryGeneratedColumn
      • Relations
      • Table
      • Trigger
      • Types decorators
        • Integer
        • Numeric
        • Float
        • Boolean
        • String
    • DDL
    • QueryBuilder
    • Additional API
Powered by GitBook
On this page
  1. API

QueryBuilder

QueryBuilderInterface<T>

Methods:

  1. createView(name: string): QueryBuilderInterface<T> name: The name of the virtual table or view. Returns: QueryBuilderInterface<T>. Allows continuing the chain of calls to build the query.

  2. select(columns?: string[]): QueryBuilderInterface<T> columns (optional): An array of column names to select. If not specified, all columns are selected. Returns: QueryBuilderInterface<T>. Allows adding columns to the selection.

  3. into(name: string): QueryBuilderInterface<T> name: The name of the table into which the insertion will occur. Returns: QueryBuilderInterface<T>. Allows specifying the table for insertion.

  4. sum(column: string): QueryBuilderInterface<T> column: The name of the column to sum. Returns: QueryBuilderInterface<T>. Adds a sum function.

  5. count(column: string): QueryBuilderInterface<T> column: The name of the column to count. Returns: QueryBuilderInterface<T>. Adds a count function.

  6. having(condition: ConditionParamsType<T>): QueryBuilderInterface<T> condition: Conditions for filtering the results of aggregate functions. Returns: QueryBuilderInterface<T>. Adds conditions for filtering results.

  7. as(alias: string): QueryBuilderInterface<T> alias: Alias for the table or column. Returns: QueryBuilderInterface<T>. Adds an alias to the table or column.

  8. groupBy(columns: string[]): QueryBuilderInterface<T> columns: An array of columns to group the results by. Returns: QueryBuilderInterface<T>. Adds a grouping condition to the query.

  9. from(table: string): QueryBuilderInterface<T> table: The name of the table for the main query. Returns: QueryBuilderInterface<T>. Adds the table from which data will be retrieved.

  10. where(params: ConditionParamsType<T>): QueryBuilderInterface<T> params: Conditions for filtering the query results. Returns: QueryBuilderInterface<T>. Adds conditions for filtering.

  11. leftJoin(table: string, condition: JoinCondition): QueryBuilderInterface<T> table: The name of the table to join. condition: Join conditions as a JoinCondition object. Returns: QueryBuilderInterface<T>. Adds a left join to the query.

  12. rightJoin(table: string, condition: JoinCondition): QueryBuilderInterface<T> table: The name of the table to join. condition: Join conditions as a JoinCondition object. Returns: QueryBuilderInterface<T>. Adds a right join to the query.

  13. innerJoin(table: string, condition: JoinCondition): QueryBuilderInterface<T> table: The name of the table to join. condition: Join conditions as a JoinCondition object. Returns: QueryBuilderInterface<T>. Adds an inner join to the query.

  14. union(queryBuilder: QueryBuilderInterface<T>): QueryBuilderInterface<T> queryBuilder: Another QueryBuilderInterface<T> whose results are to be united with the current query. Returns: QueryBuilderInterface<T>. Adds a union to the query.

  15. unionAll(queryBuilder: QueryBuilderInterface<T>): QueryBuilderInterface<T> queryBuilder: Another QueryBuilderInterface<T> whose results are to be united with the current query, including duplicates. Returns: QueryBuilderInterface<T>. Adds a union of all results to the query.

  16. orderBy(column: string, order?: string): QueryBuilderInterface<T> column: The name of the column to sort by. order (optional): The direction of sorting ('asc' or 'desc'). Returns: QueryBuilderInterface<T>. Adds a sorting condition to the query.

  17. limit(count: number): QueryBuilderInterface<T> count: The number of records to select. Returns: QueryBuilderInterface<T>. Adds a limit on the number of results.

  18. insert(values: Partial<T>, tableName: string): QueryBuilderInterface<T> values: An object with data to insert into the table. tableName: The name of the table for inserting data. Returns: QueryBuilderInterface<T>. Adds an insertion condition.

  19. insertMany(values: Partial<T>[], tableName: string): QueryBuilderInterface<T> values: An array of objects with data to insert into the table. tableName: The name of the table for inserting data. Returns: QueryBuilderInterface<T>. Adds an insertion condition for multiple records.

  20. update(values: Partial<T>, tableName: string): QueryBuilderInterface<T> values: An object with data to update in the table. tableName: The name of the table for updating data. Returns: QueryBuilderInterface<T>. Adds an update condition.

  21. delete(tableName: string): QueryBuilderInterface<T> tableName: The name of the table for deleting data. Returns: QueryBuilderInterface<T>. Adds a deletion condition.

  22. cache(options: CacheOptionsInterface, enableMonitoring?: boolean): Promise<T> options: An object implementing the CacheOptionsInterface for cache configuration. key (optional): Cache key. ttl (optional): Cache time-to-live in seconds. enableMonitoring (optional): Boolean value indicating the need for cache monitoring. Returns: Promise<T>. Promises to return query results with caching.

  23. build(isParametrized?: boolean, isSemicolon?: boolean, useCounter?: boolean): string isParametrized (optional): Boolean value indicating whether to parameterize the query (boolean). Determines the format of query parameters ($1, $2, etc. for PostgreSQL or ? for other DBMS). isSemicolon (optional): Boolean value indicating whether to add a semicolon at the end of the query (boolean). useCounter (optional): Boolean value indicating whether to use a counter for query parameters. Returns: string. The constructed SQL query as a string.

  24. parametrize(parameters: any[]): QueryBuilderInterface<T> parameters: An array of parameters to substitute into the query. Returns: QueryBuilderInterface<T>. Adds parameters to the query.

  25. buildWithoutSemicolon(): string Returns: string. The constructed SQL query as a string without a semicolon at the end.

  26. execute(enableMonitoring?: boolean): Promise<T> enableMonitoring (optional): Boolean value indicating the need for execution monitoring. Returns: Promise<T>. Promises to return the results of query execution.

Types and Interfaces:

  • ConditionParamsType<T> conditions: Filtering conditions defined as Condition<T>. logicalOperator (optional): Logical operator for combining conditions ('and' or 'or'). exists (optional): Column name for existence check.

  • Condition<T> [K in keyof ElementType<T>]: Conditions for each column with possible comparison operators.

  • JoinCondition column: The name of the column for joining. operator: Join operator. value: Value or array of values for comparison.

  • CacheOptionsInterface key (optional): Cache key. ttl (optional): Cache time-to-live in seconds.

PreviousDDLNextAdditional API

Last updated 9 months ago