mongodb,支持μmongo odm的sanic框架
sanic-mongodb-extension的Python项目详细描述
支持μmongo odm的mongodb,支持sanic框架
功能
- 使用motor_asyncio包对MongoDB进行异步查询
- 与uMongoodm很好地集成,因此您可以在项目中轻松地使用它
安装
此软件包应使用pip:
pip install sanic-mongodb-extension
示例
#!/usr/bin/env python3fromsanicimportSanic,responsefromsanic_mongodb_extimportMongoDbExtensionfromumongoimportInstance,Document,MotorAsyncIOInstancefromumongo.fieldsimportStringFieldapp=Sanic(__name__)# Configuration for MongoDB and uMongoapp.config.update({"MONGODB_DATABASE":"app",# Make ensure that the `app` database is really exists"MONGODB_URI":"mongodb://root:root@mongodb:27017","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.registerclassArtist(Document):name=StringField(required=True,allow_none=False)# And use it later for APIs@app.route("/")asyncdefhandle(request):artist=Artist(name="A new rockstar!")awaitartist.commit()returnresponse.json(artist.dump())if__name__=='__main__':app.run(host='0.0.0.0',port=8000)
许可证
sanic mongodb扩展是在bsd许可下发布的。有关详细信息,请阅读LICENSE文件。