From b9709f1c123f2f7c85620f91283f843b7f98916e Mon Sep 17 00:00:00 2001 From: andyjy Date: Wed, 6 Nov 2024 10:33:39 +0000 Subject: [PATCH 1/2] correct type of types option --- CONFIG.md | 2 +- dist/jsr/index.d.ts | 6 +++--- dist/npm/index.d.mts | 6 +++--- dist/npm/index.d.ts | 6 +++--- export/httpQuery.ts | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 4f56abf..3568e81 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -108,7 +108,7 @@ const rows = await sql('SELECT * FROM posts WHERE id = $1', [postId], { clearTimeout(timeout); ``` -### `types: typeof PgTypes` +### `types: CustomTypesConfig` The `types` option can be passed to `neon(...)` to override the default PostgreSQL type parsers provided by `PgTypes`. This is useful if you want to define custom parsing behavior for specific PostgreSQL data types, allowing you to control how data is converted when retrieved from the database. Learn more in the [PgTypes official documentation](https://github.com/brianc/node-pg-types). diff --git a/dist/jsr/index.d.ts b/dist/jsr/index.d.ts index ec5c7b4..d9bf720 100644 --- a/dist/jsr/index.d.ts +++ b/dist/jsr/index.d.ts @@ -161,9 +161,9 @@ export interface NeonConfig extends NeonConfigGlobalOnly, NeonConfigGlobalAndCli import { Client as PgClient, ClientBase as PgClientBase, + CustomTypesConfig, Pool as PgPool, - PoolClient as PgPoolClient, - types as PgTypes + PoolClient as PgPoolClient } from "pg"; export class ClientBase extends PgClientBase { @@ -246,7 +246,7 @@ export interface HTTPQueryOptions extends HTTPQueryOptions { diff --git a/dist/npm/index.d.mts b/dist/npm/index.d.mts index 6c98c62..2533c21 100644 --- a/dist/npm/index.d.mts +++ b/dist/npm/index.d.mts @@ -165,9 +165,9 @@ export interface NeonConfig extends NeonConfigGlobalOnly, NeonConfigGlobalAndCli import { Client as PgClient, ClientBase as PgClientBase, + CustomTypesConfig, Pool as PgPool, - PoolClient as PgPoolClient, - types as PgTypes + PoolClient as PgPoolClient } from "pg"; export class ClientBase extends PgClientBase { @@ -250,7 +250,7 @@ export interface HTTPQueryOptions extends HTTPQueryOptions { diff --git a/dist/npm/index.d.ts b/dist/npm/index.d.ts index ec5c7b4..d9bf720 100644 --- a/dist/npm/index.d.ts +++ b/dist/npm/index.d.ts @@ -161,9 +161,9 @@ export interface NeonConfig extends NeonConfigGlobalOnly, NeonConfigGlobalAndCli import { Client as PgClient, ClientBase as PgClientBase, + CustomTypesConfig, Pool as PgPool, - PoolClient as PgPoolClient, - types as PgTypes + PoolClient as PgPoolClient } from "pg"; export class ClientBase extends PgClientBase { @@ -246,7 +246,7 @@ export interface HTTPQueryOptions extends HTTPQueryOptions { diff --git a/export/httpQuery.ts b/export/httpQuery.ts index 70cb814..4ea5d11 100644 --- a/export/httpQuery.ts +++ b/export/httpQuery.ts @@ -1,4 +1,4 @@ -import { types as defaultTypes } from '.'; +import type { CustomTypesConfig } from 'pg'; import { Socket } from '../shims/net'; import { parse } from '../shims/url'; @@ -50,7 +50,7 @@ interface HTTPQueryOptions { arrayMode?: boolean; // default false fullResults?: boolean; // default false fetchOptions?: Record; - types?: typeof defaultTypes; + types?: CustomTypesConfig; // these callback options are not currently exported: queryCallback?: (query: ParameterizedQuery) => void; @@ -87,7 +87,7 @@ interface ProcessQueryResultOptions { fullResults: boolean; parameterizedQuery: ParameterizedQuery; resultCallback: HTTPQueryOptions['resultCallback']; - types?: typeof defaultTypes; + types?: CustomTypesConfig; } const txnArgErrMsg = From edc42b80f19c075fa2fcb953c39fb5929755ef30 Mon Sep 17 00:00:00 2001 From: andyjy Date: Wed, 6 Nov 2024 10:37:09 +0000 Subject: [PATCH 2/2] update example in CONFIG.md (but ugh pg's types require displeasing type cast) --- CONFIG.md | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 3568e81..a6391de 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -118,16 +118,23 @@ Example of usage: import PgTypes from 'pg-types'; import { neon } from '@neondatabase/serverless'; -// Define custom parsers for specific PostgreSQL types -// Parse PostgreSQL `DATE` fields as JavaScript `Date` objects -PgTypes.setTypeParser(PgTypes.builtins.DATE, (val) => new Date(val)); - -// Parse PostgreSQL `NUMERIC` fields as JavaScript `float` values -PgTypes.setTypeParser(PgTypes.builtins.NUMERIC, parseFloat); - // Configure the Neon client with the custom `types` parser const sql = neon(process.env.DATABASE_URL, { - types: PgTypes, // Pass in the custom PgTypes object here + types: { + getTypeParser: ((oid, format?: any) => { + // Define custom parsers for specific PostgreSQL types + // Parse PostgreSQL `DATE` fields as JavaScript `Date` objects + if (oid === PgTypes.builtins.DATE) { + return (val: any) => new Date(val); + } + // Parse PostgreSQL `NUMERIC` fields as JavaScript `float` values + if (oid === PgTypes.builtins.NUMERIC) { + return parseFloat; + } + // For all other types, use the default parser + return PgTypes.getTypeParser(oid, format); + }) as typeof PgTypes.getTypeParser, + }, }); ```