Skip to content

id is serialized as BigInt when using createElectricCollection, leading to a JSON.stringify error #142

Closed

Description

@xamey

This is how I use createElectricCollection and useLiveQuery :

const taskDemandCollection = createElectricCollection<TaskDemand>({
    id: "task_demands",
    streamOptions: {
      url: `http://localhost:3000/task_demands`,
      headers: {
        Authorization: `Bearer ${token}`,
      },
    },
    params: {
      table: "task_demands",
    },
    primaryKey: ["id"],
    schema: TaskDemandSchema,
  });

  const { data: taskDemands } = useLiveQuery((query) =>
    query
      .from({ taskDemandCollection })
      .select("@id", "@demand", "@status")
      .keyBy("@id")
  );

This is the subsequent HTTP response from http://localhost:3000/task_demands?handle=111322364-1749222878597402&offset=0_0 :

[
    {
        "value": {
            "demand": "Buy a new couch",
            "id": "1",
            "notify_slack": "t",
            "status": "pending",
            "user_id": "1"
        },
        "key": "\"public\".\"task_demands\"/\"1\"",
        "headers": {
            "last": true,
            "relation": [
                "public",
                "task_demands"
            ],
            "operation": "insert",
            "lsn": "36341840",
            "op_position": 0,
            "txids": [
                858
            ]
        }
    },
    {
        "value": {
            "id": "1",
            "status": "researching"
        },
        "key": "\"public\".\"task_demands\"/\"1\"",
        "headers": {
            "last": true,
            "relation": [
                "public",
                "task_demands"
            ],
            "operation": "update",
            "lsn": "36350304",
            "op_position": 0,
            "txids": [
                868
            ]
        }
    },
    {
        "value": {
            "id": "1",
            "status": "completed"
        },
        "key": "\"public\".\"task_demands\"/\"1\"",
        "headers": {
            "last": true,
            "relation": [
                "public",
                "task_demands"
            ],
            "operation": "update",
            "lsn": "36638528",
            "op_position": 0,
            "txids": [
                889
            ]
        }
    },
    {
        "headers": {
            "control": "up-to-date",
            "global_last_seen_lsn": "36638528"
        }
    }
]

whole response with headers are :

[{"value":{"demand":"Buy a new couch","id":"1","notify_slack":"t","status":"pending","user_id":"1"},"key":"\"public\".\"task_demands\"/\"1\"","headers":{"last":true,"relation":["public","task_demands"],"operation":"insert","lsn":"36341840","op_position":0,"txids":[858]}},{"value":{"id":"1","status":"researching"},"key":"\"public\".\"task_demands\"/\"1\"","headers":{"last":true,"relation":["public","task_demands"],"operation":"update","lsn":"36350304","op_position":0,"txids":[868]}},{"value":{"id":"1","status":"completed"},"key":"\"public\".\"task_demands\"/\"1\"","headers":{"last":true,"relation":["public","task_demands"],"operation":"update","lsn":"36638528","op_position":0,"txids":[889]}},{"headers":{"control":"up-to-date","global_last_seen_lsn":"36638528"}}]

But a Uncaught TypeError: Do not know how to serialize a BigInt error is triggered in key-by.ts, line 54.
row is {id: 1n, demand: 'Buy a new couch', status: 'completed'}
We see that id is a BigInt.

It it because id type is int8

Activity

  1. samwillis commented on Jun 6, 2025

    @samwillis
    Collaborator

    Ah, thanks. This is due to the hashing inside the the D2TS query engine. I've filed an issue to remind me over there: neondatabase/electric-d2ts#55

  2. KyleAMathews commented on Jun 6, 2025

    @KyleAMathews
    Collaborator

    @samwillis key-by.ts is in this repo right?

    I added a pr for that #143

  3. KyleAMathews commented on Jul 23, 2025

    @KyleAMathews
    Collaborator

    This is fixed now

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions