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

Issue with Inserting Byte String JSON into EdgeDB using orjson #502

Open
BecomeAllan opened this issue Jun 15, 2024 · 0 comments
Open

Issue with Inserting Byte String JSON into EdgeDB using orjson #502

BecomeAllan opened this issue Jun 15, 2024 · 0 comments

Comments

@BecomeAllan
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant