Code Monkey home page Code Monkey logo

aiopeewee's Introduction

AioPeewee

Asyncio interface for peewee modeled after torpeewee

Implemented database adapters:

  • [x] aiomysql
  • [ ] aiopg
  • [ ] sqlite

Currently 125 test cases have been ported from peewee, not all of them but constantly increases.

Simple Atomic operations (transactions) are also supported, but now well tested.

Install

pip install aiopeewee

# or

conda install aiopeewee

Usage

from aiopeewee import AioModel, AioMySQLDatabase
from peewee import CharField, TextField, DateTimeField
from peewee import ForeignKeyField, PrimaryKeyField


db = AioMySQLDatabase('test', host='127.0.0.1', port=3306,
                     user='root', password='')


class User(AioModel):
    username = CharField()

    class Meta:
        database = db


class Blog(AioModel):
    user = ForeignKeyField(User)
    title = CharField(max_length=25)
    content = TextField(default='')
    pub_date = DateTimeField(null=True)
    pk = PrimaryKeyField()

    class Meta:
        database = db


# create connection pool
await db.connect(loop)

# count
await User.select().count()

# async iteration on select query
async for user in User.select():
    print(user)

# fetch all records as a list from a query in one pass
users = await User.select()

# insert
user = await User.create(username='kszucs')

# modify
user.username = 'krisztian'
await user.save()

# async iteration on blog set
[b.title async for b in user.blog_set.order_by(Blog.title)]

# close connection pool
await db.close()

# see more in the tests

ManyToMany

Note that AioManyToManyField must be used instead of ManyToMany.

from aiopeewee import AioManyToManyField


class User(AioModel):
    username = CharField(unique=True)

    class Meta:
        database = db


class Note(AioModel):
    text = TextField()
    users = AioManyToManyField(User)

    class Meta:
        database = db


NoteUserThrough = Note.users.get_through_model()


async for user in note.users:
    # do something with the users

Currently the only limitation I'm aware of immidiate setting of instance relation must be replaced with a method call:

# original, which is not supported
charlie.notes = [n2, n3]

# use instead
await charlie.notes.set([n2, n3])

Serializing

Converting to dict requires the asyncified version of model_to_dict

from aiopeewee import model_to_dict

serialized = await model_to_dict(user)

aiopeewee's People

Contributors

kszucs avatar marijngiesen avatar tdna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aiopeewee's Issues

Refactor connection/atomic/transaction context handling

Due to the use of connection pool by default, the async with self.pool.acquire() idiom is required in most of the database methods. The async with hell gets complicated in the case of atomic operations. The with db.atomic() idiom requires the connection to be shipped downward resulting a deeply nested async context manager, which should look like a single one (from the API perspective). The original peewee implementation uses deque and heap structures to handle the nesting within a single thread.

The most painful parts are here:

I could use a more asyncio experienced point of view here :)
Could someone give me a hand / guidance?

cc @ajdavis @jettify

ImportError: cannot import name 'NoopSelectQuery'

hi, when i run sanic aiopeewee example, I get

Traceback (most recent call last):
  File "/Users/name/PycharmProjects/sanic-demo/sanic_aiopeewee.py", line 4, in <module>
    from aiopeewee import AioModel, AioMySQLDatabase, model_to_dict
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiopeewee/__init__.py", line 1, in <module>
    from .model import AioModel
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiopeewee/model.py", line 3, in <module>
    from .query import (AioSelectQuery, AioUpdateQuery, AioInsertQuery,
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/aiopeewee/query.py", line 2, in <module>
    from peewee import SQL, Query, RawQuery, SelectQuery, NoopSelectQuery
ImportError: cannot import name 'NoopSelectQuery'

What happen?
Modules is installed

'AioNaiveQueryResultWrapper' object has no attribute '_meta'

File "/Users/rara/.pyenv/versions/miniconda3-latest/envs/papi-conda/lib/python3.6/site-packages/sanic/app.py", line 471, in handle_request
response = await response
File "/Users/rara/git/papi/papi/access.py", line 13, in wrapper
response = await f(request, *args, **kwargs)
File "/Users/rara/git/papi/papi/account.py", line 18, in list_accounts
return await to_json(accounts, max_depth=0)
File "/Users/rara/git/papi/papi/utils.py", line 36, in to_json
return json(await to_native(record, **kwargs))
File "/Users/rara/git/papi/papi/models.py", line 190, in to_native
return await model_to_dict(record, **kwargs)
File "/Users/rara/.pyenv/versions/miniconda3-latest/envs/papi-conda/lib/python3.6/site-packages/aiopeewee/shortcuts.py", line 43, in model_to_dict
for field in model._meta.declared_fields:
AttributeError: 'AioNaiveQueryResultWrapper' object has no attribute '_meta'

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.