Added first version of database and orm
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
from abc import abstractmethod
|
||||
from abc import abstractmethod, ABC
|
||||
|
||||
from sqlalchemy import engine
|
||||
from sqlalchemy.orm import session
|
||||
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
|
||||
|
||||
class DatabaseConnectionBase(ServiceBase):
|
||||
class DatabaseConnectionBase(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
def __init__(self): pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
|
@@ -19,8 +19,6 @@ class DatabaseConnection(DatabaseConnectionBase):
|
||||
self._session: Optional[session] = None
|
||||
self._credentials: Optional[str] = None
|
||||
|
||||
self.create()
|
||||
|
||||
@property
|
||||
def engine(self) -> engine:
|
||||
return self._engine
|
||||
@@ -29,8 +27,6 @@ class DatabaseConnection(DatabaseConnectionBase):
|
||||
def session(self) -> session:
|
||||
return self._session
|
||||
|
||||
def create(self): pass
|
||||
|
||||
def connect(self, connection_string: str):
|
||||
try:
|
||||
self._engine = create_engine(connection_string)
|
||||
|
3
src/sh_edraft/database/context/__init__.py
Normal file
3
src/sh_edraft/database/context/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# imports:
|
||||
|
||||
from .database_context import DatabaseContext
|
3
src/sh_edraft/database/context/base/__init__.py
Normal file
3
src/sh_edraft/database/context/base/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# imports:
|
||||
|
||||
from .database_context_base import DatabaseContextBase
|
27
src/sh_edraft/database/context/base/database_context_base.py
Normal file
27
src/sh_edraft/database/context/base/database_context_base.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from sqlalchemy import engine
|
||||
from sqlalchemy.orm import session
|
||||
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
|
||||
|
||||
class DatabaseContextBase(ServiceBase):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def engine(self) -> engine: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def session(self) -> session: pass
|
||||
|
||||
@abstractmethod
|
||||
def connect(self, connection_string: str): pass
|
||||
|
||||
@abstractmethod
|
||||
def _create_tables(self): pass
|
45
src/sh_edraft/database/context/database_context.py
Normal file
45
src/sh_edraft/database/context/database_context.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from sqlalchemy import engine, Table
|
||||
from sqlalchemy.orm import session
|
||||
|
||||
from sh_edraft.database.connection.database_connection import DatabaseConnection
|
||||
from sh_edraft.database.connection.base.database_connection_base import DatabaseConnectionBase
|
||||
from sh_edraft.database.context.base.database_context_base import DatabaseContextBase
|
||||
from sh_edraft.database.model.dbmodel import DBModel
|
||||
from sh_edraft.database.model.database_settings import DatabaseSettings
|
||||
from sh_edraft.utils.console import Console
|
||||
|
||||
|
||||
class DatabaseContext(DatabaseContextBase):
|
||||
|
||||
def __init__(self, database_settings: DatabaseSettings):
|
||||
DatabaseContextBase.__init__(self)
|
||||
|
||||
self._db: DatabaseConnectionBase = DatabaseConnection(database_settings)
|
||||
self._tables: list[Table] = []
|
||||
|
||||
@property
|
||||
def engine(self) -> engine:
|
||||
return self._db.engine
|
||||
|
||||
@property
|
||||
def session(self) -> session:
|
||||
return self._db.session
|
||||
|
||||
def create(self):
|
||||
pass
|
||||
|
||||
def connect(self, connection_string: str):
|
||||
self._db.connect(connection_string)
|
||||
self._create_tables()
|
||||
|
||||
def _create_tables(self):
|
||||
try:
|
||||
for subclass in DBModel.__subclasses__():
|
||||
self._tables.append(subclass.__table__)
|
||||
|
||||
DBModel.metadata.drop_all(self._db.engine, self._tables)
|
||||
DBModel.metadata.create_all(self._db.engine, self._tables, checkfirst=True)
|
||||
Console.write_line(f'[{__name__}] Created tables', 'green')
|
||||
except Exception as e:
|
||||
Console.write_line(f'[{__name__}] Creating tables failed -> {e}', 'red')
|
||||
exit()
|
@@ -2,3 +2,4 @@
|
||||
|
||||
from .database_settings import DatabaseSettings
|
||||
from .database_settings_name import DatabaseSettingsName
|
||||
from .dbmodel import DBModel
|
||||
|
3
src/sh_edraft/database/model/dbmodel.py
Normal file
3
src/sh_edraft/database/model/dbmodel.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
DBModel: declarative_base = declarative_base()
|
@@ -1,15 +1,21 @@
|
||||
from abc import abstractmethod
|
||||
from abc import abstractmethod, ABC
|
||||
from collections import Callable
|
||||
from typing import Type
|
||||
|
||||
from sh_edraft.database.context.base.database_context_base import DatabaseContextBase
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
|
||||
|
||||
class ServiceProviderBase(ServiceBase):
|
||||
class ServiceProviderBase(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
def __init__(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def add_db_context(self, db_context: Type[DatabaseContextBase]): pass
|
||||
|
||||
@abstractmethod
|
||||
def get_db_context(self) -> Callable[DatabaseContextBase]: pass
|
||||
|
||||
@abstractmethod
|
||||
def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]): pass
|
||||
|
@@ -1,8 +1,9 @@
|
||||
from collections import Callable
|
||||
from inspect import signature, Parameter
|
||||
from typing import Type
|
||||
from typing import Type, Optional
|
||||
|
||||
from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase
|
||||
from sh_edraft.database.context.base.database_context_base import DatabaseContextBase
|
||||
from sh_edraft.hosting.base.application_runtime_base import ApplicationRuntimeBase
|
||||
from sh_edraft.service.providing.base.service_provider_base import ServiceProviderBase
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
@@ -13,6 +14,7 @@ class ServiceProvider(ServiceProviderBase):
|
||||
def __init__(self, app_runtime: ApplicationRuntimeBase):
|
||||
ServiceProviderBase.__init__(self)
|
||||
self._app_runtime: ApplicationRuntimeBase = app_runtime
|
||||
self._database_context: Optional[DatabaseContextBase] = None
|
||||
|
||||
self._transient_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
|
||||
self._scoped_services: dict[Type[ServiceBase], Type[ServiceBase]] = {}
|
||||
@@ -29,6 +31,9 @@ class ServiceProvider(ServiceProviderBase):
|
||||
if issubclass(parameter.annotation, ApplicationRuntimeBase):
|
||||
params.append(self._app_runtime)
|
||||
|
||||
elif issubclass(parameter.annotation, DatabaseContextBase):
|
||||
params.append(self._database_context)
|
||||
|
||||
elif issubclass(parameter.annotation, ServiceBase):
|
||||
params.append(self.get_service(parameter.annotation))
|
||||
|
||||
@@ -37,6 +42,12 @@ class ServiceProvider(ServiceProviderBase):
|
||||
|
||||
return service(*params)
|
||||
|
||||
def add_db_context(self, db_context: Type[DatabaseContextBase]):
|
||||
self._database_context = self._create_instance(db_context)
|
||||
|
||||
def get_db_context(self) -> Callable[DatabaseContextBase]:
|
||||
return self._database_context
|
||||
|
||||
def add_transient(self, service_type: Type[ServiceBase], service: Type[ServiceBase]):
|
||||
self._transient_services[service_type] = service
|
||||
|
||||
|
@@ -21,6 +21,7 @@ from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .console import Console
|
||||
from .credential_manager import CredentialManager
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=5)
|
||||
|
Reference in New Issue
Block a user