Skip to content

Commit a02de67

Browse files
authored
transform.column.to support (porsager#212)
1 parent ce37c80 commit a02de67

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

lib/backend.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ function Backend({
122122

123123
backend.query.raw
124124
? (row[i] = value)
125-
: (row[column.name] = transform.value ? transform.value(value) : value)
125+
: (row[column.name] = transform.value.from ? transform.value.from(value) : value)
126126
}
127127

128128
backend.query.stream
129-
? backend.query.stream(transform.row ? transform.row(row) : row, backend.query.result)
130-
: (backend.query.result[rows++] = transform.row ? transform.row(row) : row)
129+
? backend.query.stream(transform.row.from ? transform.row.from(row) : row, backend.query.result)
130+
: (backend.query.result[rows++] = transform.row.from ? transform.row.from(row) : row)
131131
}
132132

133133
/* c8 ignore next 3 */
@@ -208,8 +208,8 @@ function Backend({
208208
while (x[index++] !== 0);
209209
const type = x.readInt32BE(index + 6)
210210
backend.query.statement.columns[i] = {
211-
name: transform.column
212-
? transform.column(x.toString('utf8', start, index - 1))
211+
name: transform.column.from
212+
? transform.column.from(x.toString('utf8', start, index - 1))
213213
: x.toString('utf8', start, index - 1),
214214
parser: parsers[type],
215215
type

lib/index.js

+35-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ const {
88
mergeUserTypes,
99
arraySerializer,
1010
arrayParser,
11+
fromPascal,
12+
fromCamel,
13+
fromKebab,
1114
inferType,
1215
toPascal,
13-
entries,
1416
toCamel,
1517
toKebab,
18+
entries,
1619
escape,
1720
types,
1821
END
@@ -33,6 +36,9 @@ Object.assign(Postgres, {
3336
toPascal,
3437
toCamel,
3538
toKebab,
39+
fromPascal,
40+
fromCamel,
41+
fromKebab,
3642
BigInt: {
3743
to: 20,
3844
from: [20],
@@ -52,6 +58,7 @@ function Postgres(a, b) {
5258
const options = parseOptions(a, b)
5359

5460
const max = Math.max(1, options.max)
61+
, transform = options.transform
5562
, connections = Queue()
5663
, all = []
5764
, queries = Queue()
@@ -537,7 +544,9 @@ function Postgres(a, b) {
537544
function selectHelper(first, columns, xargs, types) {
538545
return entries(first).reduce((acc, [k, v]) =>
539546
acc + (!columns.length || columns.indexOf(k) > -1
540-
? (acc ? ',' : '') + parseValue(v, xargs, types) + ' as ' + escape(k)
547+
? (acc ? ',' : '') + parseValue(v, xargs, types) + ' as ' + escape(
548+
transform.column.to ? transform.column.to(k) : k
549+
)
541550
: ''
542551
),
543552
''
@@ -558,13 +567,17 @@ function Postgres(a, b) {
558567

559568
function equalsHelper(first, columns, xargs, types) {
560569
return (columns.length ? columns : Object.keys(first)).reduce((acc, k) =>
561-
acc + (acc ? ',' : '') + escape(k) + ' = ' + parseValue(first[k], xargs, types),
570+
acc + (acc ? ',' : '') + escape(
571+
transform.column.to ? transform.column.to(k) : k
572+
) + ' = ' + parseValue(first[k], xargs, types),
562573
''
563574
)
564575
}
565576

566577
function escapeHelper(xs) {
567-
return xs.reduce((acc, x) => acc + (acc ? ',' : '') + escape(x), '')
578+
return xs.reduce((acc, x) => acc + (acc ? ',' : '') + escape(
579+
transform.column.to ? transform.column.to(x) : x
580+
), '')
568581
}
569582

570583
function parseValue(x, xargs, types) {
@@ -628,7 +641,7 @@ function parseOptions(a, b) {
628641
prepare : 'prepare' in o ? o.prepare : 'no_prepare' in o ? !o.no_prepare : true,
629642
onnotice : o.onnotice,
630643
onparameter : o.onparameter,
631-
transform : Object.assign({}, o.transform),
644+
transform : parseTransform(o.transform || {}),
632645
connection : Object.assign({ application_name: 'postgres.js' }, o.connection),
633646
target_session_attrs: o.target_session_attrs || url.query.target_session_attrs || env.PGTARGETSESSIONATTRS,
634647
debug : o.debug,
@@ -638,6 +651,23 @@ function parseOptions(a, b) {
638651
)
639652
}
640653

654+
function parseTransform(x) {
655+
return {
656+
column: {
657+
from: typeof x.column === 'function' ? x.column : x.column && x.column.from,
658+
to: x.column && x.column.to
659+
},
660+
value: {
661+
from: typeof x.value === 'function' ? x.value : x.value && x.value.from,
662+
to: x.value && x.value.to
663+
},
664+
row: {
665+
from: typeof x.row === 'function' ? x.row : x.row && x.row.from,
666+
to: x.row && x.row.to
667+
}
668+
}
669+
}
670+
641671
function parseSSL(x) {
642672
return x !== 'disable' && x !== 'false' && x
643673
}

lib/types.js

+4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ module.exports.toPascal = x => {
172172

173173
module.exports.toKebab = x => x.replace(/_/g, '-')
174174

175+
module.exports.fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
176+
module.exports.fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
177+
module.exports.fromKebab = x => x.replace(/-/g, '_')
178+
175179
module.exports.errorFields = entries({
176180
S: 'severity_local',
177181
V: 'severity',

tests/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,18 @@ t('Transform value', async() => {
10361036
return [1, (await sql`select 'wat' as x`)[0].x]
10371037
})
10381038

1039+
t('Transform columns from', async() => {
1040+
const sql = postgres({ ...options, transform: { column: { to: postgres.fromCamel, from: postgres.toCamel } } })
1041+
await sql`create table test (a_test int, b_test text)`
1042+
await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }`
1043+
await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }`
1044+
return [
1045+
2,
1046+
(await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest,
1047+
await sql`drop table test`
1048+
]
1049+
})
1050+
10391051
t('Unix socket', async() => {
10401052
const sql = postgres({
10411053
...options,

0 commit comments

Comments
 (0)