{"id":20613872,"url":"https://github.com/python-tools/sanic-mongo","last_synced_at":"2025-04-15T07:24:41.625Z","repository":{"id":57463873,"uuid":"87622211","full_name":"Python-Tools/sanic-mongo","owner":"Python-Tools","description":"sanic的mongodb异步工具","archived":false,"fork":false,"pushed_at":"2018-08-14T15:46:26.000Z","size":260,"stargazers_count":14,"open_issues_count":4,"forks_count":9,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T17:57:18.445Z","etag":null,"topics":["mongodb","motor","python3","sanic"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Python-Tools.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-08T09:15:24.000Z","updated_at":"2022-02-25T10:07:25.000Z","dependencies_parsed_at":"2022-09-05T06:01:34.160Z","dependency_job_id":null,"html_url":"https://github.com/Python-Tools/sanic-mongo","commit_stats":null,"previous_names":["sanic-extensions/sanic-mongo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mongo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mongo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mongo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Python-Tools%2Fsanic-mongo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Python-Tools","download_url":"https://codeload.github.com/Python-Tools/sanic-mongo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249025068,"owners_count":21200236,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongodb","motor","python3","sanic"],"created_at":"2024-11-16T11:11:18.050Z","updated_at":"2025-04-15T07:24:41.558Z","avatar_url":"https://github.com/Python-Tools.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"sanic-mongo\n====================\n\nsanic的mongodb异步工具,灵感来源自 `官方例子 \u003chttps://github.com/channelcat/sanic/blob/master/examples/sanic_motor.py).是[motor](https://motor.readthedocs.io/en/stable/tutorial-asyncio.html\u003e`_ 的封装,\n目的只是为了简化操作.\n\n更新\n-----------------------\n\n* v1.7.1 修正了mongo连接权限可能引发的问题,现在可以配置每个连接是否是只能连database,默认为False.{\"uri\":xxxx,\"only_db\":True}\n* v1.6.0 修正了验证无法通过的问题\n* v1.5.0 将接口调整至和sanic-aioorm一致.\n\n\n\n特点 Features\n-------------------------\n\n* `motor \u003chttps://motor.readthedocs.io/en/stable/tutorial-asyncio.html\u003e`_ 支持的操作都支持\n* 支持3.5版本以上的\n* 支持多数据库\n* 支持mongodb和gridfs\n\n\n\n依赖 Requirements\n-----------------------------\n1. motor\u003e=1.1\n2. pymongo\u003e=3.4.0\n3. sanic\u003e=0.4.1\n\n\n 安装 Installation\n--------------------------\n\n- ``pip install sanic-mongo``\n\n\n用法\n----------------------------\n\nmongo需要给`app.config`设置关键字`MONGO_URIS`,它是一个由mongodb名字和url组成的字典.\n同时也可以使用`Mongo.SetConfig(app,**kws)`来注册kws的内容到`MONGO_URIS`.\n而使用的时候可以访问`app.mongo[mongodb名字]`访问对应的db\n\ngridfs与之类似,只是关键字是`GRIDFS_SETTINGS`,而访问需要使用`app.GridFS[GridFS名字]`\n\n\n例子 Example\n-----------------------------------\n\n1. mongodb\n\n.. code:: python\n    from sanic import Sanic\n    from sanic.response import json\n    from sanic_mongo import Mongo\n\n    app = Sanic(__name__)\n    mongo_uri = \"mongodb://{host}:{port}/{database}\".format(\n        database='test',\n        port=27017,\n        host='localhost'\n    )\n\n    Mongo.SetConfig(app,test=mongo_uri)\n    Mongo(app)\n\n    @app.get('/objects')\n    async def get(request):\n        docs = await app.mongo['test'].test_col.find().to_list(length=100)\n        for doc in docs:\n            doc['id'] = str(doc['_id'])\n            del doc['_id']\n        return json(docs)\n\n\n    @app.post('/objects')\n    async def new(request):\n        doc = request.json\n        print(type(app.mongo['test']))\n        object_id = await app.mongo['test'][\"test_col\"].save(doc)\n        return json({'object_id': str(object_id)})\n\n\n    if __name__ == \"__main__\":\n        app.run(host='127.0.0.1', port=8000,debug=True)\n\n\n\n\n2. gridfs\n\n.. code:: python\n\n    from sanic import Sanic\n    from sanic.response import json,text\n    from sanic_mongo import GridFS\n\n    app = Sanic(__name__)\n    mongo_uri = \"mongodb://{host}:{port}/{database}\".format(\n        database='test',\n        port=27017,\n        host='localhost'\n    )\n\n    GridFS.SetConfig(app,test_fs=(mongo_uri,\"fs\"))\n    GridFS(app)\n\n    @app.get('/pics')\n    async def get(request):\n        cursor = app.GridFS[\"test_fs\"].find()\n        result = [{i._id:i.name} async for i in cursor]\n        return json({\"result\":result})\n\n\n    @app.post('/pics')\n    async def new(request):\n        doc = request.files.get('file')\n\n        async with app.GridFS[\"test_fs\"].open_upload_stream(filename=doc.name,\n            metadata={\"contentType\": doc.type}) as gridin:\n\n            object_id = gridin._id\n            await gridin.write(doc.body)\n\n        return json({'object_id': str(object_id)})\n\n\n    if __name__ == \"__main__\":\n        app.run(host='127.0.0.1', port=8000,debug=True)\n    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-tools%2Fsanic-mongo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpython-tools%2Fsanic-mongo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpython-tools%2Fsanic-mongo/lists"}