@@ -427,6 +427,28 @@ def get_user(session:Session,id:int):
427427 await executor .commit ()
428428 return result
429429
430+ async def refresh (self , instance , attribute_names = None , with_for_update = None , session : Optional [AsyncSession ] = None ):
431+ """
432+ Refresh the attributes of the given instance from the database.
433+ Args:
434+ instance: The instance to be refreshed.
435+ attribute_names: Optional list of attribute names to refresh.
436+ with_for_update: optional boolean ``True`` indicating FOR UPDATE should be used,
437+ or may be a dictionary containing flags to
438+ indicate a more specific set of FOR UPDATE flags for the SELECT;
439+ flags should match the parameters of :meth:`_query.Query.with_for_update`.
440+ Supersedes the :paramref:`.Session.refresh.lockmode` parameter.
441+ session: If not specified, an `AsyncSession` is created.
442+ """
443+ need_close = False
444+ if session is None or not isinstance (session , AsyncSession ):
445+ session = self .session
446+ if session is None :
447+ need_close = True
448+ session = self .session_maker ()
449+ async with ExecutorContextManager (session , need_close = need_close ) as session :
450+ await session .refresh (instance , attribute_names , with_for_update )
451+
430452
431453class Database (AbcAsyncDatabase ):
432454 """`sqlalchemy` synchronous database client"""
@@ -622,6 +644,16 @@ def run_sync(
622644 executor .commit ()
623645 return result
624646
647+ def refresh (self , instance , attribute_names = None , with_for_update = None , session : Optional [Session ] = None ):
648+ need_close = False
649+ if session is None or not isinstance (session , Session ):
650+ session = self .session
651+ if session is None :
652+ need_close = True
653+ session = self .session_maker ()
654+ with ExecutorContextManager (session , need_close = need_close ) as session :
655+ session .refresh (instance , attribute_names = attribute_names , with_for_update = with_for_update )
656+
625657
626658class ExecutorContextManager :
627659 """Actuator context manager, optionally closing the executor"""
0 commit comments