Skip to content

expose types property on public HTTPQueryOptions type - #110

Merged
pffigueiredo merged 2 commits into
neondatabase:mainfrom
andyjy:types-type
Nov 5, 2024
Merged

pffigueiredo merged 2 commits into
neondatabase:mainfrom
andyjy:types-type

Conversation

@andyjy

@andyjy andyjy commented Oct 26, 2024

Copy link
Copy Markdown
Contributor

Support for passing custom type overrides was recently added in #92

Having confirmed it works, the types property needs to be added to the public types declaration files in order to be able to use it directly on the neon() function.

(This is required to fix types support for the PrismaNeonHTTP adapter)

Fixes #109

@andyjy

andyjy commented Oct 26, 2024

Copy link
Copy Markdown
Contributor Author

cc @davidgomes

@davidgomes

Copy link
Copy Markdown
Contributor

@andyjy thank you! We'll review shortly

@pffigueiredo

Copy link
Copy Markdown
Contributor

Looks great! 🚀

This is a bit of undocumented territory, but if you may, leave a note to the top of CHANGELOG.md file like:

## UNRELEASED
expose types property on public HTTPQueryOptions type

@pffigueiredo

Copy link
Copy Markdown
Contributor

Looks great! 🚀

This is a bit of undocumented territory, but if you may, leave a note to the top of CHANGELOG.md file like:

## UNRELEASED
expose types property on public HTTPQueryOptions type

We will proceed with a patch once this is merged

@andyjy

andyjy commented Nov 5, 2024

Copy link
Copy Markdown
Contributor Author

@pffigueiredo awesome! changelog duly updated.

@pffigueiredo
pffigueiredo merged commit eff8139 into neondatabase:main Nov 5, 2024
@pffigueiredo

Copy link
Copy Markdown
Contributor

Shipped in v0.10.2 🚀

@andyjy

andyjy commented Nov 6, 2024

Copy link
Copy Markdown
Contributor Author

Apologies folks - this PR correctly made the existing type public, but I now realise the existing type looks to be incorrect - I think the type of all types option params should instead be CustomTypesConfig from pg, not typeof defaultTypes. (The former exposes a single property getTypeParser; the latter incorrectly mandates the presence of the other exports from pg-types (setTypeParser, builtins). I'll look into a new PR 🙏

@pffigueiredo

Copy link
Copy Markdown
Contributor

Apologies folks - this PR correctly made the existing type public, but I now realise the existing type looks to be incorrect - I think the type of all types option params should instead be CustomTypesConfig from pg, not typeof defaultTypes. (The former exposes a single property getTypeParser; the latter incorrectly mandates the presence of the other exports from pg-types (setTypeParser, builtins). I'll look into a new PR 🙏

Uh, so this is not how you would use it?

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
});

@andyjy

andyjy commented Nov 6, 2024 •

Copy link
Copy Markdown
Contributor Author

@pffigueiredo in short, no. That code works at runtime, but:

  1. The use of PgTypes.setTypeParser() "pollutes" the global scope in that it overrides the global PgTypes parsers which are then applied anywhere pg subsequently parses types, not just when passed in via the types: option as in your final line. As an example this was previously reported as a bug in Prisma and fixed by refactoring to pass in the type parsers via the types parameter without calling the global PgTypes.setTypeParser()

  2. Your example code passing types: PgTypes is passing the whole interface from PgTypes, which includes the necessary getTypeParser() from CustomTypesConfig but also includes the additional exports (setTypeParser, builtins) which are not required:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose types property on HTTPQueryOptions public export

3 participants