def bootstrap( start_orm: bool = True, uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), notifications: AbstractNotifications = None, publish: Callable = redis_eventpublisher.publish, ) -> messagebus.MessageBus: if notifications is None: notifications = EmailNotifications() if start_orm: orm.start_mappers() dependencies = { 'uow': uow, 'notifications': notifications, 'publish': publish } injected_event_handlers = { event_type: [ inject_dependencies(handler, dependencies) for handler in event_handlers ] for event_type, event_handlers in handlers.EVENT_HANDLERS.items() } injected_command_handlers = { command_type: inject_dependencies(handler, dependencies) for command_type, handler in handlers.COMMAND_HANDLERS.items() } return messagebus.MessageBus( uow=uow, event_handlers=injected_event_handlers, command_handlers=injected_command_handlers, )
def main(): orm.start_mappers() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe('change_batch_quantity') for m in pubsub.listen(): handle_change_batch_quantity(m)
def bootstrap( start_orm: bool = True, uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), send_mail: Callable = email.send_mail, publish: Callable = event_publisher.publish, ) -> messagebus.MessageBus: if start_orm: orm.start_mappers() dependencies = {"uow": uow, "send_mail": send_mail, "publish": publish} injected_event_handlers = { event_type: [ inject_dependencies(event_handler, dependencies) for event_handler in event_handlers ] for event_type, event_handlers in handlers.EVENT_HANDLERS.items() } injected_commans_handlers = { command_type: inject_dependencies(command_handler, dependencies) for command_type, command_handler in handlers.COMMAND_HANDLERS.items() } return messagebus.MessageBus( uow=uow, event_handlers=injected_event_handlers, command_handlers=injected_commans_handlers, )
def postgres_session_factory(postgres_db: engine.Engine): clear_mappers() start_mappers(postgres_db) # tear-up yield orm.sessionmaker( bind=postgres_db, autoflush=False ) # deliver to next fixture clear_mappers() # tear-down
def session_factory(in_memory_db): orm.start_mappers() # factory to generate new Session objects: # https://docs.sqlalchemy.org/en/14/orm/session_api.html#sqlalchemy.orm.sessionmaker yield sessionmaker(bind=in_memory_db) # bind the session to a connection # https://docs.sqlalchemy.org/en/14/orm/mapping_api.html#sqlalchemy.orm.clear_mappers clear_mappers( ) # remove all mappers from all classes, not for normal use, b/c:
def bootstrap( start_orm: bool = True, uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), notifications: AbstractNotifications = None, publish: Callable = redis_eventpublisher.publish, ) -> messagebus.MessageBus: if notifications is None: notifications = EmailNotifications() if start_orm: orm.start_mappers()
def postgres_session_factory(postgres_db): start_mappers() yield sessionmaker(bind=postgres_db) clear_mappers()
def session_factory(in_memory_db): start_mappers() yield sessionmaker(bind=in_memory_db) clear_mappers()
def mappers(): start_mappers() yield clear_mappers()
from flask import Flask, jsonify, request from allocation.domain import commands from allocation.service import handlers, unit_of_work, message_bus from allocation import views from allocation.adapters import orm orm.start_mappers() app = Flask(__name__) @app.route("/", methods=["GET"]) # type: ignore def default() -> tuple: return jsonify({"status": "on-line"}), 200 @app.route("/allocate", methods=["POST"]) # type: ignore def allocate_endpoint() -> tuple: # batches = repository.SqlAlchemyRepository(session).list() # line = model.OrderLine() try: command = commands.Allocate( order_id=request.json["order_id"], sku=request.json["sku"], qty=request.json["qty"], ) uow = unit_of_work.SqlAlchemyUnitOfWork() message_bus.handle(command, uow) except handlers.InvalidSku as ex: return jsonify({"message": ex.message}), 400 return jsonify({"status": "ok"}), 202
def postgres_session(postgres_db): start_mappers() yield sessionmaker(bind=postgres_db)() clear_mappers()
def sqlite_session_factory(sqlite_db): clear_mappers() start_mappers(sqlite_db) yield orm.sessionmaker(bind=sqlite_db, autoflush=False, autocommit=False) clear_mappers()