Skip to main content

Beanie (Motor / MongoDB) async backend for varco — runtime ORM generation, repository, and UoW

Project description

varco-beanie

PyPI version Python License: Apache 2.0 GitHub

Beanie (Motor / MongoDB) async backend for varco.

Generates Beanie Document classes at runtime from your DomainModel subclasses — no hand-written Document models needed. Requires varco-core.


Install

pip install varco-beanie

Requirements

  • Python ≥ 3.12
  • MongoDB ≥ 4.0
  • For multi-document transactions: a MongoDB replica set or sharded cluster

Features

  • Zero-boilerplate ODMBeanieModelFactory generates Document subclasses at runtime from your DomainModel classes; no duplication
  • Full repositoryAsyncBeanieRepository implements AsyncRepository (CRUD, exists(), stream_by_query())
  • Unit of WorkBeanieUnitOfWork wraps Motor session lifecycle with optional transactions
  • One-liner bootstrapBeanieRepositoryProvider + BeanieFastrestApp wire everything including init_beanie()
  • Query integration — accepts varco-core QueryParams / QueryBuilder AST natively

What's in the package

Module Purpose
factory.py BeanieModelFactory — generates Document subclasses; BeanieDocRegistry — escape hatch to access the generated Document
repository.py AsyncBeanieRepository — Motor-backed CRUD + exists() + stream_by_query()
uow.py BeanieUnitOfWork — Motor session lifecycle (optional transactions)
provider.py BeanieRepositoryProvider — wires factory + repos + UoW + init_beanie()
bootstrap.py BeanieConfig, BeanieFastrestApp — one-liner app setup

Quick start

Bootstrap (one-liner)

from motor.motor_asyncio import AsyncIOMotorClient
from varco_beanie import BeanieConfig, BeanieFastrestApp

client = AsyncIOMotorClient("mongodb://localhost:27017")

app = BeanieFastrestApp(BeanieConfig(
    motor_client=client,
    db_name="myapp",
    entity_classes=(User, Post),
))

await app.init()                    # calls beanie.init_beanie() internally
uow_provider = app.uow_provider     # ready to inject as IUoWProvider

Manual setup

from varco_beanie import BeanieRepositoryProvider

provider = BeanieRepositoryProvider(motor_client=client, db_name="myapp")
provider.register(User, Post)
await provider.init()

async with provider.make_uow() as uow:
    user = await uow.users.save(User(name="Edo", email="edo@example.com"))
    print(user.pk)

Transactions (replica set required)

provider = BeanieRepositoryProvider(
    motor_client=client,
    db_name="myapp",
    transactional=True,   # wraps each UoW in a Motor session transaction
)

Query integration

from varco_core import QueryBuilder, QueryParams

async with provider.make_uow() as uow:
    # exists() — uses .count(), no document load
    if await uow.posts.exists(post_id):
        ...

    # stream_by_query() — Motor batches internally, bounded memory
    params = QueryParams(node=QueryBuilder().eq("published", True).build())
    async for post in uow.posts.stream_by_query(params):
        await process(post)

Access the generated Beanie Document (escape hatch)

from varco_beanie import BeanieDocRegistry

PostDoc = BeanieDocRegistry.get(Post)
# use PostDoc for Beanie-specific operations not exposed by the repository

Notes

  • CheckConstraint entries in varco-core metadata are silently ignored — MongoDB has no SQL CHECK constraints. Use Pydantic validators instead.
  • Foreign key hints are metadata only — MongoDB has no FK enforcement.
  • Composite PKs are emulated via compound unique indexes; find_by_id(pk_tuple) issues a find_one with a composite filter.

Related packages

Package Description
varco-core Domain model, service layer, query AST, JWT — required dependency
varco-sa SQLAlchemy async backend (alternative to this package)

Links

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

varco_beanie-1.1.0.tar.gz (110.2 kB view details)

Uploaded Source

Built Distribution

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

varco_beanie-1.1.0-py3-none-any.whl (82.0 kB view details)

Uploaded Python 3

File details

Details for the file varco_beanie-1.1.0.tar.gz.

File metadata

  • Download URL: varco_beanie-1.1.0.tar.gz
  • Upload date:
  • Size: 110.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for varco_beanie-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8042e2ecc457b798b225bbef6efbb7fca8b1ecb2f7dda74ed9d80294cac568c4
MD5 ed192d8e524d512638cceaf8b1a9cece
BLAKE2b-256 32dedadea8fd529e34d89c8a3415b79616d095402985e5cfd347e4f798dde299

See more details on using hashes here.

File details

Details for the file varco_beanie-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: varco_beanie-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 82.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for varco_beanie-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb1efca15c8061707031654dfe1ae7267c5adcfa691690c60c40e8b257c70b6d
MD5 057c71bb0233fbadaba767bc470c53d5
BLAKE2b-256 26864585c79a099ad8a4a7af3e240f9d94d3fccfd714d4265c0cd914028810cd

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