Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/relrin/sanic-mongodb-extension
MongoDB with μMongo support for Sanic framework
https://github.com/relrin/sanic-mongodb-extension
extension mongodb orm python sanic
Last synced: about 2 months ago
JSON representation
MongoDB with μMongo support for Sanic framework
- Host: GitHub
- URL: https://github.com/relrin/sanic-mongodb-extension
- Owner: Relrin
- License: bsd-3-clause
- Created: 2018-02-23T16:09:58.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-10T17:27:18.000Z (over 1 year ago)
- Last Synced: 2024-04-29T12:20:49.942Z (8 months ago)
- Topics: extension, mongodb, orm, python, sanic
- Language: Python
- Homepage:
- Size: 33.2 KB
- Stars: 27
- Watchers: 3
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
sanic-mongodb-extension
#######################
MongoDB with μMongo ODM support for Sanic frameworkFeatures
========
- Uses motor_asyncio_ package for async queries to MongoDB
- Good integrated with uMongo_ ODM, so that you can use it easily in your projectsInstallation
============
This package should be installed using pip: ::pip install sanic-mongodb-extension
Example
=======
.. code-block:: python#!/usr/bin/env python3
from sanic import Sanic, response
from sanic_mongodb_ext import MongoDbExtension
from umongo import Instance, Document
from umongo.fields import StringField
from umongo.frameworks import MotorAsyncIOInstanceapp = 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.ctx.mongodb` or `app.ctx.extensions['mongodb']`.
# The lazy client will be available as `app.ctx.lazy_mongodb` only when the database was specified,
# and which is a great choice for the structured projects.
MongoDbExtension(app)# Describe the model
@app.ctx.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... _links:
.. _uMongo: https://github.com/Scille/umongo
.. _motor_asyncio: https://motor.readthedocs.io/en/stable/
.. _LICENSE: https://github.com/Relrin/sanic-mongodb-extension/blob/master/LICENSEReal project examples
=====================
Open Matchmaking project:- `Auth/Auth microservice `_
- `Game servers pool microservice `_
- `Player statistics microservice `_