Mokito is an asynchronous ORM for working with MongoDB inside a Tornado app
Project description
# mokito
An asynchronous ORM for accessing MongoDB in Tornado
## What is mokito?
(MOngodb + [mongoKIt](https://github.com/namlook/mongokit) + TOrnado) is an asynchronous toolkit for working with ``mongodb`` inside a ``tornado`` app, like ``mongokit``. Mokito has a pure implementation of python + tornado and only depends on tornado and bson (provided by pymongo)
## Why not pymongo?
[PyMongo](http://api.mongodb.org/python/current/) is the recommended way to work with MongoDB in Python, but isn't asynchronous and not run inside tornado's IOLoop. If you use pymongo you won't take the advantages of tornado.
## Why not motor?
[Motor](http://emptysquare.net/motor/) wraps PyMongo and makes it async with greenlet. This is a great project, but it uses greenlet. If you can use greenlets why not use gevent instead of tornado? PyMongo already works with gevent. If you are using a very powerfull non-blocking web server with a pure python code, you'll probably want to work with a pure tornado driver for accessing mongo.
## Features
* validation and conversion of data to the specified type
* support for unstructured data
* dot notation
* control over data presentation
* control over data validation
* mapping onto the same document of models with different schemes
## Installing
```bash
pip install pymomgo tornado
pip install mokito
```
## Simple usage
```python
from tornado.web import RequestHandler
from tornado.gen import coroutine
import mokito
class MainHandler(RequestHandler):
def initialize(self):
self.db = mokito.Client("db_name", "mongodb://127.0.0.1:27017")
@gen.coroutine
def get(self, user_id):
user = yield self.db.user.find_one(user_id)
self.render("index.html", user=user)
```
## Using ORM
A Document declaration look as follows:
```python
from mokito import Document
class BlogPost(Document):
fields = {
'title':str,
'body':str,
'author':str
}
blogpost = BlogPost(title='my title', body='a body', author='unknown', foo='bar')
blogpost['author'] = 'me'
yield blogpost.save()
```
MongoDB in the collection "blog_post" will write this document:
```javascript
{"_id": ObjectId("..."), "body": "a body", "author": "me", "title": "my title"}
```
The field "foo" will not be saved. More about this you can read in [wiki](https://github.com/asmodius/mokito/wiki).
And you can use a more complex structure as follows:
```python
class ComplexDoc(Document):
__uri__ = "mongodb://127.0.0.1:27017"
__database__ = 'test'
__collection__ = 'example'
fields = {
"f_1": None,
"f_2": int,
"f_3": float,
"f_4": dict,
"f_5": [str],
"f_6": (int, str),
"f_7": {
"x_1": {'a': int, 'b': datetime.datetime},
"x_2": None,
"x_3": list
}
}
```
Please see the [wiki](https://github.com/asmodius/mokito/wiki) for more examples.
An asynchronous ORM for accessing MongoDB in Tornado
## What is mokito?
(MOngodb + [mongoKIt](https://github.com/namlook/mongokit) + TOrnado) is an asynchronous toolkit for working with ``mongodb`` inside a ``tornado`` app, like ``mongokit``. Mokito has a pure implementation of python + tornado and only depends on tornado and bson (provided by pymongo)
## Why not pymongo?
[PyMongo](http://api.mongodb.org/python/current/) is the recommended way to work with MongoDB in Python, but isn't asynchronous and not run inside tornado's IOLoop. If you use pymongo you won't take the advantages of tornado.
## Why not motor?
[Motor](http://emptysquare.net/motor/) wraps PyMongo and makes it async with greenlet. This is a great project, but it uses greenlet. If you can use greenlets why not use gevent instead of tornado? PyMongo already works with gevent. If you are using a very powerfull non-blocking web server with a pure python code, you'll probably want to work with a pure tornado driver for accessing mongo.
## Features
* validation and conversion of data to the specified type
* support for unstructured data
* dot notation
* control over data presentation
* control over data validation
* mapping onto the same document of models with different schemes
## Installing
```bash
pip install pymomgo tornado
pip install mokito
```
## Simple usage
```python
from tornado.web import RequestHandler
from tornado.gen import coroutine
import mokito
class MainHandler(RequestHandler):
def initialize(self):
self.db = mokito.Client("db_name", "mongodb://127.0.0.1:27017")
@gen.coroutine
def get(self, user_id):
user = yield self.db.user.find_one(user_id)
self.render("index.html", user=user)
```
## Using ORM
A Document declaration look as follows:
```python
from mokito import Document
class BlogPost(Document):
fields = {
'title':str,
'body':str,
'author':str
}
blogpost = BlogPost(title='my title', body='a body', author='unknown', foo='bar')
blogpost['author'] = 'me'
yield blogpost.save()
```
MongoDB in the collection "blog_post" will write this document:
```javascript
{"_id": ObjectId("..."), "body": "a body", "author": "me", "title": "my title"}
```
The field "foo" will not be saved. More about this you can read in [wiki](https://github.com/asmodius/mokito/wiki).
And you can use a more complex structure as follows:
```python
class ComplexDoc(Document):
__uri__ = "mongodb://127.0.0.1:27017"
__database__ = 'test'
__collection__ = 'example'
fields = {
"f_1": None,
"f_2": int,
"f_3": float,
"f_4": dict,
"f_5": [str],
"f_6": (int, str),
"f_7": {
"x_1": {'a': int, 'b': datetime.datetime},
"x_2": None,
"x_3": list
}
}
```
Please see the [wiki](https://github.com/asmodius/mokito/wiki) for more examples.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mokito-0.3.9.tar.gz
(24.8 kB
view details)
File details
Details for the file mokito-0.3.9.tar.gz
.
File metadata
- Download URL: mokito-0.3.9.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 140df0268fee212665aa8a109b03330c6cf77b5a2fac148c8ad841f0f1548cb0 |
|
MD5 | 2073a8b6f0d3ee74cf64c06948b4cebc |
|
BLAKE2b-256 | e535156fc0dbd8fd5ad8d4e6b1364c56b3c800c4d8ed9c2626896341855d2d41 |