Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/benhmoore/pylite
A Python library for working with SQLite databases with model-based syntax similar to Laravel.
https://github.com/benhmoore/pylite
database orm python sql sqlite3
Last synced: about 1 month ago
JSON representation
A Python library for working with SQLite databases with model-based syntax similar to Laravel.
- Host: GitHub
- URL: https://github.com/benhmoore/pylite
- Owner: benhmoore
- License: mit
- Created: 2022-07-05T03:48:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-08T18:02:44.000Z (over 1 year ago)
- Last Synced: 2024-10-07T08:46:58.445Z (3 months ago)
- Topics: database, orm, python, sql, sqlite3
- Language: Python
- Homepage:
- Size: 176 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# PyLite
**An object relational mapper (ORM) project inspired by Laravel's Eloquent, built for Python.**
## Tests: [![Coverage Status](https://img.shields.io/badge/coverage-100%25-brightgreen.svg)](https://github.com/benhmoore/PyLite)
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
- namebooks
- 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:
```python
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:
```python
# 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 | :x: |
| >=3.9 | :white_check_mark: |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.