MongoDB with μMongo ODM support for Sanic framework
Project description
MongoDB with μMongo ODM support for Sanic framework
Features
Uses motor_asyncio package for async queries to MongoDB
Good integrated with uMongo ODM, so that you can use it easily in your projects
Installation
This package should be installed using pip:
pip install sanic-mongodb-extension
Example
#!/usr/bin/env python3
from sanic import Sanic, response
from sanic_mongodb_ext import MongoDbExtension
from umongo import Instance, Document, MotorAsyncIOInstance
from umongo.fields import StringField
app = Sanic(__name__)
# Configuration for MongoDB and uMongo
app.config.update({
"MONGODB_DATABASE": "app", # Make ensure that the `app` database is really exists
"MONGODB_URI": "mongodb://root:root@mongodb:27017",
# You can also specify custom connection options.
# For more details check the official docs: https://api.mongodb.com/python/3.7.0/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient
"MONGODB_CONNECT_OPTIONS: {
"minPoolSize": 10,
"maxPoolSize": 50,
},
"LAZY_UMONGO": MotorAsyncIOInstance(),
})
# uMongo client is available as `app.mongodb` or `app.extensions['mongodb']`.
# The lazy client will be available as `app.lazy_mongodb` only when the database was specified,
# and which is a great choice for the structured projects.
MongoDbExtension(app)
# Describe the model
@app.lazy_umongo.register
class Artist(Document):
name = StringField(required=True, allow_none=False)
# And use it later for APIs
@app.route("/")
async def handle(request):
artist = Artist(name="A new rockstar!")
await artist.commit()
return response.json(artist.dump())
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
License
The sanic-mongodb-extension is published under BSD license. For more details read LICENSE file.
Real project examples
Open Matchmaking project:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for sanic-mongodb-extension-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c3a8a8cc8151a6d20b2986bf97ac5dbe6508c18a9b2483083ea816a2b1027d9 |
|
MD5 | f0e83407ad0eaba347f49fd4ccfc0be3 |
|
BLAKE2b-256 | 0477161fd22e6a3051f4a4e9074cae871982f71da5ff4b1a52dbccce3e76614b |
Close
Hashes for sanic_mongodb_extension-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 13d4327ac6b66f94db5a4990bbcb24d843db2783386429de204a6032df3c4da9 |
|
MD5 | 68a0e48b7f66f93ba791d0d984ab9eca |
|
BLAKE2b-256 | f8892dd0457ef4ee2f84896a14747cb84b3b8aeb27c5cbb115c135bbe39a2ca2 |