Package: dsorm
Data Structure ORMA single file ORM for SQLite in PythonTable of ContentsAbout The ProjectGetting StartedUsageFurther ExamplesRoadmapContributingLicenseContactAbout The ProjectData Structure ORM (dsORM) a tiny, extensible ORM that builds off of Python's built in data structures.
dsORM can convert simple data structures such as dictionaries, enums, and dataclasses into tables.
For fine control you can craft tables, columns, and constraints from simple 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 (though admittedly it dwarfs dsORM's feature set.)Designed for easy integration / modification100% PythonNo external dependencies100% test coverageFunctional code in a single fileShould I use this?You shouldnotuse dsORM if:You can't work with SQLiteYou want something that enforces best practicesYou should use dsORM if:You know SQL enough to get around and want to avoid some boilerplateYou are prototyping and want something minimal to stand in for another ORMYou want to make your own project tailored ORM and can use dsORM as a starting pointYou cannot pip install in your environment and need a single file solution that can be bundledGetting StartedTo get a local copy up and running follow these simple steps.Installing with pippipinstalldsormFor information about cloning and dev setup see:ContributingUsageHere is an example showing basic usage.importdataclassesfromenumimportEnumfromdsormimportComparison,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_tableclassTeam(Enum):UNASSIGNED=0RED=1BLUE=2@make_table@dataclasses.dataclassclassPerson(DataClassTable):first_name:str=Nonelast_name:str=Noneteam:Team=Team.UNASSIGNEDperson=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.Statementstmt=person.insert(data=[{"first_name":"John","last_name":"Doe","team":Team.BLUE},],)# Statements can be examined with .sql methodprint(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 methodJane=Person(first_name="Jane",last_name="Doe",team=Team.RED).save()# Select returns a list of dicts of rows matching the wheredoe_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 Deleteperson.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 commentsFurther ExamplesA Practical ExampleCompound WHERE clauses and Tables from EnumJoins and Database from DictCustom Type Handling & Column DefaultsStore Python Objects with Pickle Data HandlerConfigurationRoadmapNeeded features:Subquery/CTE supportGrouping/AggregatesOrder/Limit/OffsetSee theopen issuesfor a list of proposed features (and known issues).ContributingContributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make aregreatly appreciated.Fork the ProjectCreate your Feature Branch (git checkout -b feature/AmazingFeature)Add tests, we aim for 100% test coverageUsing Coverageexecute: py.test --cov-report xml:cov.xml --covCommit your Changes (git commit -m 'Add some AmazingFeature')Push to the Branch (git push origin feature/AmazingFeature)Open a Pull RequestCloning / Development setupClone the repo and installgitclonehttps://github.com/kajuberdut/dsorm.gitcddsorm
pipenvinstall--devRun testspipenvshell
py.testFor more about pipenv see:Pipenv GithubLicenseDistributed under the BSD Two-clause License. SeeLICENSEfor more information.ContactPatrick Shechet -patrick.shechet@gmail.comProject Link:https://github.com/kajuberdut/dsorm