Skip to main content

A MongoDB ORM for Python

Project description

mongo-orm

A MongoDB ORM for Python

Usage

Defining an Entity

Use the @entity decorator to map a class to a MongoDB collection. Your class should inherit from BaseEntity.

from mongo_orm import BaseEntity, entity

@entity("users")
class User(BaseEntity):
    name: str
    email: str
    
    # Define indexes
    __indexes__ = [
        {"keys": [("email", 1)], "unique": True},
        {"keys": [("name", 1)]}
    ]

Index Management

The ORM can automatically manage your MongoDB indexes based on the __indexes__ attribute. This behavior is controlled by the MONGODB_INDEX_AUTOAPPLY environment variable.

Configuration (MONGODB_INDEX_AUTOAPPLY)

  • never (default): No automatic index management.
  • always: Checks and applies indexes every time an entity class is initialized (on startup).
  • auto-lock: Checks and applies indexes once. After a successful check, it writes a hash of the index to mongo-orm.lock. Subsequent startups will skip the check if the hash is present in the lock file.

Manual Index Application

You can also trigger index application manually for all registered entities:

from mongo_orm.utils import apply_all_indexes

# Apply indexes using the current environment configuration
apply_all_indexes()

# Or force a specific mode
apply_all_indexes(mode="always")

Development

# Install dependencies
poetry install

# Run tests
poetry run pytest

# Format code
poetry run black .
poetry run isort .

# Type checking
poetry run mypy src/

License

MIT

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

mongo_entity_orm-0.1.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

mongo_entity_orm-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file mongo_entity_orm-0.1.1.tar.gz.

File metadata

  • Download URL: mongo_entity_orm-0.1.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.3 Darwin/24.2.0

File hashes

Hashes for mongo_entity_orm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 82d6c765ed18927c2693558205ece053cc4d61855df561a3aa754fbc240f86ca
MD5 22df1b966dbba03939582e294eb24420
BLAKE2b-256 d97be092f1b0fb52e11a95e37d803735c11962aa3e05c82f27b4a86e4900096f

See more details on using hashes here.

File details

Details for the file mongo_entity_orm-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: mongo_entity_orm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.3 Darwin/24.2.0

File hashes

Hashes for mongo_entity_orm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 52960707fb5c02601c52897c936468e84ea4b506f6baf8bd95e0c9a0743e5ff5
MD5 3ef5ae9f8c4f4e20fe0773ac4455b497
BLAKE2b-256 2b68f8d84e4277cefceca5a03e641cef94173a765eebb5548ddbbaf55b16239d

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