Skip to content

Bun.SQL with prepare:false serializes object JSONB parameters as "[object Object]" #30221

Closed as duplicate

Description

@ARMATAV

What version of Bun is running?

1.3.13+bf2e2cecf

What platform is your computer?

Darwin 24.6.0 arm64 arm

What steps can reproduce the bug?

Run this with DATABASE_URL pointing to PostgreSQL:

import { SQL } from "bun";

const sql = new SQL({
  url: process.env.DATABASE_URL!,
  max: 1,
  prepare: false,
});

try {
  const payload = { hello: "world" };

  const [row] = await sql`
    SELECT ${payload}::jsonb AS value
  `;

  console.log(row);
} finally {
  await sql.end();
}

For comparison, both of these work:

// Works with prepare:false if JSON is pre-stringified:
await sql`
  SELECT ${JSON.stringify({ hello: "world" })}::jsonb AS value
`;

// Works with prepare:true using the raw object:
const preparedSql = new SQL({
  url: process.env.DATABASE_URL!,
  max: 1,
  prepare: true,
});

await preparedSql`
  SELECT ${{ hello: "world" }}::jsonb AS value
`;

This matters for PgBouncer/Supavisor transaction-pooler usage, where Bun’s docs recommend prepare: false.

What is the expected behavior?

With prepare: false, binding a plain JS object into a jsonb parameter should serialize it as JSON, matching the prepare: true
behavior.

What do you see instead?

PostgreSQL receives [object Object] and rejects it as invalid JSON:

PostgresError: invalid input syntax for type json
errno: "22P02"
detail: "Token \"object\" is invalid."
where: "JSON data, line 1: [object...\nunnamed portal parameter $1 = '...'"
routine: "json_errsave_error"
code: "ERR_POSTGRES_SERVER_ERROR"
prepare:false + object + ::jsonb       -> ERROR: Token "object" is invalid
prepare:false + JSON.stringify + cast  -> OK: { value: { hello: "world" } }
prepare:false + object without cast    -> OK, but returns "[object Object]"
prepare:true  + object + ::jsonb       -> OK: { value: { hello: "world" } }

Additional information

No response

Activity

  1. robobun commented on May 4, 2026

    @robobun
    Collaborator

    ✅ Reproduced against local Postgres — writeBind fell through to String.fromJS (→ Object.prototype.toString) for object params when Parse sent OID 0 for prepare: false. Fix re-derives the tag with Tag.fromJS when the slot is unspecified and routes JSON-shaped values through jsonStringifyFast.

    Fix in #30222.

  2. added a commit that references this issue on May 4, 2026
  3. added 2 commits that reference this issue on May 4, 2026
    df408b3
    983f1e2
  4. CoffeeFlux commented on Jun 16, 2026

    @CoffeeFlux

    This one's burning us too; we're still on 1.3.10 due to it.

  5. Aokoooooo commented on Jun 24, 2026

    @Aokoooooo

    any updates?

  6. robobun commented on Sep 13, 2026

    @robobun
    Collaborator

    Duplicate of #29010. Both issues report the same problem: Bun.SQL (Postgres) with prepare: false sends a Date or a plain object parameter through a generic toString(), so the server rejects the timestamp or json value, while the same query works with prepare: true. Closing this one to keep the discussion in one place. Please follow #29010. If you think this is a different problem, comment here and it can be reopened.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions