1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Generalize alchemy

This commit is contained in:
Steffo 2019-03-29 15:48:13 +01:00
parent 090896b9ee
commit 571f381c75

View file

@ -1,17 +1,19 @@
import typing
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from .tables import Royal
from ..utils import classdictjanitor
class Alchemy:
def __init__(self, database_uri: str = "sqlite://"):
def __init__(self, database_uri: str = "sqlite://", tables: typing.Optional[typing.List] = None):
self.engine = create_engine(database_uri)
self.Base = declarative_base(bind=self.engine)
self.Session = sessionmaker(bind=self.engine)
self._create_tables()
self._create_tables(tables)
def _create_tables(self):
self.Royal = type("Royal", (self.Base,), classdictjanitor(Royal))
def _create_tables(self, tables: typing.Optional[typing.List]):
for table in tables:
name = table.__name__
self.__setattr__(name, type(name, (self.Base,), classdictjanitor(table)))
self.Base.metadata.create_all()