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 28 29 30 31 32 | 22x 22x 22x 22x 22x 22x 10x 10x 10x 10x 10x 23x 10x 22x | import auto from './auto.js'
import defined from './defined.js'
import id from './identifier.js'
import list from './list.js'
import tsql from './template.js'
import type S from './sanitised.js'
import type Sid from './sanitised-identifier.js'
/**
* @returns insert DML for single row.
*
* `undefined` entries are filtered out.
*
* @throws {TypeError} if there are no non-`undefined` entries.
*/
export const insertObject =
(table: Sid | string, object: Record<string, unknown>, output?: S): S => {
const object_ = defined(object)
const keys = Object.keys(object_)
Iif (keys.length === 0) {
throw new TypeError(`Expected object with keys, got ${object_}.`)
}
const table_ = id(table)
const keys_ = list(keys.map(id))
const values_ = list(keys.map(_ => object_[_]).map(auto))
return output ?
tsql`insert into ${table_} (${keys_}) output ${output} values (${values_})` :
tsql`insert into ${table_} (${keys_}) values (${values_})`
}
export default insertObject
|