Giter Club home page Giter Club logo

sanic-mongo's Introduction

sanic-mongo

sanic的mongodb异步工具,灵感来源自 官方例子 的封装, 目的只是为了简化操作.

更新

  • v1.7.1 修正了mongo连接权限可能引发的问题,现在可以配置每个连接是否是只能连database,默认为False.{"uri":xxxx,"only_db":True}
  • v1.6.0 修正了验证无法通过的问题
  • v1.5.0 将接口调整至和sanic-aioorm一致.

特点 Features

  • motor 支持的操作都支持
  • 支持3.5版本以上的
  • 支持多数据库
  • 支持mongodb和gridfs

依赖 Requirements

  1. motor>=1.1
  2. pymongo>=3.4.0
  3. sanic>=0.4.1
安装 Installation

  • pip install sanic-mongo

用法

mongo需要给`app.config`设置关键字`MONGO_URIS`,它是一个由mongodb名字和url组成的字典. 同时也可以使用`Mongo.SetConfig(app,**kws)`来注册kws的内容到`MONGO_URIS`. 而使用的时候可以访问`app.mongo[mongodb名字]`访问对应的db

gridfs与之类似,只是关键字是`GRIDFS_SETTINGS`,而访问需要使用`app.GridFS[GridFS名字]`

例子 Example

  1. mongodb
  1. gridfs
from sanic import Sanic
from sanic.response import json,text
from sanic_mongo import GridFS

app = Sanic(__name__)
mongo_uri = "mongodb://{host}:{port}/{database}".format(
    database='test',
    port=27017,
    host='localhost'
)

GridFS.SetConfig(app,test_fs=(mongo_uri,"fs"))
GridFS(app)

@app.get('/pics')
async def get(request):
    cursor = app.GridFS["test_fs"].find()
    result = [{i._id:i.name} async for i in cursor]
    return json({"result":result})


@app.post('/pics')
async def new(request):
    doc = request.files.get('file')

    async with app.GridFS["test_fs"].open_upload_stream(filename=doc.name,
        metadata={"contentType": doc.type}) as gridin:

        object_id = gridin._id
        await gridin.write(doc.body)

    return json({'object_id': str(object_id)})


if __name__ == "__main__":
    app.run(host='127.0.0.1', port=8000,debug=True)

sanic-mongo's People

Contributors

garito avatar hsz1273327 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.