Skip to main content

An SQLite ORM for Python, inspired by Laravel's Eloquent.

Project description

LitePy

An object relational mapper (ORM) project inspired by Laravel's Eloquent, built for Python.

Tests: Coverage Status

This is an ongoing project to develop a better, pythonic, model-based system for database manipulation, heavily inspired by Laravel's ORM, Eloquent.

The goal is to abstract away the necessity of writing SQL queries and maintaining complex relationships between tables. You can instead define readable model classes that represent tables, declare relationships between these models, and manipulate data in way that complements Python syntax.

Database Support

LitePy currently supports SQLite (hence the name).

Why use LitePy? A quick example.

Consider a rudimentary system that maintains a database of authors and books for a library. An author may have written one book, multiple books, or none at all. We would like to be able to relate each book with an author. That way, we can ask the system for all books written by an author or for the author of a given book. Nice and simple, right?

The database for this system consists of the following two tables:

authors
   - id
   - name

books
   - id
   - name
   - author_id

Python offers excellent support of SQLite out of the box, but you'll still need to get your hands dirty with some SQL queries.

LitePy abstracts all that away.

First, we'll define two models to represent the two tables above. These classes inherit from LiteModel, which will later be explored in detail. They each represent a single object, or row, in their respective tables:

class Author(LiteModel):

    def books(self) -> LiteCollection:
        return this.has_many(Book)

class Book(LiteModel):

    def author(self) -> LiteModel:
        return this.belongs_to(Author)

...And that's it. Literally.

Now, we can operate on our data in a beautifully abstract way:

# Create a new author in the database
jane = Author.create({
    'name': 'Jane Doe'
})

# Create a new book in the database
moneyBook = Book.create({
    'name': 'Make Money Fast!'
})

# "Attach" this book to the author
jane.attach(moneyBook)

moneyBook.author() # Returns the author!
jane.books() # Returns a list of her books!

There are a host of methods provided by LitePy for manipulating the models we've created. Methods for updating, creating, deleting, relating, and even path finding!

Of course, most use cases are orders of magnitude more complex than this example. LitePy supports all common database relationship types (has_one, has_many, belongs_to, belongs_to_many), relationships that span multiple databases, nonstandard table names, and more.

If this example piqued your interest, continue on for all the gritty details.

Installation

To build from source, clone this repository and install a live, development version with pip:

pip3 install -e [litepy_repo_directory]

Run Tests

coverage run --omit="*/tests/*" -m pytest tests

coverage report --show-missing

Documentation

Documentation is hosted on GitHub, right over here.

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

lite-py-0.0.1.tar.gz (25.4 kB view details)

Uploaded Source

File details

Details for the file lite-py-0.0.1.tar.gz.

File metadata

  • Download URL: lite-py-0.0.1.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.2

File hashes

Hashes for lite-py-0.0.1.tar.gz
Algorithm Hash digest
SHA256 14334ac0bb93b70c695afbb828189933e79334e386ac1295156d435f5700c37b
MD5 545db2fea4d361259abe101802f44546
BLAKE2b-256 617818cceb79fe0cafe7ab887f8765118c6110a8fda00769e6ded24261c92a7d

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