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

[BUG]: jsonb values are double-JSON.parse()'d on select #3018

Open
legitmaxwu opened this issue Sep 28, 2024 · 1 comment · May be fixed by #3032
Open

[BUG]: jsonb values are double-JSON.parse()'d on select #3018

legitmaxwu opened this issue Sep 28, 2024 · 1 comment · May be fixed by #3032
Labels
bug Something isn't working db/postgres priority Will be worked on next

Comments

@legitmaxwu
Copy link

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.23.0

Describe the Bug

I'm using drizzle + node postgres.

When I store a string e.g. "10.5" under a jsonb column, I expect it to be returned as "10.5" in string format. Instead it's returned as 10.5

This is probably because node-postgres does JSON.parse once, and then the jsonb column does it again.

Expected behavior

When I store a string e.g. "10.5" under a jsonb column, I expect it to be returned as "10.5" in string format.

Environment & setup

drizzle + node postgres
computer: macos

@legitmaxwu legitmaxwu added the bug Something isn't working label Sep 28, 2024
@legitmaxwu
Copy link
Author

legitmaxwu commented Sep 28, 2024

to work around this for now, either create a customJsonb type like this:

import { customType } from "drizzle-orm/pg-core";

const jsonb = customType<{
  data: unknown;
  driverData: string;
  config: undefined;
}>({
  dataType() {
    return `jsonb`;
  },
  fromDriver(value: string): unknown {
    return value;
  },
  toDriver(value: unknown): string {
    return JSON.stringify(value);
  },
});

OR remove JSON.parse from node-postgres's jsonb type parser via:

import { types } from "pg";

types.setTypeParser(types.builtins.JSONB, (val) => val);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working db/postgres priority Will be worked on next
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants