83 lines
3.4 KiB
Python
83 lines
3.4 KiB
Python
# Copyright 2026 zhaoxi826
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import os
|
|
|
|
import ray
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlmodel import SQLModel
|
|
|
|
from pretor.core.database.module.individual import IndividualDatabase
|
|
from pretor.core.database.module.user import AuthDatabase
|
|
from pretor.core.database.module.provider import ProviderDatabase
|
|
|
|
@ray.remote
|
|
class PostgresDatabase:
|
|
def __init__(self):
|
|
user = os.environ.get('POSTGRES_USER')
|
|
password = os.environ.get('POSTGRES_PASSWORD')
|
|
host = os.environ.get('POSTGRES_HOST')
|
|
port = os.environ.get('POSTGRES_PORT')
|
|
database = os.environ.get('POSTGRES_DB')
|
|
database_url = f"postgresql+asyncpg://{user}:{password}@{host}:{port}/{database}"
|
|
self.async_engine = create_async_engine(database_url, echo=True)
|
|
self.async_session_maker = sessionmaker(self.async_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
self.auth_database = AuthDatabase(self.async_session_maker)
|
|
self.provider_database = ProviderDatabase(self.async_session_maker)
|
|
self.individual_database = IndividualDatabase(self.async_session_maker)
|
|
|
|
async def init_db(self) -> None:
|
|
async with self.async_engine.begin() as conn:
|
|
await conn.run_sync(SQLModel.metadata.create_all)
|
|
|
|
# provider_database操作
|
|
async def get_providers(self):
|
|
return await self.provider_database.get_provider()
|
|
|
|
async def add_provider(self, **kwargs):
|
|
return await self.provider_database.add_provider(**kwargs)
|
|
|
|
# auth_database操作
|
|
async def add_user(self, **kwargs):
|
|
return await self.auth_database.add_user(**kwargs)
|
|
|
|
async def change_password(self, **kwargs):
|
|
return await self.auth_database.change_password(**kwargs)
|
|
|
|
async def delete_user(self, **kwargs):
|
|
return await self.auth_database.delete_user(**kwargs)
|
|
|
|
async def login_user(self, **kwargs):
|
|
return await self.auth_database.login_user(**kwargs)
|
|
|
|
async def get_user_authority(self, **kwargs):
|
|
return await self.auth_database.get_user_authority(**kwargs)
|
|
|
|
##individual_database 操作
|
|
async def add_worker_individual(self, **kwargs):
|
|
return await self.individual_database.add_worker_individual(**kwargs)
|
|
|
|
async def get_worker_individual(self, agent_id: str):
|
|
return await self.individual_database.get_worker_individual(agent_id)
|
|
|
|
async def get_worker_individual_list(self, owner_id: str):
|
|
return await self.individual_database.get_worker_individual_list(owner_id)
|
|
|
|
async def update_worker_individual(self, agent_id: str, **kwargs):
|
|
return await self.individual_database.update_worker_individual(agent_id, **kwargs)
|
|
|
|
async def delete_worker_individual(self, agent_id: str):
|
|
return await self.individual_database.delete_worker_individual(agent_id) |