From 00c618c2ca9c7a44738b656df8cfb016c9d22037 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Fri, 13 Aug 2021 16:09:29 +0200 Subject: [PATCH] transform.column.to support --- lib/backend.js | 10 +++++----- lib/index.js | 40 +++++++++++++++++++++++++++++++++++----- lib/types.js | 4 ++++ tests/index.js | 12 ++++++++++++ 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/lib/backend.js b/lib/backend.js index cfabb80a..c8c44ae5 100644 --- a/lib/backend.js +++ b/lib/backend.js @@ -122,12 +122,12 @@ function Backend({ backend.query.raw ? (row[i] = value) - : (row[column.name] = transform.value ? transform.value(value) : value) + : (row[column.name] = transform.value.from ? transform.value.from(value) : value) } backend.query.stream - ? backend.query.stream(transform.row ? transform.row(row) : row, backend.query.result) - : (backend.query.result[rows++] = transform.row ? transform.row(row) : row) + ? backend.query.stream(transform.row.from ? transform.row.from(row) : row, backend.query.result) + : (backend.query.result[rows++] = transform.row.from ? transform.row.from(row) : row) } /* c8 ignore next 3 */ @@ -208,8 +208,8 @@ function Backend({ while (x[index++] !== 0); const type = x.readInt32BE(index + 6) backend.query.statement.columns[i] = { - name: transform.column - ? transform.column(x.toString('utf8', start, index - 1)) + name: transform.column.from + ? transform.column.from(x.toString('utf8', start, index - 1)) : x.toString('utf8', start, index - 1), parser: parsers[type], type diff --git a/lib/index.js b/lib/index.js index 6edf49ca..1998ccd4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,11 +8,14 @@ const { mergeUserTypes, arraySerializer, arrayParser, + fromPascal, + fromCamel, + fromKebab, inferType, toPascal, - entries, toCamel, toKebab, + entries, escape, types, END @@ -33,6 +36,9 @@ Object.assign(Postgres, { toPascal, toCamel, toKebab, + fromPascal, + fromCamel, + fromKebab, BigInt: { to: 20, from: [20], @@ -52,6 +58,7 @@ function Postgres(a, b) { const options = parseOptions(a, b) const max = Math.max(1, options.max) + , transform = options.transform , connections = Queue() , all = [] , queries = Queue() @@ -532,7 +539,9 @@ function Postgres(a, b) { function selectHelper(first, columns, xargs, types) { return entries(first).reduce((acc, [k, v]) => acc + (!columns.length || columns.indexOf(k) > -1 - ? (acc ? ',' : '') + parseValue(v, xargs, types) + ' as ' + escape(k) + ? (acc ? ',' : '') + parseValue(v, xargs, types) + ' as ' + escape( + transform.column.to ? transform.column.to(k) : k + ) : '' ), '' @@ -553,13 +562,17 @@ function Postgres(a, b) { function equalsHelper(first, columns, xargs, types) { return (columns.length ? columns : Object.keys(first)).reduce((acc, k) => - acc + (acc ? ',' : '') + escape(k) + ' = ' + parseValue(first[k], xargs, types), + acc + (acc ? ',' : '') + escape( + transform.column.to ? transform.column.to(k) : k + ) + ' = ' + parseValue(first[k], xargs, types), '' ) } function escapeHelper(xs) { - return xs.reduce((acc, x) => acc + (acc ? ',' : '') + escape(x), '') + return xs.reduce((acc, x) => acc + (acc ? ',' : '') + escape( + transform.column.to ? transform.column.to(x) : x + ), '') } function parseValue(x, xargs, types) { @@ -623,7 +636,7 @@ function parseOptions(a, b) { prepare : 'prepare' in o ? o.prepare : 'no_prepare' in o ? !o.no_prepare : true, onnotice : o.onnotice, onparameter : o.onparameter, - transform : Object.assign({}, o.transform), + transform : parseTransform(o.transform || {}), connection : Object.assign({ application_name: 'postgres.js' }, o.connection), target_session_attrs: o.target_session_attrs || url.query.target_session_attrs || env.PGTARGETSESSIONATTRS, debug : o.debug, @@ -633,6 +646,23 @@ function parseOptions(a, b) { ) } +function parseTransform(x) { + return { + column: { + from: typeof x.column === 'function' ? x.column : x.column && x.column.from, + to: x.column && x.column.to + }, + value: { + from: typeof x.value === 'function' ? x.value : x.value && x.value.from, + to: x.value && x.value.to + }, + row: { + from: typeof x.row === 'function' ? x.row : x.row && x.row.from, + to: x.row && x.row.to + } + } +} + function parseSSL(x) { return x !== 'disable' && x !== 'false' && x } diff --git a/lib/types.js b/lib/types.js index b372e70b..644dd22b 100644 --- a/lib/types.js +++ b/lib/types.js @@ -172,6 +172,10 @@ module.exports.toPascal = x => { module.exports.toKebab = x => x.replace(/_/g, '-') +module.exports.fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase() +module.exports.fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase() +module.exports.fromKebab = x => x.replace(/-/g, '_') + module.exports.errorFields = entries({ S: 'severity_local', V: 'severity', diff --git a/tests/index.js b/tests/index.js index 58905432..51020cdd 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1037,6 +1037,18 @@ t('Transform value', async() => { return [1, (await sql`select 'wat' as x`)[0].x] }) +t('Transform columns from', async() => { + const sql = postgres({ ...options, transform: { column: { to: postgres.fromCamel, from: postgres.toCamel } } }) + await sql`create table test (a_test int, b_test text)` + await sql`insert into test ${ sql([{ aTest: 1, bTest: 1 }]) }` + await sql`update test set ${ sql({ aTest: 2, bTest: 2 }) }` + return [ + 2, + (await sql`select ${ sql('aTest', 'bTest') } from test`)[0].aTest, + await sql`drop table test` + ] +}) + t('Unix socket', async() => { const sql = postgres({ ...options,