Trod is a low-level simple asynchronous ORM using Python asyncio


Keywords
orm asyncio mysql aiomysql pymysql python3 async/await, aiomysql, async, asyncio, await, mysql, orm, pymysql, python3, web
License
MIT
Install
pip install trod==0.0.4

Documentation

trod

https://travis-ci.org/at7h/trod.svg?branch=master https://coveralls.io/repos/github/at7h/trod/badge.svg?branch=master https://api.codacy.com/project/badge/Grade/24451621f9554f7a8d857c5b3dd6e522

Trod is a low-level simple asynchronous ORM using Python asyncio.

View Chinese

  • Using it to easily build expressive and commonly used SQL, suitable for scenarios with simple business structures and a certain amount of concurrency
  • Now only supports MySQL, using aiomysql as the connection driver
  • Not supports table relationship

Quickstart:

Requires:

PyPI - Python Version

Installation

pip install trod

Base Examples

Defining models is simple:

from trod import Model, types

class User(Model):
    id = types.BigAuto()
    name = types.VarChar(length=45, null=False)
    email = types.Email(default='')
    password = types.VarChar(length=100, null=False)
    create_at = types.Timestamp(default=types.ON_CREATE)


class Post(Model):
    id = types.Auto()
    title = types.VarChar(length=100)
    author = types.Int(default=0)
    content = types.Text(encoding=types.ENCODING.utf8mb4)
    create_at = types.Timestamp(default=types.ON_CREATE)
    update_at = types.Timestamp(default=types.ON_UPDATE)

Shows some basic examples:

import asyncio
from datetime import datetime

from trod import Trod, JOINTYPE, types


db = Trod()


async def base_example():

    # Binding the database(creating a connection pool)
    # and create the table:
    await db.bind('mysql://user:password@host:port/db')
    await db.create_tables([User, Post])

    # Inserting few rows:

    user = User(name='at7h', password='1111')
    user_id = await user.save()
    print(user_id)  # 1

    users = await User.get(user_id)
    print(user.id, user.name)  # 1, at7h

    await User.update(email='g@gmail.com').where(User.id == user_id).do()

    ret = await User.insert(name='pope', password='2222').do()
    posts = [
        {'title': 'Python', 'author': 1},
        {'title': 'Golang', 'author': 2},
    ]
    ret = await Post.minsert(posts).do()
    print(ret)  # (2, 1)

    # Supports expressive and composable queries:

    count = await User.select().count()
    print(count) # 2

    # Last gmail user
    user = await User.select().where(
        User.email.endswith('gmail.com')
    ).order_by(
        User.create_at.desc()
    ).first()
    print(user) # [<User object> at 1]

    # using `trod.util.tdict`
    users = await User.select(
        User.id, User.name
    ).where(
        User.id < 2
    ).all(wrap=False)
    print(user)  # [{'id': 1, 'name': 'at7h'}]

    # Paginate get users who wrote Python posts this year
    users = await User.select().where(
        User.id.in_(
            Post.select(Post.author).where(
                Post.update_at > datetime(2019, 1, 1),
                Post.title.contains('Python')
            ).order_by(
                Post.update_at.desc()
            )
        )
    ).paginate(1, 10)
    print(users) # [<User object> at 1]

    # How many posts each user wrote?
    user_posts = await User.select(
        User.name, types.F.COUNT(types.SQL('1')).as_('posts')
    ).join(
        Post, JOINTYPE.LEFT, on=(User.id == Post.author)
    ).group_by(
        User.name
    ).rows(100)


asyncio.run(base_example())

👉 See more examples

Contributing 👏

  • I hope those who are interested can join in and work together. Any kind of contribution is expected: report a bug 🐞, give a advice or create a pull request 🙋‍♂️.

Thanks 🤝

  • Special thanks to projects aiomysql and peewee, trod uses aiomysql, and referenced peewee in program design.
  • Please feel free to ⭐️ this repository if this project helped you 😉!

TODO 📝

  • Documents ✍️