The main goal of ORMagic is to provide a simple and easy-to-use ORM for Python, that is easy to understand and use, while still providing the necessary features to interact with a database. Is based on the Pydantic model and extends it with the ability to interact with SQLite database.
from ormagic import DBModel
class User(DBModel):
name: str
age: int
User.create_table()
User(name="John", age=30).save()
User.get(name="John")
>>> User(id=1, name='John', age=30)
You can find more examples and detailed documentation at spaceshaman.github.io/ORMagic/
You can install ORMagic using pip:
pip install ORMagic
Or you can install the latest version from the GitHub repository:
git clone git@github.com:SpaceShaman/ORMagic.git
cd ORMagic
pip install .
The full documentation is available at spaceshaman.github.io/ORMagic/
- Define table schema using Pydantic models
- Basic CRUD operations
- Save data to the database
- Read data from the database
- Update data in the database
- Delete data from the database
- Relationships between tables
- One-to-many
- Create a tables with a foreign key
- Save data with a foreign key
- Read data with a foreign key
- Update data with a foreign key
- Delete data with a foreign key
- Cascade
- Set null
- Restrict
- Set default
- No action
- One-to-one
- Many-to-many
- One-to-many
- Unique constraints
- Remove table
- Read all data from the database
- Filter data and retrieve multiple records
- Equal
- Not equal
- Greater than
- Greater than or equal
- Less than
- Less than or equal
- Like (Pattern matching with % and _)
- Not like (Pattern matching with % and _)
- In (List of values)
- Not in (List of values)
- Between (Two values)
- Not between (Two values)
- Q objects to combine filters (AND, OR, NOT)
- Protect against SQL injection
- Order by
- Limit and offset
- Update table schema
- Add new column
- Rename column
- Drop column
- Custom primary key
- Transactions
- Functions
- Aggregate functions
- String functions
- Date and time functions
- Mathematical functions
- Control flow functions
- Migrations
- Integration with other databases
Changes for each release are thoroughly documented in release notes
Contributions are welcome! Feel free to open an issue or submit a pull request. I would like to keep the library to be safe as possible, so i would appreciate if you cover any new feature with tests to maintain 100% coverage.
-
First, clone the repository:
git clone git@github.com:SpaceShaman/ORMagic.git
-
Install poetry if you don't have, here you can find the instructions
-
Create a virtual environment and install the dependencies:
cd ORMagic poetry install --no-root
-
Activate the virtual environment:
poetry shell
If you in the virtual environment, you can run the tests with the following command:
pytest
You can also run the tests with coverage:
pytest --cov=ormagic
This project is licensed under the terms of the MIT license