Skip to main content

Easy async ORM for python, built with relations in mind.

Project description

postmodel

Introduction

Postmodel is an easy-to-use asyncio ORM (Object Relational Mapper) inspired by Django and Tortoise ORM.

Postmodel provides 90% Django ORM like API, to ease the migration of developers wishing to switch to asyncio.

Currently, Postmodel provides following features:

  • full active-record pattern
  • optimistic locking
  • 100% code coverage

But, it still have some limits:

  • only support Postgresql
  • no planing support SQLite, instead it will supports RediSQL
  • no support relation

Postmodel is supported on CPython >= 3.6 for PostgreSQL.

Getting Started

Installation

You have to install postmodel like this:

pip install postmodel

Quick Tutorial

Primary entity of postmodel is postmodel.models.Model. You can start writing models like this:

from postmodel import models

class Book(models.Model):
    id = models.IntField(pk=True)
    name = models.TextField()
    tag = models.CharField(max_length=120)

    class Meta:
        table = "book_test"

    def __str__(self):
        return self.name

After you defined all your models, postmodel needs you to init them, in order to create backward relations between models and match your db client with appropriate models.

You can do it like this:

from postmodel import Postmodel

async def init():
    # Here we connect to a PostgreSQL DB.
    # also specify the app name of "models"
    # which contain models from "app.models"
    await Postmodel.init(
        'postgres://postgres@localhost:54320/test_db',
        modules= [__name__]
    )
    # Generate the schema
    await Postmodel.generate_schemas()

Here we create connection to Postgres database, and then we discover & initialise models.

Postmodel currently supports the following databases:

  • PostgreSQL (requires asyncpg)

generate_schema generates the schema on an empty database. Postmodel generates schemas in safe mode by default which includes the IF NOT EXISTS clause, so you may include it in your main code.

After that you can start using your models:

# Create instance by save
book = Book(id=1, name='Mastering postmdel', tag="orm")
await book.save()

# Or by .create()
await Book.create(id=2, name='Learning Python', tag="python")

# Query

books = await Book.filter(tag="orm").all()
assert len(books) == 1

Contributing

Please have a look at the Contribution Guide <docs/CONTRIBUTING.md>_

License

This project is licensed under the MIT License - see the LICENSE <LICENSE>_ file for details

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

postmodel-0.2.0.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

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

postmodel-0.2.0-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

File details

Details for the file postmodel-0.2.0.tar.gz.

File metadata

  • Download URL: postmodel-0.2.0.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for postmodel-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b7b83eeb3a8fa116ea8ccdebda4d2630fe91055165a5ac9d250f3b056e9cfc1f
MD5 c5bf05083bc01bcdd12004e4e63f8de9
BLAKE2b-256 06b8265f3eb136ac539bbd7911a98694d217c72b5adea418a53b97269a7fdbf0

See more details on using hashes here.

File details

Details for the file postmodel-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: postmodel-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for postmodel-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 245e1eaa5b6c236accde32318cd3d38ef6441f2e086a45064f9ee22b39587d15
MD5 051cbe3660377a4280ddd1ae924d0a5b
BLAKE2b-256 58a73baf369d51c7bca11caa40770613bc1ed3c01157671f7cee6a4d694a6896

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