@@ -35,17 +35,18 @@ $ pip install -U db_first
3535### Full example
3636
3737``` python
38- from uuid import UUID
39-
4038from db_first import BaseCRUD
4139from db_first.base_model import ModelMixin
40+ from db_first.decorators import Validation
4241from db_first.mixins import CreateMixin
4342from db_first.mixins import DeleteMixin
4443from db_first.mixins import ReadMixin
4544from db_first.mixins import UpdateMixin
4645from marshmallow import fields
4746from marshmallow import Schema
4847from sqlalchemy import create_engine
48+ from sqlalchemy import Result
49+ from sqlalchemy.exc import NoResultFound
4950from sqlalchemy.orm import declarative_base
5051from sqlalchemy.orm import Mapped
5152from sqlalchemy.orm import mapped_column
@@ -64,60 +65,63 @@ class Items(ModelMixin, Base):
6465Base.metadata.create_all(engine)
6566
6667
67- class InputSchemaOfCreate (Schema ):
68- data = fields.String ()
68+ class IdSchema (Schema ):
69+ id = fields.UUID ()
6970
7071
71- class InputSchemaOfUpdate ( InputSchemaOfCreate ):
72- id = fields.UUID ()
72+ class SchemaOfCreate ( Schema ):
73+ data = fields.String ()
7374
7475
75- class InputSchemaOfRead ( Schema ):
76- id = fields.UUID()
76+ class SchemaOfUpdate ( IdSchema , SchemaOfCreate ):
77+ """ Update item schema. """
7778
7879
79- class OutputSchema (InputSchemaOfUpdate ):
80+ class OutputSchema (SchemaOfUpdate ):
8081 created_at = fields.DateTime()
8182
8283
8384class ItemController (CreateMixin , ReadMixin , UpdateMixin , DeleteMixin , BaseCRUD ):
8485 class Meta :
8586 session = session
8687 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
9488 sortable = [' created_at' ]
9589
90+ @Validation.input (SchemaOfCreate)
91+ @Validation.output (OutputSchema, serialize = True )
92+ def create (self , ** data ) -> Result:
93+ return super ().create_object(** data)
9694
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)
99104
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)
104108
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)
109109
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)
116115
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)
121118
119+ updated_item = item_controller.update(id = new_item[' id' ], data = ' updated_first' )
120+ print (' Item as dict:' , updated_item)
122121
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)
123127```
0 commit comments