Exemplo n.º 1
0
 def __init__(self, session, **kwargs):
     # Construct infrastructure objects for storing events with SQLAlchemy
     entity_active_record_strategy = SQLAlchemyActiveRecordStrategy(
         active_record_class=IntegerSequencedItemRecord, session=session)
     snapshot_active_record_strategy = SQLAlchemyActiveRecordStrategy(
         active_record_class=SnapshotRecord, session=session)
     # Initialize
     super(UnitOfWork, self).__init__(
         entity_active_record_strategy=entity_active_record_strategy,
         snapshot_active_record_strategy=snapshot_active_record_strategy,
         **kwargs)
     # Construct repositories
     self.snapshot_strategy = EventSourcedSnapshotStrategy(
         event_store=self.snapshot_event_store)
     # -- User
     self.users = EventSourcedRepository(
         event_store=self.entity_event_store,
         mutator=User._mutate,
         snapshot_strategy=self.snapshot_strategy)
     self.user_collections = CollectionRepository(
         event_store=self.entity_event_store)
     self.user_projection_policy = UserProjectionPolicy(
         user_collections=self.user_collections)
     self.user_snapshotting_policy = DomainSnapshottingPolicy(
         repository=self.users)
Exemplo n.º 2
0
def construct_sqlalchemy_eventstore(
    session,
    sequenced_item_class=StoredEvent,
    sequence_id_attr_name=None,
    position_attr_name=None,
    json_encoder_class=ObjectJSONEncoder,
    json_decoder_class=ObjectJSONDecoder,
    always_encrypt=False,
    cipher=None,
    active_record_class=StoredEventRecord,
):
    sequenced_item_mapper = SequencedItemMapper(
        sequenced_item_class=sequenced_item_class,
        sequence_id_attr_name=sequence_id_attr_name,
        position_attr_name=position_attr_name,
        json_encoder_class=json_encoder_class,
        json_decoder_class=json_decoder_class,
        always_encrypt=always_encrypt,
        cipher=cipher,
    )
    active_record_strategy = SQLAlchemyActiveRecordStrategy(
        session=session,
        active_record_class=active_record_class,
        sequenced_item_class=sequenced_item_class,
    )
    event_store = EventStore(
        active_record_strategy=active_record_strategy,
        sequenced_item_mapper=sequenced_item_mapper,
    )
    return event_store
Exemplo n.º 3
0
def init_example_application_with_sqlalchemy():
    init_example_application(
        entity_active_record_strategy=SQLAlchemyActiveRecordStrategy(
            active_record_class=IntegerSequencedItem,
            session=db.session,
        )
    )
Exemplo n.º 4
0
def init_kanban_application_w_sqlalchemy(db_host=None):
    datastore.init_database(uri=db_host)
    db = get_database()
    db.setup_connection()
    db.setup_tables()
    init_kanban_application(
        entity_active_record_strategy=SQLAlchemyActiveRecordStrategy(
            active_record_class=IntegerSequencedItemRecord,
            session=db.session))
Exemplo n.º 5
0
    def __init__(self, **kwargs):
        session = get_session().session
        entity_active_record_strategy = SQLAlchemyActiveRecordStrategy(
            active_record_class=IntegerSequencedItemRecord, session=session)
        snapshot_active_record_strategy = SQLAlchemyActiveRecordStrategy(
            active_record_class=SnapshotRecord, session=session)
        super(UnitOfWork, self).__init__(
            entity_active_record_strategy=entity_active_record_strategy,
            snapshot_active_record_strategy=snapshot_active_record_strategy,
            **kwargs)

        self.snapshot_strategy = EventSourcedSnapshotStrategy(
            event_store=self.snapshot_event_store)

        self.users = EventSourcedRepository(
            event_store=self.entity_event_store,
            mutator=User._mutate,
            snapshot_strategy=self.snapshot_strategy)

        self.snapshotting_policy = KanbanSnapshottingPolicy(
            repository=self.users)
Exemplo n.º 6
0
 def __init__(self, datastore):
     self.event_store = EventStore(
         active_record_strategy=SQLAlchemyActiveRecordStrategy(
             datastore=datastore,
             active_record_class=SqlExtendedIntegerSequencedItem,
             sequenced_item_class=ExtendedSequencedItem,
         ),
         sequenced_item_mapper=ExtendedSequencedItemMapper(
             sequenced_item_class=ExtendedSequencedItem,
             event_sequence_id_attr='entity_id',
             event_position_attr='entity_version',
         ))
     self.repository = ExampleRepository(event_store=self.event_store, )
     self.persistence_policy = PersistencePolicy(self.event_store)
 def __init__(self, session):
     self.event_store = EventStore(
         active_record_strategy=SQLAlchemyActiveRecordStrategy(
             session=session,
             active_record_class=StoredEventRecord,
             sequenced_item_class=StoredEvent,
         ),
         sequenced_item_mapper=SequencedItemMapper(
             sequenced_item_class=StoredEvent,
             sequence_id_attr_name='originator_id',
             position_attr_name='originator_version',
         ))
     self.repository = ExampleRepository(event_store=self.event_store, )
     self.persistence_policy = PersistencePolicy(self.event_store)
Exemplo n.º 8
0
    def setUp(self):
        assert_event_handlers_empty()
        super(TestEventPlayer, self).setUp()

        self.datastore.setup_connection()
        self.datastore.setup_tables()

        # Setup an event store for versioned entity events.
        self.entity_event_store = EventStore(
            active_record_strategy=SQLAlchemyActiveRecordStrategy(
                session=self.datastore.session,
                active_record_class=IntegerSequencedItemRecord,
                sequenced_item_class=SequencedItem,
            ),
            sequenced_item_mapper=SequencedItemMapper(
                sequenced_item_class=SequencedItem,
                sequence_id_attr_name='originator_id',
                position_attr_name='originator_version',
            ),
        )

        # Setup an event store for snapshots.
        self.snapshot_store = EventStore(
            active_record_strategy=SQLAlchemyActiveRecordStrategy(
                session=self.datastore.session,
                active_record_class=SnapshotRecord,
                sequenced_item_class=SequencedItem,
            ),
            sequenced_item_mapper=SequencedItemMapper(
                sequenced_item_class=SequencedItem,
                sequence_id_attr_name='originator_id',
                position_attr_name='originator_version',
            ),
        )
        self.entity_persistence_policy = None
        self.snapshot_persistence_policy = None
Exemplo n.º 9
0
    def setUp(self):
        assert_event_handlers_empty()
        super(TestEventPlayer, self).setUp()

        self.datastore.setup_connection()
        self.datastore.setup_tables()

        # Setup an event store for version entity events.
        self.version_entity_event_store = EventStore(
            active_record_strategy=SQLAlchemyActiveRecordStrategy(
                datastore=self.datastore,
                active_record_class=SqlIntegerSequencedItem,
                sequenced_item_class=SequencedItem,
            ),
            sequenced_item_mapper=SequencedItemMapper(
                sequenced_item_class=SequencedItem,
                event_sequence_id_attr='entity_id',
                event_position_attr='entity_version',
            ),
        )

        # Setup an event store for timestamp entity events.
        self.timestamp_entity_event_store = EventStore(
            active_record_strategy=SQLAlchemyActiveRecordStrategy(
                datastore=self.datastore,
                active_record_class=SqlTimestampSequencedItem,
                sequenced_item_class=SequencedItem,
            ),
            sequenced_item_mapper=SequencedItemMapper(
                sequenced_item_class=SequencedItem,
                event_sequence_id_attr='entity_id',
                event_position_attr='timestamp',
            ),
        )

        self.policy = None
Exemplo n.º 10
0
    def test(self):
        self.datastore.setup_connection()
        self.datastore.setup_tables()
        active_record_strategy = SQLAlchemyActiveRecordStrategy(
            active_record_class=IntegerSequencedItemRecord,
            session=self.datastore.session,
        )

        # Can't get the single instance before it has been constructed.
        with self.assertRaises(AssertionError):
            get_example_application()

        # Construct single instance.
        init_example_application(
            entity_active_record_strategy=active_record_strategy)

        # Can't construct single instance twice.
        with self.assertRaises(AssertionError):
            init_example_application(
                entity_active_record_strategy=active_record_strategy)

        # Get the single instance.
        app1 = get_example_application()
        app2 = get_example_application()
        self.assertEqual(id(app1), id(app2))

        # Close single instance.
        close_example_application()

        # Can't get the single instance before it has been constructed.
        with self.assertRaises(AssertionError):
            get_example_application()

        # Construct single instance.
        init_example_application(
            entity_active_record_strategy=active_record_strategy)

        # Can't construct single instance twice.
        with self.assertRaises(AssertionError):
            init_example_application(
                entity_active_record_strategy=active_record_strategy)

        # Get the single instance.
        app1 = get_example_application()
        app2 = get_example_application()
        self.assertEqual(id(app1), id(app2))
Exemplo n.º 11
0
 def __init__(self, datastore):
     event_store = EventStore(
         active_record_strategy=SQLAlchemyActiveRecordStrategy(
             session=datastore.session,
             active_record_class=IntegerSequencedItemRecord,
         ),
         sequenced_item_mapper=SequencedItemMapper(
             sequence_id_attr_name='originator_id',
             position_attr_name='originator_version',
         ))
     self.aggregate_repository = EventSourcedRepository(
         mutator=ExampleAggregateRoot._mutate,
         event_store=event_store,
     )
     self.persistence_policy = PersistencePolicy(
         event_type=ExampleAggregateRoot.Event,
         event_store=event_store,
     )
Exemplo n.º 12
0
def construct_integer_sequenced_active_record_strategy(datastore):
    return SQLAlchemyActiveRecordStrategy(
        active_record_class=IntegerSequencedItemRecord,
        sequenced_item_class=SequencedItem,
        session=datastore.session,
    )
Exemplo n.º 13
0
def construct_snapshot_active_record_strategy(datastore):
    return SQLAlchemyActiveRecordStrategy(
        active_record_class=SnapshotRecord,
        sequenced_item_class=SequencedItem,
        session=datastore.session,
    )
def construct_timestamp_sequenced_active_record_strategy(datastore):
    return SQLAlchemyActiveRecordStrategy(
        active_record_class=SqlTimestampSequencedItem,
        sequenced_item_class=SequencedItem,
        datastore=datastore,
    )