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

ORM: support json

Open 1st1 opened this issue 7 months ago • 4 comments

We are not unpacking JSON by default -- we should consider doing that.

Pydantic supports JSON unpacking (partial & full), details here: https://docs.pydantic.dev/latest/concepts/json/#json-parsing


If we have this in the schema

type User {
  name: str;
  data: json;
}

we can maybe enable something like this then:

# User subclasses the generated model:
class MyUser(models.default.User):
    data: gel.JsonUnpacker[
        models.default.User.__typeof__.data,
        MyDataModel
    ]

# User defines their model to parse JSON:
class MyDataModel(pydantic.BaseModel):
    ...

Or, alternatively:

# User subclasses the generated model to
# specify a *new* computed field to unpack
# JSON into
class MyUser(models.default.User):
    parsed_data: gel.JsonUnpackFrom[
        models.default.User.__typeof__.data,
        MyDataModel
    ]

# User defines their model to parse JSON:
class MyDataModel(pydantic.BaseModel):
    ...

I think we have two options:

  1. Always unpack JSON -- if there's a type for it setup -- use that. No type -- return whatever json.loads() returns

  2. Only unpack JSON when the user asked for it, either with ORM model customization, or with the upcoming .with_codecs() API.

I'm in favor of (2).

1st1 avatar May 31 '25 15:05 1st1

Given time constraints, on day 1, we can just keep the status quo and return strings. If we go with option (2).

1st1 avatar May 31 '25 16:05 1st1

  • Use Gel schema annotations to specify full qualified name for a Pydantic JSON model -- this is better than making users overload reflected classes - that composes poorly
  • @msullivan Need to add lang::py lang::js to Gel 6.8 -- this is to enable this as soon as we have bandwidth
  • Modify gel-python to unpack JSON into strings NOW (just use json.loads)

1st1 avatar Jun 16 '25 19:06 1st1

I've doubts about unpacking JSON by default. The whole point of using pydantic is type safety, maybe nobody wants to unpack automatically to non-pydantic raw python dicts?

1st1 avatar Jul 12 '25 05:07 1st1

Consensus reached: we will not unpack JSON automatically if it doesn't have a schema (std::lang::py::annotation). We can add a client config-like thing client.with_auto_json().query(...) and then unpack. But not by default.

1st1 avatar Jul 15 '25 18:07 1st1