11"""This module is a CRUD interface between resource managers and the sqlalchemy ORM"""
22import logging
3- from typing import TYPE_CHECKING , Any , Iterable , List , Optional , Tuple , Type , Union
3+ from typing import TYPE_CHECKING , Any , Iterable , List , Literal , Optional , Tuple , Type , Union
44
55from sqlalchemy import delete , func , select
66from sqlalchemy .exc import DBAPIError , IntegrityError , MissingGreenlet , NoResultFound
4545log = logging .getLogger (__name__ )
4646
4747ModelTypeOneOrMany = Union [TypeModel , list [TypeModel ]]
48+ ActionTrigger = Literal ["create" , "update" ]
4849
4950
5051class SqlalchemyDataLayer (BaseDataLayer ):
@@ -141,13 +142,15 @@ async def link_relationship_object(
141142 obj : TypeModel ,
142143 relation_name : str ,
143144 related_data : Optional [ModelTypeOneOrMany ],
145+ action_trigger : ActionTrigger ,
144146 ):
145147 """
146148 Links target object with relationship object or objects
147149
148150 :param obj:
149151 :param relation_name:
150152 :param related_data:
153+ :param action_trigger: indicates which one operation triggered relationships applying
151154 """
152155 # todo: relation name may be different?
153156 setattr (obj , relation_name , related_data )
@@ -204,12 +207,18 @@ async def get_related_data_to_link(
204207 id_value = relationship_in .data .id ,
205208 )
206209
207- async def apply_relationships (self , obj : TypeModel , data_create : BaseJSONAPIItemInSchema ) -> None :
210+ async def apply_relationships (
211+ self ,
212+ obj : TypeModel ,
213+ data_create : BaseJSONAPIItemInSchema ,
214+ action_trigger : ActionTrigger ,
215+ ) -> None :
208216 """
209217 Handles relationships passed in request
210218
211219 :param obj:
212220 :param data_create:
221+ :param action_trigger: indicates which one operation triggered relationships applying
213222 :return:
214223 """
215224 relationships : "PydanticBaseModel" = data_create .relationships
@@ -245,7 +254,7 @@ async def apply_relationships(self, obj: TypeModel, data_create: BaseJSONAPIItem
245254 )
246255
247256 await self .check_object_has_relationship_or_raise (obj , relation_name )
248- await self .link_relationship_object (obj , relation_name , related_data )
257+ await self .link_relationship_object (obj , relation_name , related_data , action_trigger )
249258
250259 async def create_object (self , data_create : BaseJSONAPIItemInSchema , view_kwargs : dict ) -> TypeModel :
251260 """
@@ -262,7 +271,7 @@ async def create_object(self, data_create: BaseJSONAPIItemInSchema, view_kwargs:
262271 await self .before_create_object (model_kwargs = model_kwargs , view_kwargs = view_kwargs )
263272
264273 obj = self .model (** model_kwargs )
265- await self .apply_relationships (obj , data_create )
274+ await self .apply_relationships (obj , data_create , action_trigger = "create" )
266275
267276 self .session .add (obj )
268277 try :
@@ -388,7 +397,7 @@ async def update_object(
388397 """
389398 new_data = data_update .attributes .dict (exclude_unset = True )
390399
391- await self .apply_relationships (obj , data_update )
400+ await self .apply_relationships (obj , data_update , action_trigger = "update" )
392401
393402 await self .before_update_object (obj , model_kwargs = new_data , view_kwargs = view_kwargs )
394403
0 commit comments