Skip to content

Commit

Permalink
feat: adds raw property to error and debug parameters (#167)
Browse files Browse the repository at this point in the history
* feat: adds `raw` property to error and debug parameters

Fixes #140

* fix(types): adds `null` as additional type to `Parameter.raw` and `Parameter.value`
  • Loading branch information
stalniy committed Apr 5, 2021
1 parent a6394c1 commit 9e83e7f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,15 @@ function Postgres(a, b) {

function getType(x) {
if (x == null)
return { type: 0, value: x }
return { type: 0, value: x, raw: x }

const value = x.type ? x.value : x
, type = x.type || inferType(value)

return {
type,
value: (options.serializers[type] || types.string.serialize)(value)
value: (options.serializers[type] || types.string.serialize)(value),
raw: x
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1075,11 +1075,16 @@ t('Error contains query string', async() => [
(await sql`selec 1`.catch(err => err.query))
])

t('Error contains query parameters', async() => [
t('Error contains query serialized parameters', async() => [
'1',
(await sql`selec ${ 1 }`.catch(err => err.parameters[0].value))
])

t('Error contains query raw parameters', async() => [
1,
(await sql`selec ${ 1 }`.catch(err => err.parameters[0].raw))
])

t('Query string is not enumerable', async() => {
const sql = postgres({ ...options, debug: false })
return [
Expand Down
8 changes: 6 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,13 @@ declare namespace postgres {
*/
type: number;
/**
* Value to serialize
* Serialized value
*/
value: T;
value: string | null;
/**
* Raw value to serialize
*/
raw: T | null;
}

interface ArrayParameter<T extends SerializableParameter[] = SerializableParameter[]> extends Parameter<T | T[]> {
Expand Down

0 comments on commit 9e83e7f

Please sign in to comment.