Skip to content

A Python library for working with SQLite databases with model-based syntax similar to Laravel.

License

Notifications You must be signed in to change notification settings

benhmoore/PyLite

Repository files navigation

PyLite

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.

Why use PyLite? 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.

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

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

Environment Requirements

Python Version Supported
<3.9
>=3.9

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

pip3 install -e [PyLite_repo_directory]

Distribution

To build a distribution, run the following command from the root directory of the project: python3 -m build --sdist .

Then, use Twine to upload the distribution to PyPI: https://twine.readthedocs.io/en/latest/

Run Tests

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

coverage report --show-missing

Documentation

Documentation is hosted on GitHub, coming soon.

About

A Python library for working with SQLite databases with model-based syntax similar to Laravel.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages