Skip to content

Commit 56873c2

Browse files
committed
Add .values() method to return rows as arrays of values
1 parent eab71e5 commit 56873c2

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,20 @@ await sql`
401401
Rather than executing a given query, `.describe` will return information utilized in the query process. This information can include the query identifier, column types, etc.
402402
403403
This is useful for debugging and analyzing your Postgres queries. Furthermore, **`.describe` will give you access to the final generated query string that would be executed.**
404+
405+
### Rows as Array of Values
406+
#### ```sql``.values()```
404407
405-
### Raw
408+
Using `.values` will return rows as an array of values for each column, instead of objects.
409+
410+
This can be useful to receive identically named columns, or for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
411+
412+
### Rows as Raw Array of Buffers
406413
#### ```sql``.raw()```
407414
408-
Using `.raw()` will return rows as an array with `Buffer` values for each column, instead of objects.
415+
Using `.raw` will return rows as an array with `Buffer` values for each column, instead of objects.
409416
410-
This can be useful to receive identically named columns, or for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
417+
This can be useful for specific performance/transformation reasons. The column definitions are still included on the result array, plus access to parsers for each column.
411418
412419
### File
413420
#### `await sql.file(path, [args], [options]) -> Result[]`

src/connection.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
478478

479479
value = length === -1
480480
? null
481-
: query.isRaw
481+
: query.isRaw === true
482482
? x.slice(index, index += length)
483483
: column.parser === undefined
484484
? x.toString('utf8', index, index += length)
@@ -487,7 +487,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
487487
: column.parser(x.toString('utf8', index, index += length))
488488

489489
query.isRaw
490-
? (row[i] = value)
490+
? (row[i] = query.isRaw === true
491+
? value
492+
: transform.value.from ? transform.value.from(value) : value)
491493
: (row[column.name] = transform.value.from ? transform.value.from(value) : value)
492494
}
493495

src/query.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ export class Query extends Promise {
127127
return this
128128
}
129129

130+
values() {
131+
this.isRaw = 'values'
132+
return this
133+
}
134+
130135
async handle() {
131136
!this.executed && (this.executed = true) && await 1 && this.handler(this)
132137
}

tests/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,10 @@ t('Raw method returns values unparsed as Buffer', async() => {
16101610
]
16111611
})
16121612

1613+
t('Array returns rows as arrays of columns', async() => {
1614+
return [(await sql`select 1`.values())[0][0], 1]
1615+
})
1616+
16131617
t('Copy read', async() => {
16141618
const result = []
16151619

0 commit comments

Comments
 (0)