-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generalize unit of work to ny type of repo
- Loading branch information
Showing
33 changed files
with
376 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from internal.infrastructure.data_storage.relational.context import ( # noqa: F401 | ||
RelationalContextType, | ||
RelationalContextMakerType, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy.orm import Session, sessionmaker | ||
|
||
RelationalContextType = Session | ||
type RelationalContextType = Session | ||
|
||
type RelationalContextMakerType = sessionmaker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from internal.infrastructure.data_storage.flat import FlatContext, FlatContextMaker | ||
from internal.infrastructure.data_storage.relational import ( | ||
RelationalContextType, | ||
RelationalContextMakerType, | ||
) | ||
|
||
|
||
class UniversalDataStorageContext: | ||
|
||
def __init__( | ||
self, relational_context: RelationalContextType, flat_context: FlatContext | ||
): | ||
self._relational_context = relational_context | ||
self._flat_context = flat_context | ||
|
||
@property | ||
def relational_context(self) -> RelationalContextType: | ||
return self._relational_context | ||
|
||
@property | ||
def flat_context(self) -> FlatContext: | ||
return self._flat_context | ||
|
||
def commit(self) -> None: | ||
self._relational_context.commit() | ||
self._flat_context.commit() | ||
|
||
def rollback(self) -> None: | ||
self._relational_context.rollback() | ||
self._flat_context.rollback() | ||
|
||
def close(self) -> None: | ||
self._relational_context.close() | ||
self._flat_context.close() | ||
|
||
|
||
class UniversalDataStorageContextMaker: | ||
|
||
def __init__( | ||
self, | ||
relational_context_maker: RelationalContextMakerType, | ||
flat_context_maker: FlatContextMaker, | ||
): | ||
self.relational_context_maker = relational_context_maker | ||
self.flat_context_maker = flat_context_maker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from internal.infrastructure.data_storage.flat import FlatContextMaker | ||
from internal.infrastructure.data_storage.relational import RelationalContextMakerType | ||
from internal.infrastructure.data_storage.universal_context import ( | ||
UniversalDataStorageContext, | ||
) | ||
from internal.uow import AbstractUnitOfWork | ||
from internal.uow.exception import NotActiveContextException | ||
|
||
|
||
class UnitOfWork(AbstractUnitOfWork[UniversalDataStorageContext]): | ||
|
||
def __init__( | ||
self, | ||
postgres_context_maker: RelationalContextMakerType, | ||
flat_context_maker: FlatContextMaker, | ||
): | ||
self.postgres_context_maker = postgres_context_maker | ||
self.flat_context_maker = flat_context_maker | ||
self._context: UniversalDataStorageContext | None = None | ||
|
||
def __enter__(self) -> UniversalDataStorageContext: | ||
db_session = self.postgres_context_maker() | ||
file_storage = self.flat_context_maker() | ||
self._context = UniversalDataStorageContext(db_session, file_storage) | ||
if self._context is None: | ||
raise NotActiveContextException() | ||
else: | ||
return self._context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.