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-0.1.0.tar.gz (69.9 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-0.1.0-py3-none-any.whl (50.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: varco_beanie-0.1.0.tar.gz
  • Upload date:
  • Size: 69.9 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-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d2671ba9819c16b1ca28da0770e8a85a8b852410ab2fa38c3e768edd8c15371a
MD5 a6e7a18755edea9f032b5c20787f4bd5
BLAKE2b-256 c11ba7d8377d24b3b3911f6a588d8e1bb7880fde006dbd56ccb7c01943e40a8e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: varco_beanie-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 50.3 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-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dcd02a3203c3238fe19bbd0b2d0d84ae1c073c436139ab488958fc4f4f0562e3
MD5 cce7075158d83deb4f209ed070e2e925
BLAKE2b-256 2f592417541c190067c5c070c55a4897334d91bed0f568595142a8daed873115

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