Skip to main content

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

Project description

🌎 [English] ∙ [简体中文] PyPI - Python Version

🌟 Trod is a simple low-level asynchronous ORM using Python asyncio. It is very intuitive and easy to use.

Trod can help you easily build expressive common SQL statements in your asynchronous applications. You only need to use friendly object-oriented APIs to manipulate data without caring about the details of SQL statement writing and data processing. Suitable for scenarios where the business logic structure is relatively simple and has a certain amount of concurrency.

  • Requires: Python 3.7+
  • Now only supports MySQL
  • Not supports table relationship


See the wiki page for more information and quickstart documentation.


$ pip install trod

See the installation wiki page for more options.

Basic Examples

First, you should to import the Trod and instantiate a global variable:

from trod import Trod

db = Trod()

Defining models is simple:

from trod import types

class User(db.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(db.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)

Show some basic examples:

import asyncio
from datetime import datetime

from trod import JOINTYPE, types

async def show_case():

    # 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
    print(user_id)  # 1

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

    await User.update(email='').where( == 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
    print(count) # 2

    # Last gmail user
    user = await'')
    print(user) # [<User object> at 1]

    # Using `trod.util.tdict`
    users = await,
    ).where( < 2
    print(user)  # [{'id': 1, 'name': 'at7h'}]

    # Paginate get users who wrote Python posts this year
    users = await
                Post.update_at > datetime(2019, 1, 1),
    ).paginate(1, 10)
    print(users) # [<User object> at 1]

    # How many posts each user wrote?
    user_posts = await, types.F.COUNT(types.SQL('1')).as_('posts')
        Post, JOINTYPE.LEFT, on=( ==

👉 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 (as the MySQL connection driver), and referenced peewee in program design.
  • Please feel free to ⭐️ this repository if this project helped you 😉!

Project details

Download files

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

Files for trod, version 0.0.4
Filename, size File type Python version Upload date Hashes
Filename, size trod-0.0.4-py3-none-any.whl (59.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page