Skip to main content

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

Project description

gel-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 gel-python library.

For legacy EdgeDB <v6 support, check the last edgedb-pydantic-codegen release and its PyPI page.

Usage

In an Gel initialized project, simply run

uvx gel-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 gel 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

gel_pydantic_codegen-1.0.1.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gel_pydantic_codegen-1.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file gel_pydantic_codegen-1.0.1.tar.gz.

File metadata

File hashes

Hashes for gel_pydantic_codegen-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a524797291a99584133ed5544fdf997bce986739512b97235c6e493880b31978
MD5 fb173d1377576b554197f2a297378037
BLAKE2b-256 047047ea39846c538442bb41d86d6e4fe08fab9e340ccfe2f48b5c50841068af

See more details on using hashes here.

File details

Details for the file gel_pydantic_codegen-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gel_pydantic_codegen-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 feebfa1c7b481b41db8daf829e146205ff093a9f560f2528e9b0df272884b894
MD5 540b0be46a07af0038967cfebeeb2ac8
BLAKE2b-256 1eb3a822a61f596fb36d655256b68c21e6d1591c1ae3208bb13bcc24c2a0e579

See more details on using hashes here.

Supported by

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