A single file ORM for SQLite in Python
Data Structure ORM (dsORM) a tiny, extensible ORM that leverages Python's built in data structures. dsORM easily converts dictionaries, enums, and dataclasses into tables. For fine control you can craft tables, columns, and constraints from provided base classes.
If SQLAlchemy's expression language comes to mind, yes, this is a bit like that. But dsORM is much simpler. The entire functional code is in a single file which is currently under 1,500 lines. For comparison, PeeWee, a fairly small ORM is 7,723 lines long in it's main file and that doesn't contain all functional code. SQLAlchemy as of this writing contains 343,975 lines of Python code (admittedly, it dwarfs dsORM's feature set.)
- 100% Python
- No external dependencies
- 100% test coverage
- Functional code in a single file
- You can't work with SQLite
- You want something that enforces best practices
- You know SQL enough to get around and want to avoid some boilerplate
- You are prototyping and want something minimal to stand in for another ORM
- You want to make your own project tailored ORM and can use dsORM as a starting point
- You cannot pip install in your environment and need a single file solution that can be bundled
To get a local copy up and running follow these simple steps.
pip install dsorm
For information about cloning and dev setup see: Contributing
Here is an example showing basic usage.
import dataclasses
from enum import Enum
from dsorm import Comparison, Database, DataClassTable, make_table
# the .memory() constructor is equivilent to Database(db_path=":memory:", is_default=True)
db = Database.memory()
# Leverage enums for efficient small lookup tables
@make_table
class Team(Enum):
UNASSIGNED = 0
RED = 1
BLUE = 2
@make_table
@dataclasses.dataclass
class Person(DataClassTable):
first_name: str = None
last_name: str = None
team: Team = Team.UNASSIGNED
person = db.table("Person")
print(person.sql())
# CREATE TABLE IF NOT EXISTS person (id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT);
# Tables have insert, select, and delete methods which return subclasses of dsorm.Statement
stmt = person.insert(
data=[
{"first_name": "John", "last_name": "Doe", "team": Team.BLUE},
],
)
# Statements can be examined with .sql method
print(stmt.sql())
# INSERT INTO [Person] (first_name, last_name, team) VALUES ('John', 'Doe', 2)
# or executed with .execute()
stmt.execute()
# Subclasses of DataClassTable inherit a save method
Jane = Person(first_name="Jane", last_name="Doe", team=Team.RED).save()
# Select returns a list of dicts of rows matching the where
doe_family = person.select(
where={"first_name": Comparison.like(target="J%n%")},
).execute()
print(doe_family)
# [
# {'id': 1, 'first_name': 'John', 'last_name': 'Doe', 'team': <Team.BLUE: 2>
# },
# {'id': 2, 'first_name': 'Jane', 'last_name': 'Doe', 'team': <Team.RED: 1>
# }
# ]
# And Delete
person.delete(where={"id": doe_family[0]["id"]}).execute()
print(person.select(column=["id", "first_name"]).execute())
# [{'id': '2', 'first_name': 'Jane'}]
The same example without comments or prints. Shows efficiency and readability of DDL/DML
- A Practical Example
- Compound WHERE clauses and Tables from Enum
- Joins and Database from Dict
- Custom Type Handling & Column Defaults
- Store Python Objects with Pickle Data Handler
- Configuration
Needed features:
- Subquery/CTE support
- Grouping/Aggregates
- Order/Limit/Offset
See the open issues for a list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Add tests, we aim for 100% test coverage Using Coverage
- execute: py.test --cov-report xml:cov.xml --cov
- Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
- Clone the repo and install
git clone https://github.com/kajuberdut/dsorm.git cd dsorm pipenv install --dev
- Run tests
pipenv shell py.test
For more about pipenv see: Pipenv Github
Distributed under the BSD Two-clause License. See LICENSE
for more information.
Patrick Shechet - [email protected]
Project Link: https://github.com/kajuberdut/dsorm