|
1 | 1 |  |
2 | | - |
| 2 | + |
3 | 3 |
|
4 | 4 |
|
5 | | -# DBTOGO |
| 5 | +# DBTogo |
| 6 | +Python ORM with focus on simplicity and compatibility. |
| 7 | +DBTogo is made to allow you to interact with SQL databases in Python as simply and seamlessly as possible. Its goal is to completely abstract the db away, providing a simple Python native interface. |
6 | 8 |
|
7 | | -dbtogo is a simple work-in-progress ORM library for Python3. Its goal is to emulate the style libraries like peewee use (maximal abstraction and simplicity) while being build on top of Pydantic models for simple integration with FastAPI and others. |
| 9 | +Advantages of DBTogo include: |
| 10 | +- full Pydantic integration |
| 11 | +- leverages native Python annotation |
| 12 | +- focuses heavily on simplicity and reducing boilerplate |
8 | 13 |
|
9 | | -dbtogo is aimed at smaller projects that do not require optimization. I am making this primarily to learn more about ORMs, so this should not be used in any serious production code. |
| 14 | +### CURRENT STATE |
| 15 | + |
| 16 | +DBTogo currently supports: |
| 17 | +- Full crud functionality |
| 18 | +- Basic migrations |
| 19 | +- Sqlite integration |
| 20 | +- Native Pydantic model support with no limitations |
| 21 | + |
| 22 | +However, the project is currently not mature enough to be used in larger production project. |
| 23 | + |
| 24 | +### INSTALLATION |
| 25 | + |
| 26 | +DBTogo requires Python 3.12 or newer and Pydantic. |
| 27 | +It can be installed with `pip install dbtogo` |
| 28 | + |
| 29 | +### EXAMPLE |
| 30 | +``` |
| 31 | +from dbtogo import DBEngineFactory, DBModel |
| 32 | +
|
| 33 | +
|
| 34 | +class Duck(DBModel): |
| 35 | + duck_id: int | None = None |
| 36 | + name: str |
| 37 | + color: str = "Brown" |
| 38 | + age: int | None = None |
| 39 | + shopping_list: list[str] = ["bread crumbs"] |
| 40 | + children: list["Duck"] = [] |
| 41 | +
|
| 42 | + def quack(self): |
| 43 | + print(f"Hi! I am {self.name} age {self.age} quack!") |
| 44 | +``` |
| 45 | +DBTogos base class DBModel is just a regular python class (with attributes defined on class level for Pydantic support). It can even have objects as attributes! |
| 46 | + |
| 47 | +``` |
| 48 | + engine = DBEngineFactory.create_sqlite3_engine("test.db") |
| 49 | +
|
| 50 | + Duck.bind(engine, primary_key="duck_id", unique=["name"]) |
| 51 | +``` |
| 52 | + |
| 53 | +To use the DBModel class, you need to bind it with a DBEngine. This also allows you to set unique fields and specify the primary key. Nullable fields are set by including None in the type annotation of the nullable field. |
| 54 | + |
| 55 | +``` |
| 56 | + mc_duck_junior = Duck( |
| 57 | + name="Junior", |
| 58 | + age=15, |
| 59 | + shopping_list=["rohlik", "gothaj"], |
| 60 | + ) |
| 61 | + mc_duck_junior.save() |
| 62 | +
|
| 63 | + mc_duck = Duck(name="McDuck", color="Yellow", age=45, children=[mc_duck_junior]) |
| 64 | + mc_duck.save() |
| 65 | +
|
| 66 | +``` |
| 67 | +Calling the .save() method saves the object to the db. If the object is already stored in the db, it instead updates it. You can modify any attribute of the object including the primary key! |
| 68 | + |
| 69 | +``` |
| 70 | + mc_duck = Duck.get(name="McDuck") |
| 71 | + mc_duck.children[0].quack() |
| 72 | +``` |
| 73 | +.get() method returns the object from the db (or identity cache). You can still access any of attributes/methods of the class (even the non primitive ones)! |
| 74 | + |
| 75 | +### License |
| 76 | +This project uses the MIT license. |
0 commit comments