vercel / vercel/storage

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

Open
#635 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
595
Forks
101
Avg merge
2h 24m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the createPool, db.connect, sql'Foo', and createClient snippets, then inspect how each entry point handles the types option and how pg-types is consulted. Done means type parsers supplied through the supported pool or connection paths are invoked consistently, with coverage for the bigint parser use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, postgres, typescript
Domain
backend-api-design, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.