Giter Site home page Giter Site logo

feileo / helo Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 4.0 602 KB

A simple and small low-level asynchronous ORM using Python asyncio.

Home Page: https://github.com/at7h/helo/wiki

License: MIT License

Python 99.98% Shell 0.02%
orm python3 asyncio aiomysql mysql web pymysql async await

helo's Introduction

https://cdn.at7h.com/img/helo.png

helo

๐ŸŒŽ [English] โˆ™ [็ฎ€ไฝ“ไธญๆ–‡]

https://travis-ci.org/at7h/helo.svg?branch=master https://coveralls.io/repos/github/at7h/helo/badge.svg?branch=master https://app.codacy.com/project/badge/Grade/c68578653eb546488fadddd95f19939c PyPI - Python Version

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

Helo 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.

  • Requires: Python 3.7+
  • Only supports MySQL now, and the version is 5.7+
  • Integration with web framework:
  • Not supports table relationship now

Installation

$ pip install helo

See the installation wiki page for more options.

Quickstart

See the wiki page for more information and quickstart documentation.

Basic Examples

First, you should to import helo and instantiate a global variable with helo.G

import helo

db = helo.G()

Defining models is simple:

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


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

Show some basic examples:

import asyncio
import datetime


async def show_case():
    # Binding the database(creating a connection pool)
    await db.bind('mysql://user:password@host:port/db')
    # Creating tables
    await db.create_tables([Author, Post])

    # Inserting few rows:

    author = Author(name='at7h', password='1111')
    author_id = await author.save()
    print(author_id)  # 1

    authors = await Author.get(author_id)
    print(author.id, author.name)  # 1, at7h

    await Author.update(email='[email protected]').where(Author.id == author_id).do()

    ret = await Author.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 Author.select().count()
    print(count) # 2

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

    # Using `helo.adict`
    authors = await Author.select(
        Author.id, Author.name
    ).where(
        Author.id < 2
    ).all(wrap=False)
    print(author)  # [{'id': 1, 'name': 'at7h'}]

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

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

asyncio.run(show_case())

With Quart

If you're using quart , a minimum application example is:

import quart
import helo

app = quart.Quart(__name__)
app.config["HELO_DATABASE_URL"] = "mysql://user:password@host:port/db"

db = helo.G(app)


@app.route('/api/authors')
async def authors():
    await Author.insert(
        name='at7h', email='[email protected]', password='xxxx'
    ).do()
    author_list = await Author.select().all(False)
    return quart.jsonify(author_list)


app.run()

Run it:

$ curl http://127.0.0.1:5000/api/authors
[{"email":"[email protected]","id":1,"name":"at7h","password":"xxxx"}]

๐Ÿ‘‰ 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 ๐Ÿค

  • Helo used aiomysql and was inspired by peewee in programming. Thank you very much for both!
  • Please feel free to โญ๏ธ this repository if this project helped you ๐Ÿ˜‰ !

helo's People

Stargazers

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

Watchers

 avatar  avatar  avatar

helo's Issues

How to update data use pk

Dear auth,
I use this library in my program๏ผŒand like it. But I make a model using auto_pk settings, but when i want to update one row like this:
eg: await User.update(dict(password=654321, date='2019-01-01'),
User.id == user.id)
It raise ValueError. I found that auto_pk filed id not a type of field type, so User.id == user.id not work, becasue it not Logic_ or Where_ instance. How can i do this? Is there just define id by self not use auto_pk ?

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.