Use this file to discover all available pages before exploring further.
HexCore’s BeanieRepository (also exported as BeanieODMCommonImplementationsRepo) provides a ready-made CRUD implementation for MongoDB using the Beanie ODM. This page explains how to define a BaseDocument subclass, implement a concrete repository, register all documents at startup with init_beanie_documents, and handle complex fields with resolvers and serializers.
BaseDocument maps entity_id to your entity’s domain id. The MongoDB document _id is an auto-generated ObjectId managed by Beanie; entity_id is the UUID that HexCore uses to query and convert records.
from beanie import Indexedfrom hexcore.infrastructure.repositories.orms.beanie import BaseDocumentimport typing as tclass UserDocument(BaseDocument): name: str email: t.Annotated[str, Indexed(unique=True)] role: str = "member" class Settings: name = "users" # MongoDB collection name
Do not declare entity_id, created_at, updated_at, or is_active in your subclass — they are already defined on BaseDocument. The @after_event([Save]) hook on BaseDocument automatically updates updated_at on every save.
Returns all documents. limit_offset_pagination defaults to True, so limit and offset are applied automatically. Set it to False on the repository class to always return every document.
Executes a filtered, sorted, paginated MongoDB query. Returns (items, total_count). Supports all FilterOperator values including CONTAINS, STARTSWITH, IN, and more.
Decorated with @register_entity_on_uow. Upserts the document (creates on first call, updates on subsequent calls matching entity_id). Returns the refreshed entity.
Call init_beanie_documents() once during application startup. It auto-discovers every concrete BaseDocument subclass in your codebase and initialises Beanie with the MongoDB connection from ServerConfig.mongo_uri:
init_beanie_documents uses get_all_concrete_subclasses(BaseDocument) internally to collect every non-abstract BaseDocument subclass that has been imported at the time it is called. Import your document modules before calling it (or rely on HexCore’s repository discovery to import them automatically).
Resolvers translate raw document values into richer entity types after a document is loaded. Each entry maps an entity field name to a (source_field, async_resolver_fn) tuple. The resolver receives the full document instance.
Serializers convert complex entity fields to MongoDB-storable primitives before the document is saved. Each entry maps an entity field to a (dest_field, sync_serializer_fn) tuple.
fields_resolvers runs on read (Document → Entity). fields_serializers runs on write (Entity → Document). Use @cycle_protection_resolver for resolvers that load related entities to avoid infinite recursion in bidirectional relationships.