Source

Mongolore /

Filename Size Date modified Message
mongolore
88 B
18 B
3.9 KB
334 B
8.4 KB

Overview

Mongolore is an ORM-like wrapper for MongoDB.

It is heavily based on python-couchdb mapping module by Christopher Lenz. I just did some adjustments for MongoDB and validation support.

Features

  1. Schema declaration Including complex nested objects.

  2. Validation Custom validators support. Validation is only done on object save. Inspired by WTForms.

  3. Auto-refs Dereference on first access only.

  4. Embedded objects (without dbrefs) To reuse repeated pattern in your objects. Look at example below.

  5. Django-like managers Without changes they are just wrappers around pytmongo.Collection, but they allow you to encapsulate your own complex queries.

  6. Small codebase Thin wrapper around pymongo. Just 5 files, most fit one screen.

Simple usage:

from mongolore import *
connection = Connection()
db = Database(connection, 'mongolore_test_db')

# for a complete list of validators and fields see validators.py and mapping.py
@db.register
class Player(Document):
    name = TextField(validators=[Required()])
    contact = DictField(Mapping.build(
        email = TextField(validators=[Required()]),
        addr = TextField(),
        phones = ListField(TextField(), validators=[Length(max=2)]),
    ))

p = Player(name='mongo')
p.contact.email = 'test@gmail.com'
p.contact.addr = 'address'
p.contact.phones = ['first', 'second']
p.save()

p = Player.objects.find_one()

Inline validators:

@db.register
class Player(Document):
    name = TextField(validators=[Required()])

    def validate_name(self, value):
        if not value == 'mongo':
            raise ValidationError()

Embedded objects with dbref:

    @db.register
    class A(Document):
        name = TextField()

    @db.register
    class B(Document):
        name = TextField()
        embedded = DBRefField(A)

    a = A(name='mongoa')
    a.save()

    b = B(name='mongob', embedded=a)
    assert isinstance(b.embedded, A)
    b.save()
    b = B.objects.find_one()
    # note that DBRef is dereferenced on first access only
    assert isinstance(b.embedded, A)

Embedded objects without dbref:

    class Contact(Mapping):
        email = TextField()
        addr = TextField()

        def validate_email(self, value):
            pass

        def __unicode__(self):
            pass

    @db.register
    class Player(Document):
        name = TextField()
        # contact is just and embedded dictionary
        # useful for encapsulation and reusability
        contact = DictField(Contact)

    p = Player(name='12')
    # you can set contact using Contact instance
    p.contact = Contact(email='', addr='')
    # or dictionary
    p.contact = {'email': '', 'addr': ''}
    # result is the same
    assert isinstance(p.contact, Contact)

Managers and queriyng database:

    # you can use document's manager
    # manager is just a wrapper around pymongo.collection, syntax is the same
    Player.objects.find({'name': 'mongo'})

    # you can also use original pymongo interface, objects will be converted
    # document is saved to collection named after classname
    db.player.filter() #.insert() ...

    # you can define your managers too
    class CustomManager(Manager):
        def startswith(self, s):
            return self.find({'name': {'$regex': r'^%s' % s}})

    @db.register
    class Player(Document):
        name = TextField()

        custom = CustomManager()

    # 'objects' manager is always created
    assert hasattr(Player, 'objects')
    p = Player(name='mongodb')
    p.save()
    assert p.custom.startswith('mongo').count() == 1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.