All files / src inline-table-of-objects.ts

94.11% Statements 16/17
100% Branches 2/2
100% Functions 3/3
92.85% Lines 13/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2822x 22x 22x 22x 22x     22x               22x 15x 15x 15x 3073x 15x           22x  
import auto from './auto.js'
import id from './identifier.js'
import keysOfObjects from './keys-of-objects.js'
import list from './list.js'
import row from './row.js'
import type S from './sanitised.js'
import type Sid from './sanitised-identifier.js'
import tsql from './template.js'
 
/**
 * @returns literal table from provided array of records.
 *
 * @throws {Error} if provided values array is empty.
 */
export const inlineTableOfObjects =
  (table: Sid | string, objects: readonly Record<string, unknown>[], maybeKeys?: string[]): S => {
    const table_ = id(table)
    const keys = maybeKeys || keysOfObjects(objects)
    const columns_ = row(keys.map(id))
    const values_ = list(objects.map(object => row(keys.map(key => auto(object[key])))), undefined, ',\n')
    return tsql`
      (values
        ${values_}
      ) as ${table_} ${columns_}`
  }
 
export default inlineTableOfObjects