@@ -758,10 +758,10 @@ available:
758758You can also provide custom decoders to the client that will be used to decode
759759the result data. This can be done by setting the ` decoders ` controls option in
760760the client configuration. This option is a map object where the keys are the
761- type names or Oid numbers and the values are the custom decoder functions.
761+ type names or OID numbers and the values are the custom decoder functions.
762762
763763You can use it with the decode strategy. Custom decoders take precedence over
764- the strategy and internal parsers .
764+ the strategy and internal decoders .
765765
766766``` ts
767767{
@@ -785,7 +785,36 @@ the strategy and internal parsers.
785785 const result = await client .queryObject (
786786 " SELECT ID, NAME, IS_ACTIVE FROM PEOPLE" ,
787787 );
788- console .log (result .rows [0 ]); // {id: '1', name: 'Javier', is_active: { value: false, type: "boolean"}}
788+ console .log (result .rows [0 ]);
789+ // {id: '1', name: 'Javier', is_active: { value: false, type: "boolean"}}
790+ }
791+ ```
792+
793+ The driver takes care of parsing the related ` array ` OID types automatically.
794+ For example, if a custom decoder is defined for the ` int4 ` type, it will be applied
795+ when parsing ` int4[] ` arrays. If needed, you can have separate custom decoders for the
796+ array and non-array types by defining another custom decoders for the array type itself.
797+
798+ ``` ts
799+ {
800+ const client = new Client ({
801+ database: " some_db" ,
802+ user: " some_user" ,
803+ controls: {
804+ decodeStrategy: " string" ,
805+ decoders: {
806+ // Custom decoder for int4 (OID 23 = int4)
807+ // convert to int and multiply by 100
808+ 23 : (value : string ) => parseInt (value , 10 ) * 100 ,
809+ },
810+ },
811+ });
812+
813+ const result = await client .queryObject (
814+ " SELECT ARRAY[ 2, 2, 3, 1 ] AS scores, 8 final_score;" ,
815+ );
816+ console .log (result .rows [0 ]);
817+ // { scores: [ 200, 200, 300, 100 ], final_score: 800 }
789818}
790819```
791820
0 commit comments