Skip to main content

a simple orm suport asyncio,fork of aiopeewee

Project description


Asyncio interface for peewee modeled after torpeewee

Feature

  • support mysql and postgresql

  • database factory using database URL

  • use peewee’s fields

  • ManyToManyField support

  • Shortcuts support

  • csv dump /load support

  • can use playhose.postgres_ext.JSONField

Install

  • python -m pip install aioorm

Example: GRUD

from aioorm 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

Example: Many to many

Note that AioManyToManyField must be used instead of ManyToMany.

from aioorm 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 aioorm import model_to_dict

serialized = await model_to_dict(user)

Dump to csv

tables can be dump to a csv file.

from aioorm.utils import aiodump_csv
query = User.select().order_by(User_csv.id)
await aiodump_csv(query,str(filepath))

Documentation

Documentation on Readthedocs.

TODO

  • async dataset support

  • more test

Limitations

  • untested transactions

  • only support mysql and postgresql

Bug fix

  • fixed get and get_or_create ‘s bug

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aioorm-0.1.2.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioorm-0.1.2-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file aioorm-0.1.2.tar.gz.

File metadata

  • Download URL: aioorm-0.1.2.tar.gz
  • Upload date:
  • Size: 29.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aioorm-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a0d4b47fbcf35b6abfe6991120a13569ca4f3d649fea5459d96be1007629d164
MD5 bddfbb24f3843a0172d7030f1c5c409a
BLAKE2b-256 c873c879cfa22798b9c630e0233470abdc08bdfe67a2e06f797c9e2a909cf711

See more details on using hashes here.

File details

Details for the file aioorm-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for aioorm-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 da1a3cee24c3a2edee53d531c7a44dbb439347ded0bce66d6f6f4b0985ec05d3
MD5 39690253ac4ff8c4a75bcd64aa2b2aa1
BLAKE2b-256 fd8979c2d91da0c2a3558db50bc0502500967f65fab576f82182470baf0ed577

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page