Skip to main content

Alternative Python EdgeDB code generator (Pydantic V2 + asyncio, FastAPI compatible)

Project description

edgedb-pydantic-codegen

This tool generates Python typesafe async code for EdgeQL queries using Pydantic V2.

The generated models can be directly used with other libraries such as FastAPI.

This is an alternative to the built-in code generator of the official edgedb-python library.

Usage

In an EdgeDB initialized project, simply run

uvx edgedb-pydantic-codegen <directory>

All *.edgeql files in <directory> and its subdirectories will be processed and the generated code saved next to them.

Generated code example

from enum import StrEnum

from edgedb import AsyncIOExecutor
from pydantic import BaseModel, TypeAdapter

EDGEQL_QUERY = r"""
with
  discord_id := <int64>$discord_id,
  moecoins := <optional int32>$moecoins,
  blood_shards := <optional int32>$blood_shards,
  updated := (
    update waicolle::Player
    filter .client = global client and .user.discord_id = discord_id
    set {
      moecoins := .moecoins + (moecoins ?? 0),
      blood_shards := .blood_shards + (blood_shards ?? 0),
    }
  )
select updated {
  game_mode,
  moecoins,
  blood_shards,
  user: {
    discord_id,
    discord_id_str,
  },
}
"""


class WaicolleGameMode(StrEnum):
    WAIFU = "WAIFU"
    HUSBANDO = "HUSBANDO"
    ALL = "ALL"


class PlayerAddCoinsResultUser(BaseModel):
    discord_id: int
    discord_id_str: str


class PlayerAddCoinsResult(BaseModel):
    game_mode: WaicolleGameMode
    moecoins: int
    blood_shards: int
    user: PlayerAddCoinsResultUser


adapter = TypeAdapter(PlayerAddCoinsResult | None)


async def player_add_coins(
    executor: AsyncIOExecutor,
    *,
    discord_id: int,
    moecoins: int | None = None,
    blood_shards: int | None = None,
) -> PlayerAddCoinsResult | None:
    resp = await executor.query_single_json(
        EDGEQL_QUERY,
        discord_id=discord_id,
        moecoins=moecoins,
        blood_shards=blood_shards,
    )
    return adapter.validate_json(resp, strict=False)

Caveats

Currently this tool does not support:

  • TupleType, RangeType and MultiRangeType collections
  • std::duration, cal::relative_duration, cal::date_duration, cfg::memory and ext::pgvector::vector objects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edgedb_pydantic_codegen-2024.10.6.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file edgedb_pydantic_codegen-2024.10.6.tar.gz.

File metadata

File hashes

Hashes for edgedb_pydantic_codegen-2024.10.6.tar.gz
Algorithm Hash digest
SHA256 c224b3af042a99512028ebb4af4fa02a6c56c4133f93e4c7cba6a3463ee25c72
MD5 5baeab1149b1e51384b8014bf30caf67
BLAKE2b-256 e6b04fe2430cc8a427e0a835aed47088aa2110588d9249a29e4fea81d563ce98

See more details on using hashes here.

File details

Details for the file edgedb_pydantic_codegen-2024.10.6-py3-none-any.whl.

File metadata

File hashes

Hashes for edgedb_pydantic_codegen-2024.10.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1a7798f318f927f3847b656ccffca3529f46a4aa2d8d286cbbe001a2fdc939cc
MD5 abf43e9a7de870b080c6f49f2b53d40b
BLAKE2b-256 8a841e43d5f8b4fcdb45096d3b47bb55d65cb1563115e6b83b0f04ad0d62d6a4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page