Skip to main content

A simple asynchronous ORM for aiosqlite with extensive type hinting.

Project description

CobraORM

CobraORM is a simple, asynchronous ORM for aiosqlite with a big focus on type hinting and autocompletion.

Tired of constantly having to double check the name of a column in a table? Did you forget that column may be null?

Then CobraORM may be for you!

It currently doesn't have full SQL support, but that may change in the future.

Features

  • Create models (tables) with integer, real and text fields (columns);
  • Fields can be optional and have a default value;
  • Fields may be specified to accept a null value;
  • SELECT, DELETE, UPDATE and INSERT statements with proper type hinting according to the respective model;
  • Fluent interface (method chaining) construction of statements:
    • ORDER BY
    • LIMIT

Example usage

Let's start by connecting to an in-memory database:

import aiosqlite
import asyncio
from cobra_orm import Model, Text, Real, Integer

async def connect_to_database():
    db_connection = await aiosqlite.connect(":memory:")
    return db_connection

async def main():
    db_conn = await connect_to_database()

    # ... rest of the examples go here ...

    await db_conn.close()

if __name__ == "__main__":
    asyncio.run(main())

Now we can create a couple tables. We'll be creating an application for an hypothetical library:

    # -- SNIP --
    class Book(Model):
        book_id: Integer = Integer(primary_key=True, unique=True)
        title: Text = Text()
        genre: Text = Text()
        year: Integer = Integer()

    class User(Model):
        user_id: Integer = Integer(primary_key=True, unique=True)
        name: Text = Text(default="John Doe")
        favourite_genre: NullableText = NullableText(default=None)

    # pass the connection to the db to the models
    Book._conn = db_conn
    User._conn = db_conn

Let's start by adding some new books:

    # -- SNIP --
    new_books = [
        Book("Automate the Boring Stuff with Python", "Programming", 2019),
        Book("Cosmos", "Astronomy", 1980),
        Book("1984", "Dystopian Fiction", 1949),
    ]

    await Book.insert(*new_books)

We want to recommend 10 of the most recent books to the user john:

    # -- SNIP --
    recommendations = (
        await Book.select()
        .where(Book.genre == john.favourite_genre)
        .order_by(Book.year, desc=True)
        .limit(10)
    )

Jane seems to have changed her taste:

    # -- SNIP --
    jane.favourite_genre = "Fiction"
    await jane.update()

The following type hints can be inferred by the type checker:

    # -- SNIP --
    title: str, year: int = await Book.select(Book.title, Book.year)[0]
    books: tuple[Book, ...] = await Book.select()

Docs

For further information, check out USAGE.md.

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

cobra_orm-0.1.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

cobra_orm-0.1.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file cobra_orm-0.1.0.tar.gz.

File metadata

  • Download URL: cobra_orm-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.5 Linux/5.15.0-92-generic

File hashes

Hashes for cobra_orm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8811d99f62c77114c2bbc7b994ac3d59ea03b14a35a7aebe44c75efef60297af
MD5 a393b22a21ead35758886dd7e41405f2
BLAKE2b-256 440bed4d3c72408646ae4055a6b568b9c8cc817b457dc536efb9032304cf392c

See more details on using hashes here.

File details

Details for the file cobra_orm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cobra_orm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.5 Linux/5.15.0-92-generic

File hashes

Hashes for cobra_orm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f417ea6f3d5d2a83ca2bd7ba0ccd7bf4d471f825870a264d04a830013c7e89e
MD5 df25ad30e0af19d08ebad080bf33bc8c
BLAKE2b-256 a54de07c43e94cf5e43c05762d8600007d6a4d152366cad7e29b07ddf51b7ae9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page