mongey


Namemongey JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
Summaryasynchronous MongoDB ORM
upload_time2024-06-09 13:07:34
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT
keywords pymongo mongodb motor asyncio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Mongey

Mongey is a successor of [Glasskit MongoDB ORM](https://gitlab.com/viert/glasskit) 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

```python
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.
 

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mongey",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "pymongo, mongodb, motor, asyncio",
    "author": null,
    "author_email": "Pavel Vorobyev <aquavitale@yandex.ru>",
    "download_url": "https://files.pythonhosted.org/packages/c0/f4/89bd9f86810fc7bf53ebd25534e4ff01b4f9934faac34c9a05e1df23e819/mongey-0.1.4.tar.gz",
    "platform": null,
    "description": "## Mongey\n\nMongey is a successor of [Glasskit MongoDB ORM](https://gitlab.com/viert/glasskit) based on Motor MongoDB driver.\nUnlike GlassKit, Mongey is a stand-alone module detached from any web framework.\n\nMongey is based on Motor AsyncIO, meaning it is fully asynchronous.\n\n### Example\n\n```python\nimport asyncio\nfrom mongey.context import ctx\nfrom mongey.models import StorableModel\nfrom mongey.models.fields import StringField, IntField, DictField\nfrom mongey.decorators import api_field\n\n\nclass User(StorableModel):\n    COLLECTION = \"users\"\n    KEY_FIELD = \"username\"\n\n    username = StringField(required=True)\n    first_name = StringField(required=True)\n    last_name = StringField(required=True)\n    age = IntField(default=None)\n    user_settings = DictField(default=dict)\n\n    @api_field\n    def full_name(self) -> str:\n        return f\"{self.first_name} {self.last_name}\"\n\n\nasync def run():\n    ctx.setup_db({\"meta\": {\"uri\": \"mongodb://127.0.0.1:27017/mydb\", \"kwargs\": {}}, \"shards\": {}})\n    user = User({\"username\": \"superuser\", \"first_name\": \"Joe\", \"last_name\": \"White\"})\n    await user.save()\n\n\nif __name__ == \"__main__\":\n    asyncio.run(run())\n\n```\n\n### Context\n\nMongey context is a global variable holding configuration bits and the global database object.\nGlobal context allows Mongey to behave more like ActiveRecord rather than Django ORM or SQLAlchemy.\nIn other words you do `user.save()` instead of `db.save(user)`.\n\n\n### Configuration\n\nThe global context object is created on import but stays not configured until you do so explicitly.\n\nLogging and caching have their default versions and are pre-configured for you while `db` does not.\n\nUse `ctx.setup_db(...)` to configure the database when your application starts, accessing the `db` property\nprior to database configuration will raise a `ConfigurationError` exception.\n\n\n### Caching\n\nPersistent models, like `StorableModel` have `cache_get` method along with the original `get`. This method\nfetches the model and if it succeeds, the model is stored to level1+level2 caches. \n\nL1 cache is usually request local while the L2 is more \"persistent\", e.g. stored in memcached. \n\nIf you're developing a web app, this allows Mongey to get the same model\nmultiple times within one web request quickly and \"for free\" from your app memory,\nwhile for new requests the L2 cache will be used.\n\nCache invalidation is a complex topic being considered one of the main problems in coding (along with naming of variables)\nso this is to be covered in a full documentation which is currently WIP.\n\n### Computed fields\n\n`@api_field` decorator can be added to arg-less sync or async methods of your model which will expose the method\nto the sync `to_dict()` and async `to_dict_ext()` methods of the model which are the primary methods for further \nmodel serialization.\n \n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "asynchronous MongoDB ORM",
    "version": "0.1.4",
    "project_urls": {
        "Source": "https://github.com/viert/mongey"
    },
    "split_keywords": [
        "pymongo",
        " mongodb",
        " motor",
        " asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ec1f5ff9b93c6f9db2ed5221cd49efdc900140dfcb898e6a25169b3d5a65107a",
                "md5": "e7c94c115f28a5646770c08bbf48ad44",
                "sha256": "ad4c67b2b38f23f9b632748043c0b05346609ef79738fe134acfbde5f6cd1741"
            },
            "downloads": -1,
            "filename": "mongey-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e7c94c115f28a5646770c08bbf48ad44",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 36578,
            "upload_time": "2024-06-09T13:07:33",
            "upload_time_iso_8601": "2024-06-09T13:07:33.231026Z",
            "url": "https://files.pythonhosted.org/packages/ec/1f/5ff9b93c6f9db2ed5221cd49efdc900140dfcb898e6a25169b3d5a65107a/mongey-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c0f489bd9f86810fc7bf53ebd25534e4ff01b4f9934faac34c9a05e1df23e819",
                "md5": "bd5124fb4fe3f4454a95235a86938d8a",
                "sha256": "94f2d826904c9be02c928e54d008f88f228295c7ae194f61011862ce7d2696a0"
            },
            "downloads": -1,
            "filename": "mongey-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "bd5124fb4fe3f4454a95235a86938d8a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 28712,
            "upload_time": "2024-06-09T13:07:34",
            "upload_time_iso_8601": "2024-06-09T13:07:34.189534Z",
            "url": "https://files.pythonhosted.org/packages/c0/f4/89bd9f86810fc7bf53ebd25534e4ff01b4f9934faac34c9a05e1df23e819/mongey-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-09 13:07:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "viert",
    "github_project": "mongey",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "mongey"
}
        
Elapsed time: 0.63462s