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.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: cobra_orm-0.1.1.tar.gz
  • Upload date:
  • Size: 11.3 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.1.tar.gz
Algorithm Hash digest
SHA256 9d3ddebcf597df47c8aeb9593d91864b5686755d2ac9c37f1d03d5866610ed07
MD5 0938efd5b9757a72ac2043b284613585
BLAKE2b-256 c6ae75e755927d0d21436b92e0c5a408b3725261a83ea39cb4caa49827673f2b

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: cobra_orm-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5538de69c8ee2e21e76e6a6e5dcb67d0b001d400a65dbd2199920a7aa075219b
MD5 3621efef8ec08c9d9d5ea433fb753cbc
BLAKE2b-256 751000b73aaeb7ec38225951f85431284aa0437fc24cc6a4f9282430b1d58044

See more details on using hashes here.

Provenance

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