ChatiumFor developersPlaygroundPricing
Sign in

HeapTableRepo.select

Creates a constructor for a complex query HeapSelectQueryBuilder to the heap table, specifying a set of fields/expressions that we want to see in the result of executing this query.

Usage / Signature

const query = table.select(...fieldNames)
const query = table.select(aliasesObject)

Arguments

  • fieldNames Array<keyof HsProperties>
    A simple list of field names from the original heap table or saved query (HeapView) that we want to see in the result of the query. This format can only be used if you need to request fields "as is" without changing their names. The list of names is specified as function arguments, not as an array in the first argument.

  • aliasesObject: Record<string, HqExpr>
    An object where the keys are the names (aliases) of the fields in the final result of the query, and the values are expressions that define what will be requested as the value of that field.

    Expressions (HqExpr) can be one of several types:

    • HqExprColumn: string (keyof HsProperties)
      A simple name of one of the fields from the original table or view (subquery). Example: { aliasName: 'realFieldName' }.

    • HqExprPath: Array<string | number>
      A path to a nested complex field represented as an array with a chain of nested field names or array indices that form the path from the root field of the table to the final deep field that needs to be output in the result. Example: { deepField: ['deep', 'array', 0, 'name'] }.

    • HqExprFn: { [$fnName: string]: HqExpr[] }
      A regular function represented as an object with a single key (always starting with $) - the name of the function, and the value of that key - an array of function arguments, which are also HqExpr expressions. Example: { upperCaseName: { $upper: ['firstName'] } }.

    • HqExprAgg: { [$aggFnName: string]: HqExpr[], $distinct?: boolean }
      An aggregate function represented as an object with a key (always starting with $) - the name of the function, and the value of that key - an array of function arguments, which are also HqExpr expressions. It may have an additional field $distinct, which allows counting aggregates only for unique values (SELECT COUNT(DISTINCT name)). Examples: { cnt: { $count: ['*'] } } or { uniqCnt: { $count: [['deep', 'field']], $distinct: true }.

    • HqExprDyn: { $dyn: number | string | boolean | Date }
      A dynamic value (variable) that is substituted into the query as a parameter (with protection against SQL injection). Example: { constantValue: { $dyn: 15 }}.

Return Value: HeapSelectQueryBuilder

An instance of the query builder that can be "extended" using its own methods where, group, having, order, limit, offset, and others, and then executed using one of several runXXX methods.

Examples

Selecting all values of a single field from the table

const Tasks = Heap.Table('tasks', {
  title: Heap.String(),
  responsible: Heap.RefLink('employees'),
  priority: Heap.Integer(),
  client: Heap.Object({
    firstName: Heap.String(),
    lastName: Heap.String(),
    age: Heap.Integer(),
  })
})
const allTitles = await Tasks.select('title').runFlatten(ctx)
Selecting the name and age of a user from the structure
const clientsInfo: Array<{ name: string, age: number }> = await Tasks
  .select({
    name: {
      $concat: [
        ['client', 'firstName'],
        { $dyn: ' ' },
        ['client', 'lastName'],
      ]
    },
    age: ['client', 'age']
  })
  .where({ age: { $gt: 40 } })
  .order('age')
  .run(ctx)