Skip to content

SweetIQ/tx-warmongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

Based on [https://github.com/robbrit/warmongo], Twisted Warmongo uses a Twisted-based asynchronous connector to MongoDB to give better throughput.

Documentation from Warmongo

This extends the JSON schema by supporting extra BSON types:

  • ObjectId - use the "object_id" type in your JSON schema to validate that a field is a valid ObjectId.
  • datetime - use the "date" type in your JSON schema to validate that a field is a valid datetime

Usage

Note: most of the calls in tx-warmongo are asynchronous calls and return a Deferred object. It is highly advised to use defer.inlineCallbacks with your code:

@defer.inlineCallbacks
def foo():
    results = yield Country.find_all()

    for result in results:
        result.field = "some value"
        yield result.save()
  1. Build your schema

    schema = { 'name': 'Country', 'properties': { 'name': {'type': 'string'}, 'abbreviation': {'type': 'string'}, }, 'additionalProperties': False, }

  2. Connect to your database

    import txwarmongo yield txwarmongo.connect("test")

  3. Create a model

    Country = txwarmongo.model_factory(schema)

  4. Create an object using your model

    sweden = Country({"name": 'Sweden', "abbreviation": 'SE'}) yield sweden.save() sweden._id ObjectId('50b506916ee7d81d42ca2190')

  5. Let the object validate itself!

    sweden = Country.find_one({"name" : "Sweden"}) sweden.name = 5 Traceback (most recent call last): File "", line 1, in File "warmongo/model.py", line 254, in setattr self.validate_field(attr, self._schema["properties"][attr], value) File "warmongo/model.py", line 189, in validate_field self.validate_simple(key, value_schema, value) File "warmongo/model.py", line 236, in validate_simple (key, value_type, str(value), type(value))) warmongo.exceptions.ValidationError: Field 'name' is of type 'string', received '5' (<type 'int'>)

    sweden.overlord = 'Bears' Traceback (most recent call last): File "", line 1, in File "warmongo/model.py", line 257, in setattr raise ValidationError("Additional property '%s' not allowed!" % attr) warmongo.exceptions.ValidationError: Additional property 'overlord' not allowed!

Choosing a collection

By default Warmongo will use the pluralized version of the model's name. If you want to use something else, put it in the JSON-schema:

{
    "name": "MyModel",
    ...
    "collectionName": "some_collection",
    ...
}

Multiple Databases

To use multiple databases, simply call connect() multiple times:

>>> import txwarmongo
>>> yield txwarmongo.connect("test")
>>> yield txwarmongo.connect("other_db")

By default all models will use the first database specified. If you want to use a different one, put it in the JSON-schema:

{
    "name": "MyModel",
    ...
    "databaseName": "other_db",
    ...
}

Licence

Apache Version 2.0

Production Examples

I use warmongo every day at my startup http://www.sweetiq.com/ to share data definitions between our Python and Node.js applications. It has been running in production for some time now, so it has been reasonably tested for robustness and performance.

About

Twisted Implementation of Warmongo

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages