Skip to content

Commit c44d0ae

Browse files
committed
Improve transform handling
1 parent f048b0f commit c44d0ae

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

.eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"node": true,
55
"es6": true
66
},
7+
"parser": "babel-eslint",
78
"parserOptions": {
89
"ecmaVersion": 9,
910
"sourceType": "module"

lib/backend.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ function Backend({
192192
start = index
193193
while (x[index++] !== 0);
194194
backend.query.statement.columns[i] = {
195-
n: transform(x.utf8Slice(start, index - 1)),
195+
n: transform === null
196+
? x.utf8Slice(start, index - 1)
197+
: transform(x.utf8Slice(start, index - 1)),
196198
p: parsers[x.readInt32BE(index + 6)]
197199
}
198200
index += 18

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default function Postgres(url, options) {
4343
}
4444

4545
Object.assign(postgres, {
46+
options,
4647
parameters: {},
4748
listen,
4849
begin,
@@ -442,7 +443,7 @@ function parseOptions(uri = {}, options) {
442443
pass : o.pass || o.password || auth[1] || env.PGPASSWORD || '',
443444
max : o.max || url.query.max || Math.max(1, os.cpus().length),
444445
fifo : o.fifo || url.query.fifo || false,
445-
transform : o.transform || (x => x),
446+
transform : typeof o.transform === 'function' ? o.transform : null,
446447
types : o.types || {},
447448
ssl : o.ssl || url.ssl || false,
448449
timeout : o.timeout,

tests/index.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { fileURLToPath } from 'url'
12
import { t, ot, not } from './test.js'
23
import cp from 'child_process'
4+
import path from 'path'
35

46
import postgres from '../lib/index.js'
57

8+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
9+
610
const login = {
711
user: 'postgres_js_test'
812
}
@@ -248,18 +252,12 @@ t('Point type array', async() => {
248252
}, () => sql`drop table test`)
249253

250254
t('sql file', async() =>
251-
[1, (await sql.file('./tests/select.sql'))[0].x]
255+
[1, (await sql.file(path.join(__dirname, 'select.sql')))[0].x]
252256
)
253-
/*
254-
t('select column vars', async() => {
255-
await sql`create table test (x int)`
256-
await sql`insert into test values (1)`
257-
return [1, (await sql`select ${ 'x' } from test`)[0].x]
258-
})
259-
*/
257+
260258
t('sql file can stream', async() => {
261259
let result
262-
await sql.file('./tests/select.sql')
260+
await sql.file(path.join(__dirname, 'select.sql'))
263261
.stream(({ x }) => result = x)
264262

265263
return [1, result]
@@ -424,3 +422,27 @@ t('big query body', async() => {
424422
}`).count]
425423
}, () => sql`drop table test`)
426424

425+
426+
/*
427+
428+
429+
t('select column vars', async() => {
430+
await sql`create table test (x int)`
431+
await sql`insert into test values (1)`
432+
return [1, (await sql`select ${ 'x' } from test`)[0].x]
433+
})
434+
435+
t('select column vars', async() => {
436+
await sql`create table test (x int)`
437+
await sql`insert into test values (1)`
438+
return [1, (await sql`select ${ 'x' } from test`)[0].x]
439+
})
440+
441+
t('select column vars', async() => {
442+
await sql`create table test (x int)`
443+
await sql`insert into test values (1)`
444+
return [1, (await sql`select ${ 'x' } from test`)[0].x]
445+
})
446+
447+
448+
*/

0 commit comments

Comments
 (0)