mongo-objects


Namemongo-objects JSON
Version 1.1.2 PyPI version JSON
download
home_pageNone
SummaryAccess MongoDB documents and subdocuments through user-defined UserDict and proxy objects.
upload_time2024-04-27 00:10:30
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License
keywords mongo mongodb pymongo
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            *************
mongo_objects
*************

A lightweight wrapper around pymongo to access MongoDB documents and subdocuments through custom user-defined classes.

Documents are returned as UserDict subclasses:

* convenient pass-through to ``find()`` and ``find_one()``
* convenient ``load_by_id()`` to locate documents by ObjectId
* smart ``save()`` function to insert, upsert or replace documents as appropriate
* automatically record document creation and update times
* track separate object schema versions
* support polymorphic user objects loaded from the same collection

Subdocuments are accessed through dictionary proxy objects:

* returned as their own classes independent of the parent MongoDB document class
* data access is proxied back to the parent so no separate database access is performed
* subdocuments have their own unique URL-safe ID useful for loading the data
* subdocuments can be grouped in either dictionary or list containers
* polymorphic subdocuments are supported within the same container
* using subdocuments avoids "JOIN-like" additional database queries across collections


Example
=======

Imagine an event ticketing system with a single MongoDB collection containing documents like the following::

    {
        'name' : 'Fabulous Event',
        'date' : '...',      # datetime
        'desc' : 'This will be a lot of fun'
        'ticketTypes' : {
            '1' : {
                'name' : 'VIP Ticket',
                'desc' : 'Front-row seating; comes with a free plushie',
                'cost' : 200,
                'quantity' : 10,
            },
            '2' : {
                'name' : 'General Seating',
                'desc' : 'Everyone is welcome!',
                'cost' : 100,
                'quantity' : 100,
            },
        },
        'tickets' : [
            {
                'holder' : 'Fred',
                'purchased' : '...'      # datetime
                'ticketType' : 2,
            },
            {
                'holder' : 'Susan',
                'purchased' : '...'      # datetime
                'ticketType' : 1,
            },
        ]
    }


MongoUserDict
-------------

``mongo_objects`` allows us to create our own class for viewing these event documents::

    class Event( mongo_objects.MongoUserDict ):

        db = ...     # provide your MongoDB database object here
        collection_name = 'events'

        def isFuture( self ):
            return self['date'] >= datetime.utcnow()

Loop through all events::

    for event in Event.find():
        ...

Create a new event::

    myevent = Event( {
        'name' : '...',
        'date' : '...',
    } )
    myevent.save()

Record the unique ID (ObjectId) of an event::

    eventId = myevent.id()

Locate an event by its ID::

    myevent = Event.load_by_id( eventId )

Call a method on our custom object::

    myevent.isFuture()



MongoDictProxy
--------------

``mongo_objects`` allows us to create additional proxy classes for managing subdocuments. The proxy classes
behave like dictionaries but redirect all access back to the parent MongoUserDict object. No additional
database access is performed.::

    class TicketTypes( mongo_objects.MongoDictProxy ):
        container_name = 'ticketTypes'

First load an Event document object::

    event = Event.find_one()

Loop through the existing ticket type subdocuments within the parent ``Event``::

    for tt in TicketTypes.get_proxies( event ):
        ...

Obtain a specific proxy by key::

    tt = TicketType.get_proxy( event, '1' )

Get the unique ID of a proxy item::

    ticket_type_id = tt.id()

Loading a proxy object by ID is a classmethod of the parent document class;
the proxy can only exist once the parent document is loaded::

    tt = Event.load_proxy_by_id( ticket_type_id, TicketTypes )

Create a new ticket type. A unique per-document key will be assigned automatically::

    TicketType.create( event, {
        'name' : 'Student Ticket',
        'desc' : 'For our student friends',
        'cost' : 50,
        'quantity' : 25,
    } )


Credits
-------

Development sponsored by `Headwaters Entrepreneurs Pte Ltd <https://headwaters.com.sg>`_.

Originally developed by `Frontier Tech Team LLC <https://frontiertechteam.com>`_
for the `Wasted Minutes <https://wasted-minutes.com>`_ ™️ language study tool.


License
-------
mongo_objects is made available to the community under the "MIT License".

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "mongo-objects",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "Mongo, MongoDB, pymongo",
    "author": null,
    "author_email": "Jonathan Lindstrom <lindstrom.j@headwaters.com.sg>",
    "download_url": "https://files.pythonhosted.org/packages/17/e6/e5d4a63661a1cfff56e7df2acf7912fb55ef0ce856ae6ea0f6d00e23177f/mongo_objects-1.1.2.tar.gz",
    "platform": null,
    "description": "*************\nmongo_objects\n*************\n\nA lightweight wrapper around pymongo to access MongoDB documents and subdocuments through custom user-defined classes.\n\nDocuments are returned as UserDict subclasses:\n\n* convenient pass-through to ``find()`` and ``find_one()``\n* convenient ``load_by_id()`` to locate documents by ObjectId\n* smart ``save()`` function to insert, upsert or replace documents as appropriate\n* automatically record document creation and update times\n* track separate object schema versions\n* support polymorphic user objects loaded from the same collection\n\nSubdocuments are accessed through dictionary proxy objects:\n\n* returned as their own classes independent of the parent MongoDB document class\n* data access is proxied back to the parent so no separate database access is performed\n* subdocuments have their own unique URL-safe ID useful for loading the data\n* subdocuments can be grouped in either dictionary or list containers\n* polymorphic subdocuments are supported within the same container\n* using subdocuments avoids \"JOIN-like\" additional database queries across collections\n\n\nExample\n=======\n\nImagine an event ticketing system with a single MongoDB collection containing documents like the following::\n\n    {\n        'name' : 'Fabulous Event',\n        'date' : '...',      # datetime\n        'desc' : 'This will be a lot of fun'\n        'ticketTypes' : {\n            '1' : {\n                'name' : 'VIP Ticket',\n                'desc' : 'Front-row seating; comes with a free plushie',\n                'cost' : 200,\n                'quantity' : 10,\n            },\n            '2' : {\n                'name' : 'General Seating',\n                'desc' : 'Everyone is welcome!',\n                'cost' : 100,\n                'quantity' : 100,\n            },\n        },\n        'tickets' : [\n            {\n                'holder' : 'Fred',\n                'purchased' : '...'      # datetime\n                'ticketType' : 2,\n            },\n            {\n                'holder' : 'Susan',\n                'purchased' : '...'      # datetime\n                'ticketType' : 1,\n            },\n        ]\n    }\n\n\nMongoUserDict\n-------------\n\n``mongo_objects`` allows us to create our own class for viewing these event documents::\n\n    class Event( mongo_objects.MongoUserDict ):\n\n        db = ...     # provide your MongoDB database object here\n        collection_name = 'events'\n\n        def isFuture( self ):\n            return self['date'] >= datetime.utcnow()\n\nLoop through all events::\n\n    for event in Event.find():\n        ...\n\nCreate a new event::\n\n    myevent = Event( {\n        'name' : '...',\n        'date' : '...',\n    } )\n    myevent.save()\n\nRecord the unique ID (ObjectId) of an event::\n\n    eventId = myevent.id()\n\nLocate an event by its ID::\n\n    myevent = Event.load_by_id( eventId )\n\nCall a method on our custom object::\n\n    myevent.isFuture()\n\n\n\nMongoDictProxy\n--------------\n\n``mongo_objects`` allows us to create additional proxy classes for managing subdocuments. The proxy classes\nbehave like dictionaries but redirect all access back to the parent MongoUserDict object. No additional\ndatabase access is performed.::\n\n    class TicketTypes( mongo_objects.MongoDictProxy ):\n        container_name = 'ticketTypes'\n\nFirst load an Event document object::\n\n    event = Event.find_one()\n\nLoop through the existing ticket type subdocuments within the parent ``Event``::\n\n    for tt in TicketTypes.get_proxies( event ):\n        ...\n\nObtain a specific proxy by key::\n\n    tt = TicketType.get_proxy( event, '1' )\n\nGet the unique ID of a proxy item::\n\n    ticket_type_id = tt.id()\n\nLoading a proxy object by ID is a classmethod of the parent document class;\nthe proxy can only exist once the parent document is loaded::\n\n    tt = Event.load_proxy_by_id( ticket_type_id, TicketTypes )\n\nCreate a new ticket type. A unique per-document key will be assigned automatically::\n\n    TicketType.create( event, {\n        'name' : 'Student Ticket',\n        'desc' : 'For our student friends',\n        'cost' : 50,\n        'quantity' : 25,\n    } )\n\n\nCredits\n-------\n\nDevelopment sponsored by `Headwaters Entrepreneurs Pte Ltd <https://headwaters.com.sg>`_.\n\nOriginally developed by `Frontier Tech Team LLC <https://frontiertechteam.com>`_\nfor the `Wasted Minutes <https://wasted-minutes.com>`_ \u2122\ufe0f language study tool.\n\n\nLicense\n-------\nmongo_objects is made available to the community under the \"MIT License\".\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Access MongoDB documents and subdocuments through user-defined UserDict and proxy objects.",
    "version": "1.1.2",
    "project_urls": {
        "Documentation": "https://mongo-objects.headwaters.com.sg/en/latest/",
        "Homepage": "https://github.com/lindstrom-j/mongo_objects",
        "Issues": "https://github.com/lindstrom-j/mongo_objects/issues"
    },
    "split_keywords": [
        "mongo",
        " mongodb",
        " pymongo"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "582dc45455c6272307d58d207c16456550cda81f6e78dd7b3234fd455c4d6417",
                "md5": "755ba0c2d76f8986ddc4ba0209638211",
                "sha256": "d76981cc347cc610b024031efed8f7df70c2e2bdd5a81d4a20e1e32c7692d807"
            },
            "downloads": -1,
            "filename": "mongo_objects-1.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "755ba0c2d76f8986ddc4ba0209638211",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 10999,
            "upload_time": "2024-04-27T00:10:28",
            "upload_time_iso_8601": "2024-04-27T00:10:28.684647Z",
            "url": "https://files.pythonhosted.org/packages/58/2d/c45455c6272307d58d207c16456550cda81f6e78dd7b3234fd455c4d6417/mongo_objects-1.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "17e6e5d4a63661a1cfff56e7df2acf7912fb55ef0ce856ae6ea0f6d00e23177f",
                "md5": "2eca96238913637525b887ce2c99c0a0",
                "sha256": "908e92a69675fc172d5639d12eb3a6e4f3e69e8ec1c10c4429839f408224f0f7"
            },
            "downloads": -1,
            "filename": "mongo_objects-1.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "2eca96238913637525b887ce2c99c0a0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 34902,
            "upload_time": "2024-04-27T00:10:30",
            "upload_time_iso_8601": "2024-04-27T00:10:30.855505Z",
            "url": "https://files.pythonhosted.org/packages/17/e6/e5d4a63661a1cfff56e7df2acf7912fb55ef0ce856ae6ea0f6d00e23177f/mongo_objects-1.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-27 00:10:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lindstrom-j",
    "github_project": "mongo_objects",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "mongo-objects"
}
        
Elapsed time: 0.23602s