Skip to main content

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

Project description

🌎 [English] ∙ [简体中文]

https://img.shields.io/pypi/v/trod.svg 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 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

Quickstart

See the wiki page for more information and quickstart documentation.

Installation

$ 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 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(show_case())

👉 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