mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +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.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()
|
||||
|
|
Loading…
Reference in a new issue