Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct type of types option to CustomTypesConfig #115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -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,
},
});
```

Expand Down
6 changes: 3 additions & 3 deletions dist/jsr/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -246,7 +246,7 @@ export interface HTTPQueryOptions<ArrayMode extends boolean, FullResults extends
* Custom type parsers
* See https://github.com/brianc/node-pg-types
*/
types?: typeof PgTypes;
types?: CustomTypesConfig;
}

export interface HTTPTransactionOptions<ArrayMode extends boolean, FullResults extends boolean> extends HTTPQueryOptions<ArrayMode, FullResults> {
Expand Down
6 changes: 3 additions & 3 deletions dist/npm/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -250,7 +250,7 @@ export interface HTTPQueryOptions<ArrayMode extends boolean, FullResults extends
* Custom type parsers
* See https://github.com/brianc/node-pg-types
*/
types?: typeof PgTypes;
types?: CustomTypesConfig;
}

export interface HTTPTransactionOptions<ArrayMode extends boolean, FullResults extends boolean> extends HTTPQueryOptions<ArrayMode, FullResults> {
Expand Down
6 changes: 3 additions & 3 deletions dist/npm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -246,7 +246,7 @@ export interface HTTPQueryOptions<ArrayMode extends boolean, FullResults extends
* Custom type parsers
* See https://github.com/brianc/node-pg-types
*/
types?: typeof PgTypes;
types?: CustomTypesConfig;
}

export interface HTTPTransactionOptions<ArrayMode extends boolean, FullResults extends boolean> extends HTTPQueryOptions<ArrayMode, FullResults> {
Expand Down
6 changes: 3 additions & 3 deletions export/httpQuery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { types as defaultTypes } from '.';
import type { CustomTypesConfig } from 'pg';
import { Socket } from '../shims/net';
import { parse } from '../shims/url';

Expand Down Expand Up @@ -50,7 +50,7 @@ interface HTTPQueryOptions {
arrayMode?: boolean; // default false
fullResults?: boolean; // default false
fetchOptions?: Record<string, any>;
types?: typeof defaultTypes;
types?: CustomTypesConfig;

// these callback options are not currently exported:
queryCallback?: (query: ParameterizedQuery) => void;
Expand Down Expand Up @@ -87,7 +87,7 @@ interface ProcessQueryResultOptions {
fullResults: boolean;
parameterizedQuery: ParameterizedQuery;
resultCallback: HTTPQueryOptions['resultCallback'];
types?: typeof defaultTypes;
types?: CustomTypesConfig;
}

const txnArgErrMsg =
Expand Down