Coder Social home page Coder Social logo

muffin-databases's Introduction

Muffin-Databases

Muffin-Databases -- Async support for a range of databases for Muffin Framework

Tests Status PYPI Version Python Versions

Requirements

  • python >= 3.7

Note

The plugin supports only asyncio evenloop (not trio)

Installation

Muffin-Databases should be installed using pip:

$ pip install muffin-databases

Optionally you can install the required database drivers with:

$ pip install muffin-databases[sqlite]
$ pip install muffin-databases[postgres]
$ pip install muffin-databases[mysql]

Driver support is provided using one of asyncpg, aiomysql, or aiosqlite.

Usage

Setup the plugin and connect it into your app:

from muffin import Application
from muffin_databases import Plugin as DB

# Create Muffin Application
app = Application('example')

# Initialize the plugin
# As alternative: db = DB(app, **options)
db = DB(url='sqlite:///db.sqlite')
db.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 database."""
    rows = await db.fetch_all('SELECT * from items')
    return [dict(row.items()) for row in rows]

@app.route('/items', methods=['POST'])
async def insert_item(request):
    """Store an item into database."""
    data = await request.data()  # parse formdata/json from the request
    await db.execute_many('INSERT INTO items (name, value) VALUES (:name, :value)', values=data)
    return 'OK'

The Muffin-Databases plugin is based on databases. See the databases's docs for further references.

Options

Name Default value Desctiption
url "sqlite:///db.sqlite" A database connection URL
params {} A database connection params

You are able to provide the options when you are initiliazing the plugin:

db.setup(app, url='postgresql://localhost/example', params={'ssl': True, 'min_size': 5, 'max_size': 20})

Or setup it from Muffin.Application configuration using the DATABASES_ prefix:

DATABASES_URL = 'postgresql://localhost/example'

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-databases/issues

Contributing

Development of Muffin-Databases happens at: https://github.com/klen/muffin-databases

Contributors

  • klen (Kirill Klenov)

License

Licensed under a MIT license.

muffin-databases's People

Contributors

dependabot[bot] avatar klen avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.