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

Postgres: Cannot use type parser using createPool or db.connect #635

Open
mattorchard opened this issue Mar 17, 2024 · 1 comment
Open

Comments

@mattorchard
Copy link

There seems to be no way to override the type parser other than createClient (which then requires manually closing connections).

  • createPool accepts the types option, but ignores it
  • pg-types types.setTypeParser is ignored
  • db.connect does not accept the types option
  • sql'Foo' does not accept the types option

I assume this is a bug for createPool, but also seems like the pg-types approach should work for compatibility sake.

Environment

  • Running locally, using Next.js, connected to a cloud vercel-psql instance
  • version: 0.7.2

Snippets

Supplying the type parser to createPool has no impact.

const pool = createPool({
  types: {
    getTypeParser: (typeId, format) => {
      console.log("This function is never invoked");
    },
  },
});

Calling setTypeParser has no impact

import types from "pg-types";
types.setTypeParser(types.builtins.INT8, () => {
  console.log("This function is never invoked");
});

Create client on the on the other hand, does work.
But this option is unappealing in many cases because that then requires manually closing the client connection.

const client = createClient({
  types: {
    getTypeParser: (typeId, format) => {
      console.log("This function is called as expected")
    },
  },
});

Further context

My actual use case is to override the bigint parser with one that converts it to a JS-float rather than a string.
The following snippet technically works. But my specific setup does not have a great spot to close the DB connection, making it rather unappealing.

import types from "pg-types";

const integerParser = (value: string): number => {
  const int = parseInt(value);
  if (isNaN(int)) throw new Error(`Unable to parse integer "${value}"`);
  if (int >= Number.MAX_SAFE_INTEGER)
    throw new Error(`Integer "${value}" exceeds maximum safe size`);
  return int;
};

const client = createClient({
  types: {
    getTypeParser: (typeId, format) => {
      switch (typeId) {
        case types.builtins.INT8:
          return integerParser;
        default:
          return types.getTypeParser(typeId, format) as unknown;
      }
    },
  },
});
@mattorchard
Copy link
Author

mattorchard commented Mar 18, 2024

Did find a workaround: going direct to neon by adding it as a peer dep in my package.json

  "peerDependencies": {
    "@neondatabase/serverless": "0.7.2"
  },

and then overwriting the type parser that it exports

import { types } from "@neondatabase/serverless";
types.setTypeParser(types.builtins.INT8, integerParser);

@vercel/postgres could probably re-export the types to make this slightly less janky

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

No branches or pull requests

1 participant