Skip to main content

Mongey

Mongey is a successor of Glasskit MongoDB ORM based on Motor MongoDB driver. Unlike GlassKit, Mongey is a stand-alone module detached from any web framework.

Mongey is based on Motor AsyncIO, meaning it is fully asynchronous.

Example

import asyncio
from mongey.context import ctx
from mongey.models import StorableModel
from mongey.models.fields import StringField, IntField, DictField
from mongey.decorators import api_field


class User(StorableModel):
    COLLECTION = "users"
    KEY_FIELD = "username"

    username = StringField(required=True)
    first_name = StringField(required=True)
    last_name = StringField(required=True)
    age = IntField(default=None)
    user_settings = DictField(default=dict)

    @api_field
    def full_name(self) -> str:
        return f"{self.first_name} {self.last_name}"


async def run():
    ctx.setup_db({"meta": {"uri": "mongodb://127.0.0.1:27017/mydb", "kwargs": {}}, "shards": {}})
    user = User({"username": "superuser", "first_name": "Joe", "last_name": "White"})
    await user.save()


if __name__ == "__main__":
    asyncio.run(run())

Context

Mongey context is a global variable holding configuration bits and the global database object. Global context allows Mongey to behave more like ActiveRecord rather than Django ORM or SQLAlchemy. In other words you do user.save() instead of db.save(user).

Configuration

The global context object is created on import but stays not configured until you do so explicitly.

Logging and caching have their default versions and are pre-configured for you while db does not.

Use ctx.setup_db(...) to configure the database when your application starts, accessing the db property prior to database configuration will raise a ConfigurationError exception.

Caching

Persistent models, like StorableModel have cache_get method along with the original get. This method fetches the model and if it succeeds, the model is stored to level1+level2 caches.

L1 cache is usually request local while the L2 is more "persistent", e.g. stored in memcached.

If you're developing a web app, this allows Mongey to get the same model multiple times within one web request quickly and "for free" from your app memory, while for new requests the L2 cache will be used.

Cache invalidation is a complex topic being considered one of the main problems in coding (along with naming of variables) so this is to be covered in a full documentation which is currently WIP.

Computed fields

@api_field decorator can be added to arg-less sync or async methods of your model which will expose the method to the sync to_dict() and async to_dict_ext() methods of the model which are the primary methods for further model serialization.

Metadata

Release files for mongey 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongey 0.1.10
File Size Uploaded
mongey-0.1.10.tar.gz 28.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mongey 0.1.10
File Interpreter ABI Platform
mongey-0.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 65.6 kB

Release files / mongey-0.1.10.tar.gz

Download URL mongey-0.1.10.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b292fa6da774ced2e5758bf8b06dadca9a6b994502ccc7ab092d455d06a9b8ea
BLAKE2b-256 checksum
How to use checksums
b6c277ab4a3e00591339f7474526d200d3bb1930ab496c42be73dd89a7b2d43a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / mongey-0.1.10-py3-none-any.whl

Download URL mongey-0.1.10-py3-none-any.whl
Size 36.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
46415069eaf987a71f655d571e81a2701589c9edb7a33c3a3c7a362505b0d65b
BLAKE2b-256 checksum
How to use checksums
709357886193747f59dfa8a06359bd2dc516df2c264bf5fc8afbb8f2702bd2cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.1.10 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page