All files / src select.ts

45% Statements 9/20
0% Branches 0/8
0% Functions 0/4
52.94% Lines 9/17

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       22x                         22x  
import fallback from './fallback.js'
import id from './identifier.js'
import line from './line.js'
import list from './list.js'
import S from './sanitised.js'
import type Sid from './sanitised-identifier.js'
import tsql from './template.js'
 
export const ids =
  (xs: (S | string)[]): S[] =>
    xs.map(_ => _ instanceof S ? _ : id(_))
 
export const select =
  (what: S | (S | string)[], { from, where }: { from?: Sid | string, where?: unknown } = {}): S => {
    const what_ = fallback(what, _ => list(ids(_)))
    Iif (typeof what_ === 'undefined') {
      throw new Error('Empty projection.')
    }
    const from_ = from != null ? id(from) : undefined
    return line(
      tsql`select ${what_}`,
      tsql`from ${from_}`,
      where != null ? tsql`where ${where}` : undefined
    )
  }
 
export default select