mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-15 06:50:58 +00:00
add database connect config
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import pip
|
||||||
|
import sqlalchemy.ext.asyncio as sqlalchemy_asyncio
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from .. import database
|
||||||
|
|
||||||
|
|
||||||
|
@database.manager_class('postgresql')
|
||||||
|
class PostgreSQLDatabaseManager(database.BaseDatabaseManager):
|
||||||
|
"""PostgreSQL database manager"""
|
||||||
|
|
||||||
|
async def initialize(self) -> None:
|
||||||
|
|
||||||
|
# default to PostgreSQL with asyncpg driver
|
||||||
|
try:
|
||||||
|
__import__("asyncpg")
|
||||||
|
except ImportError:
|
||||||
|
print('以下依赖包未安装,将自动安装,请完成后重启程序:')
|
||||||
|
print(
|
||||||
|
'The dependence package asyncpg is missing, it will be installed automatically, please restart the program after completion:'
|
||||||
|
)
|
||||||
|
pip.main(['install', "asyncpg"])
|
||||||
|
print('已自动安装缺失的依赖包 asyncpg ,请重启程序。')
|
||||||
|
print('The missing dependence asyncpg have been installed automatically, please restart the program.')
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
engine_url = self.ap.instance_config.data['system'].get('database', {}).get('engine_url', 'postgresql+asyncpg://root:***@127.0.0.1:5432/postgres')
|
||||||
|
self.engine = sqlalchemy_asyncio.create_async_engine(engine_url)
|
||||||
@@ -10,5 +10,5 @@ class SQLiteDatabaseManager(database.BaseDatabaseManager):
|
|||||||
"""SQLite database manager"""
|
"""SQLite database manager"""
|
||||||
|
|
||||||
async def initialize(self) -> None:
|
async def initialize(self) -> None:
|
||||||
sqlite_path = 'data/langbot.db'
|
engine_url = self.ap.instance_config.data['system'].get('database', {}).get('engine_url', 'sqlite+aiosqlite:///data/langbot.db')
|
||||||
self.engine = sqlalchemy_asyncio.create_async_engine(f'sqlite+aiosqlite:///{sqlite_path}')
|
self.engine = sqlalchemy_asyncio.create_async_engine(engine_url)
|
||||||
|
|||||||
@@ -38,9 +38,12 @@ class PersistenceManager:
|
|||||||
async def initialize(self):
|
async def initialize(self):
|
||||||
self.ap.logger.info('Initializing database...')
|
self.ap.logger.info('Initializing database...')
|
||||||
|
|
||||||
|
database_type = self.ap.instance_config.data['system'].get('database', {}).get('type', 'sqlite')
|
||||||
for manager in database.preregistered_managers:
|
for manager in database.preregistered_managers:
|
||||||
self.db = manager(self.ap)
|
if manager.name == database_type:
|
||||||
await self.db.initialize()
|
self.db = manager(self.ap)
|
||||||
|
await self.db.initialize()
|
||||||
|
break
|
||||||
|
|
||||||
await self.create_tables()
|
await self.create_tables()
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ system:
|
|||||||
jwt:
|
jwt:
|
||||||
expire: 604800
|
expire: 604800
|
||||||
secret: ''
|
secret: ''
|
||||||
|
database:
|
||||||
|
type: sqlite
|
||||||
|
engine_url: 'sqlite+aiosqlite:///data/langbot.db'
|
||||||
vdb:
|
vdb:
|
||||||
use: chroma
|
use: chroma
|
||||||
qdrant:
|
qdrant:
|
||||||
|
|||||||
Reference in New Issue
Block a user