edgedb-python icon indicating copy to clipboard operation
edgedb-python copied to clipboard

Issue with Inserting Byte String JSON into EdgeDB using `orjson`

Open BecomeAllan opened this issue 1 year ago • 0 comments

Hello everyone,

I'm having trouble inserting JSON data serialized as a byte string into an EdgeDB query. I'm using the orjson library to serialize the JSON into a byte string, and I want to decode and insert this data into the database using the json_array_unpack function.

Here is the code I'm trying to use:

import orjson
from edgedb import create_async_client

# Data to serialize
data = [{"name": "example_name"}]

# Serializing the data to a byte string JSON
serialized_data = orjson.dumps(data)

# Creating the EdgeDB client (assuming you have the correct client configuration)
client = create_async_client()

async def insert_data(client, serialized_data):
    await client.query_json("""
    WITH
    raw_data := <json>$data
    FOR item IN json_array_unpack(raw_data) UNION (
        UPDATE Post SET {
            name := item['name']
        }
    )
    """, data=serialized_data)

# Calling the data insertion function
import asyncio
asyncio.run(insert_data(client, serialized_data))

Problem:

When trying to execute the query, I am unable to insert the data correctly. I've tried various configurations, but it seems that the database is not properly interpreting the byte string JSON that I'm passing as the $data parameter.

Questions:

  1. How should I correctly pass a byte string JSON in the EdgeDB query?
  2. Is there a better way to handle and decode byte string JSONs directly within the EdgeDB query?
  3. Could someone provide a working example of how to do this?

Any help would be greatly appreciated!

BecomeAllan avatar Jun 15 '24 13:06 BecomeAllan