-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
51 lines (35 loc) · 1.09 KB
/
example.py
File metadata and controls
51 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from dbtogo import DBEngineFactory, DBModel
class Duck(DBModel):
duck_id: int | None = None
name: str
color: str = "Brown"
age: int | None = None
shopping_list: list[str] = ["bread crumbs"]
children: list["Duck"] = []
@classmethod
def bind(cls, engine):
super().bind(engine, primary_key="duck_id", unique=["name"])
def quack(self):
print(f"Hi! I am {self.name} age {self.age} quack!")
def main():
engine = DBEngineFactory.create_sqlite3_engine("test.db")
Duck.bind(engine)
mc_duck_junior = Duck(
name="Junior",
age=15,
shopping_list=["rohlik", "gothaj"],
)
mc_duck_junior.save()
mc_duck = Duck(name="McDuck", color="Yellow", age=45, children=[mc_duck_junior])
mc_duck.save()
mc_duck = Duck.get(name="McDuck")
mc_duck.children[0].quack()
mc_duck.name = "McDuckyDuck"
mc_duck.save()
mc_duck = Duck.get(duck_id=mc_duck.duck_id)
print(mc_duck.name)
print([x.name for x in Duck.all()])
mc_duck.delete()
mc_duck_junior.delete()
if __name__ == "__main__":
main()