MongoDB support for Muffin framework.
Project description
Muffin-Mongo – MongoDB support for Muffin framework.
Requirements
python >= 3.7
Installation
Muffin-Mongo should be installed using pip:
pip install muffin-mongo
Usage
Setup the plugin and connect it into your app:
from muffin import Application
from muffin_mongo import Plugin as Mongo
# Create Muffin Application
app = Application('example')
# Initialize the plugin
# As alternative: db = DB(app, **options)
mongo = Mongo(db_url='mongodb://localhost:27017', database='db_name')
mongo.setup(app)
That’s it now you are able to use the plugin inside your views:
@app.route('/items', methods=['GET'])
async def get_items(request):
"""Return a JSON with items from the database."""
documents = await mongo.items.find().sort('key').to_list(100)
return [dict(dd.items(), _id=str(dd['_id'])) for dd in documents]
@app.route('/items', methods=['POST'])
async def insert_item(request):
"""Store items from JSON into database. Return ids."""
data = await request.data() # parse formdata/json from the request
res = await mongo.items.insert_many(data)
return [str(key) for key in res.inserted_ids]
Configuration options
Name |
Default value |
Desctiption |
db_url |
"mongodb://localhost:27017" |
A mongo connection URL |
database |
None |
A database name (optional) |
You are able to provide the options when you are initiliazing the plugin:
mongo.setup(app, db_url='mongodb://localhost:27017')
Or setup it from Muffin.Application configuration using the MONGO_ prefix:
MONGO_DB_URL = 'mongodb://localhost:27017'
Muffin.Application configuration options are case insensitive
Bug tracker
If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/muffin-mongo/issues
Contributing
Development of Muffin-Mongo happens at: https://github.com/klen/muffin-mongo
Contributors
klen (Kirill Klenov)
License
Licensed under a MIT license.
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 Distributions
Built Distribution
File details
Details for the file muffin_mongo-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: muffin_mongo-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4acb74502406a06fa7c6eaa933a6eab3d2a20592e2c6d3b968e1a203fa254a2 |
|
MD5 | f36d4b9bf818d0f4626c2374468fec2e |
|
BLAKE2b-256 | 9080541eafa83ef6d19e6b61e8be6a7b3c9faf195bd4457f93938c9021188aa1 |