Skip to content

Commit

Permalink
chore: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bombillazo committed Feb 18, 2024
1 parent 5bc209b commit ad3e8f9
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,10 @@ available:
You can also provide custom decoders to the client that will be used to decode
the result data. This can be done by setting the `decoders` controls option in
the client configuration. This option is a map object where the keys are the
type names or Oid numbers and the values are the custom decoder functions.
type names or OID numbers and the values are the custom decoder functions.

You can use it with the decode strategy. Custom decoders take precedence over
the strategy and internal parsers.
the strategy and internal decoders.

```ts
{
Expand All @@ -785,7 +785,36 @@ the strategy and internal parsers.
const result = await client.queryObject(
"SELECT ID, NAME, IS_ACTIVE FROM PEOPLE",
);
console.log(result.rows[0]); // {id: '1', name: 'Javier', is_active: { value: false, type: "boolean"}}
console.log(result.rows[0]);
// {id: '1', name: 'Javier', is_active: { value: false, type: "boolean"}}
}
```

The driver takes care of parsing the related `array` OID types automatically.
For example, if a custom decoder is defined for the `int4` type, it will be applied
when parsing `int4[]` arrays. If needed, you can have separate custom decoders for the
array and non-array types by defining another custom decoders for the array type itself.

```ts
{
const client = new Client({
database: "some_db",
user: "some_user",
controls: {
decodeStrategy: "string",
decoders: {
// Custom decoder for int4 (OID 23 = int4)
// convert to int and multiply by 100
23: (value: string) => parseInt(value, 10) * 100,
},
},
});

const result = await client.queryObject(
"SELECT ARRAY[ 2, 2, 3, 1 ] AS scores, 8 final_score;",
);
console.log(result.rows[0]);
// { scores: [ 200, 200, 300, 100 ], final_score: 800 }
}
```

Expand Down

0 comments on commit ad3e8f9

Please sign in to comment.