@@ -35,17 +35,18 @@ $ pip install -U db_first
35
35
### Full example
36
36
37
37
``` python
38
- from uuid import UUID
39
-
40
38
from db_first import BaseCRUD
41
39
from db_first.base_model import ModelMixin
40
+ from db_first.decorators import Validation
42
41
from db_first.mixins import CreateMixin
43
42
from db_first.mixins import DeleteMixin
44
43
from db_first.mixins import ReadMixin
45
44
from db_first.mixins import UpdateMixin
46
45
from marshmallow import fields
47
46
from marshmallow import Schema
48
47
from sqlalchemy import create_engine
48
+ from sqlalchemy import Result
49
+ from sqlalchemy.exc import NoResultFound
49
50
from sqlalchemy.orm import declarative_base
50
51
from sqlalchemy.orm import Mapped
51
52
from sqlalchemy.orm import mapped_column
@@ -64,60 +65,63 @@ class Items(ModelMixin, Base):
64
65
Base.metadata.create_all(engine)
65
66
66
67
67
- class InputSchemaOfCreate (Schema ):
68
- data = fields.String ()
68
+ class IdSchema (Schema ):
69
+ id = fields.UUID ()
69
70
70
71
71
- class InputSchemaOfUpdate ( InputSchemaOfCreate ):
72
- id = fields.UUID ()
72
+ class SchemaOfCreate ( Schema ):
73
+ data = fields.String ()
73
74
74
75
75
- class InputSchemaOfRead ( Schema ):
76
- id = fields.UUID()
76
+ class SchemaOfUpdate ( IdSchema , SchemaOfCreate ):
77
+ """ Update item schema. """
77
78
78
79
79
- class OutputSchema (InputSchemaOfUpdate ):
80
+ class OutputSchema (SchemaOfUpdate ):
80
81
created_at = fields.DateTime()
81
82
82
83
83
84
class ItemController (CreateMixin , ReadMixin , UpdateMixin , DeleteMixin , BaseCRUD ):
84
85
class Meta :
85
86
session = session
86
87
model = Items
87
- input_schema_of_create = InputSchemaOfCreate
88
- input_schema_of_update = InputSchemaOfUpdate
89
- output_schema_of_create = OutputSchema
90
- input_schema_of_read = InputSchemaOfRead
91
- output_schema_of_read = OutputSchema
92
- output_schema_of_update = OutputSchema
93
- schema_of_paginate = OutputSchema
94
88
sortable = [' created_at' ]
95
89
90
+ @Validation.input (SchemaOfCreate)
91
+ @Validation.output (OutputSchema, serialize = True )
92
+ def create (self , ** data ) -> Result:
93
+ return super ().create_object(** data)
96
94
97
- if __name__ == ' __main__' :
98
- item = ItemController()
95
+ @Validation.input (IdSchema, keys = [' id' ])
96
+ @Validation.output (OutputSchema, serialize = True )
97
+ def read (self , ** data ) -> Result:
98
+ return super ().read_object(data[' id' ])
99
+
100
+ @Validation.input (SchemaOfUpdate)
101
+ @Validation.output (OutputSchema, serialize = True )
102
+ def update (self , ** data ) -> Result:
103
+ return super ().update_object(** data)
99
104
100
- first_new_item = item.create({' data' : ' first' }, deserialize = True )
101
- print (' Item as object:' , first_new_item)
102
- second_new_item = item.create({' data' : ' second' }, deserialize = True , serialize = True )
103
- print (' Item as dict:' , second_new_item)
105
+ @Validation.input (IdSchema, keys = [' id' ])
106
+ def delete (self , ** data ) -> None :
107
+ super ().delete_object(** data)
104
108
105
- first_item = item.read({' id' : first_new_item.id})
106
- print (' Item as object:' , first_item)
107
- first_item = item.read({' id' : first_new_item.id})
108
- print (' Item as dict:' , first_item)
109
109
110
- updated_first_item = item.update(data = {' id' : first_new_item.id, ' data' : ' updated_first' })
111
- print (' Item as object:' , updated_first_item)
112
- updated_second_item = item.update(
113
- data = {' id' : UUID(second_new_item[' id' ]), ' data' : ' updated_second' }, serialize = True
114
- )
115
- print (' Item as dict:' , updated_second_item)
110
+ if __name__ == ' __main__' :
111
+ item_controller = ItemController()
112
+
113
+ new_item = item_controller.create(data = ' first' )
114
+ print (' Item as dict:' , new_item)
116
115
117
- items = item.paginate(sort_created_at = ' desc' )
118
- print (' Items as objects:' , items)
119
- items = item.paginate(sort_created_at = ' desc' , serialize = True )
120
- print (' Items as dicts:' , items)
116
+ item = item_controller.read(id = new_item[' id' ])
117
+ print (' Item as dict:' , item)
121
118
119
+ updated_item = item_controller.update(id = new_item[' id' ], data = ' updated_first' )
120
+ print (' Item as dict:' , updated_item)
122
121
122
+ item_controller.delete(id = new_item[' id' ])
123
+ try :
124
+ item = item_controller.read(id = new_item[' id' ])
125
+ except NoResultFound:
126
+ print (' Item deleted:' , item)
123
127
```
0 commit comments