Skip to content

Commit

Permalink
Add support for dynamic columns with returning - fixes #317
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Apr 14, 2022
1 parent 6a631b7 commit 822fb21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,28 @@ function values(first, rest, parameters, types, transform) {
return valuesBuilder(multi ? first : [first], parameters, types, transform, columns)
}

function select(first, rest, parameters, types, transform) {
typeof first === 'string' && (first = [first].concat(rest))
if (Array.isArray(first))
return first.map(x => escapeIdentifier(transform.column.to ? transform.column.to(x) : x)).join(',')

let value
const columns = rest.length ? rest.flat() : Object.keys(first)
return columns.map(x => {
value = first[x]
return (
value instanceof Query ? value.strings[0] :
value instanceof Identifier ? value.value :
handleValue(value, parameters, types)
) + ' as ' + escapeIdentifier(transform.column.to ? transform.column.to(x) : x)
}).join(',')
}

const builders = Object.entries({
values,
in: values,
select,
returning: select,

update(first, rest, parameters, types, transform) {
return (rest.length ? rest.flat() : Object.keys(first)).map(x =>
Expand All @@ -121,23 +140,6 @@ const builders = Object.entries({
)
},

select(first, rest, parameters, types, transform) {
typeof first === 'string' && (first = [first].concat(rest))
if (Array.isArray(first))
return first.map(x => escapeIdentifier(transform.column.to ? transform.column.to(x) : x)).join(',')

let value
const columns = rest.length ? rest.flat() : Object.keys(first)
return columns.map(x => {
value = first[x]
return (
value instanceof Query ? value.strings[0] :
value instanceof Identifier ? value.value :
handleValue(value, parameters, types)
) + ' as ' + escapeIdentifier(transform.column.to ? transform.column.to(x) : x)
}).join(',')
},

insert(first, rest, parameters, types, transform) {
const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)
return '(' + columns.map(x =>
Expand Down
9 changes: 9 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,15 @@ t('dynamic select array', async() => {
return ['yay', (await sql`select ${ sql(['a', 'b']) } from test`)[0].b, await sql`drop table test`]
})

t('dynamic returning array', async() => {
await sql`create table test (a int, b text)`
return [
'yay',
(await sql`insert into test (a, b) values (42, 'yay') returning ${ sql(['a', 'b']) }`)[0].b,
await sql`drop table test`
]
})

t('dynamic select args', async() => {
await sql`create table test (a int, b text)`
await sql`insert into test (a, b) values (42, 'yay')`
Expand Down

0 comments on commit 822fb21

Please sign in to comment.