question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: a way to convert edgedb.Object to dict

See original GitHub issue

Summary

Currently Object class does not look like dict and different tools do not understand how to use it. My suggestion is to add Object.to_dict method or inherit from python dict to allow direct use.

Use case

Converting query result to pydantic model:

class User(pydantic.BaseModel):
    id: uuid.UUID
    nickname: str
    created_at: datetime.datetime
    bio: Optional[str] = None

async def get(db: edgedb.AsyncIOConnection, *, id: uuid.UUID) -> Optional[User]:
    result = await db.fetchall(
        """
        SELECT User {
            id,
            nickname,
            bio,
            created_at,
        }
        FILTER
            .id = <uuid>$id
            AND .email_vefified = true
        LIMIT 1
        """,
        id=id,
    )

    if not result:
        return None

    return User.parse_obj(result[0])  # unable to parse edgedb.Object

There are workarounds to this neither of which look good enough:

  • custom function for converting (not convenient, probably not efficient)
  • fetching results with fetchone_json and then using parse_raw pydantic method, but it’s very library specific, forces the use of fetchone over fetchall and inefficient (double json conversion)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:24 (16 by maintainers)

github_iconTop GitHub Comments

5reactions
1st1commented, Aug 1, 2020

Would it help if we add tools to generate files like https://github.com/kurtrottmann/simple-stack-fastapi-edgedb/blob/master/backend/app/schemas.py automatically from the DB schema? Would that allow you to no longer depend on pydantic?

And it sounds like you don’t need a recursive asdict?

2reactions
feluxecommented, Jun 3, 2022

Would it help if we add tools to generate files like https://github.com/kurtrottmann/simple-stack-fastapi-edgedb/blob/master/backend/app/schemas.py automatically from the DB schema? Would that allow you to no longer depend on pydantic?

This would be amazing. If it was possible to generate Python and TypeScript types from SDL I could use them on every layer of my application stack:

  • DB Models
  • Python HTTP Endpoints
  • Typescript Frontend HTTP Client

After that add something like edgerpc (similar to grpc) or sdl_to_protobufs for service to service communication and the toolkit would be complete. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · edgedb/edgedb-python - GitHub
Contribute to edgedb/edgedb-python development by creating an account on GitHub. ... Feature request: a way to convert edgedb.Object to dict.
Read more >
Bringing .NET to EdgeDB
Today, we're excited to announce the official .NET binding for EdgeDB! I crafted the first version of the library a few months ago....
Read more >
How to convert request.data to dict? - python - Stack Overflow
Edit: For post request: import requests import json url = "https://jsonplaceholder.typicode.com/posts/" payload = { "userId": 10, "id": 901, ...
Read more >
NET SDK for Cloud Bucketing - DevCycle Docs
All requests, including user data are sent to DevCycle servers to ensure ... for a given user and return them as Dictionary<String, Feature>....
Read more >
PEP 603: Adding a frozenmap type to collections
Author: Yury Selivanov yury@edgedb.com ... a dict ,; another frozenmap ,; an object with an items() method that is expected to return
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found