mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
Generalize alchemy
This commit is contained in:
parent
090896b9ee
commit
571f381c75
1 changed files with 7 additions and 5 deletions
|
@ -1,17 +1,19 @@
|
||||||
|
import typing
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from .tables import Royal
|
|
||||||
from ..utils import classdictjanitor
|
from ..utils import classdictjanitor
|
||||||
|
|
||||||
|
|
||||||
class Alchemy:
|
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.engine = create_engine(database_uri)
|
||||||
self.Base = declarative_base(bind=self.engine)
|
self.Base = declarative_base(bind=self.engine)
|
||||||
self.Session = sessionmaker(bind=self.engine)
|
self.Session = sessionmaker(bind=self.engine)
|
||||||
self._create_tables()
|
self._create_tables(tables)
|
||||||
|
|
||||||
def _create_tables(self):
|
def _create_tables(self, tables: typing.Optional[typing.List]):
|
||||||
self.Royal = type("Royal", (self.Base,), classdictjanitor(Royal))
|
for table in tables:
|
||||||
|
name = table.__name__
|
||||||
|
self.__setattr__(name, type(name, (self.Base,), classdictjanitor(table)))
|
||||||
self.Base.metadata.create_all()
|
self.Base.metadata.create_all()
|
||||||
|
|
Loading…
Reference in a new issue