|
@@ -37,7 +37,7 @@ class DataStore(Generic[DataType]):
|
|
|
("_" + c.lower()) if "A" <= c <= "Z" else c for c in datatype.__name__ if c.isalnum()
|
|
|
).strip("_")
|
|
|
|
|
|
- async def setup(self):
|
|
|
+ async def _setup(self):
|
|
|
await self.connection.execute(
|
|
|
f'CREATE TABLE IF NOT EXISTS {self.table_name} ( \
|
|
|
key TEXT NOT NULL PRIMARY KEY, \
|
|
@@ -88,7 +88,6 @@ class DataFor(Injector[Optional[DataType]]):
|
|
|
key = await self.key.inject(message, context)
|
|
|
async with context.database() as db:
|
|
|
store = DataStore(self.datatype, db)
|
|
|
- await store.setup()
|
|
|
return await store.load_or(key, **self.kwargs)
|
|
|
|
|
|
|
|
@@ -97,9 +96,13 @@ class Data(InjectorWithCleanup[DataStore[DataType]]):
|
|
|
self.datatype = datatype
|
|
|
self.For = lambda key, **kw: DataFor(datatype, key, kw)
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ async def initialize(datatype: Type[DataType], connection: Connection):
|
|
|
+ store = DataStore(datatype, connection)
|
|
|
+ await store._setup()
|
|
|
+
|
|
|
async def inject(self, message: Message, context: Context) -> DataStore[DataType]:
|
|
|
store = DataStore(self.datatype, await context.database())
|
|
|
- await store.setup()
|
|
|
return store
|
|
|
|
|
|
async def cleanup(self, store: DataStore[DataType]):
|