import type { EntityTarget , Connection } from 'typeorm' ; import { createConnection } from 'typeorm' ; import Logger from '@/core/Logger' ; import connectConfig from '@/databaseConfig' ; import type { App } from '@/core/App' ; export default class DataBase { private app : App ; private connection ? : Connection ; constructor ( app ) { this . app = app ; } /**
     * 获取数据库链接
     */ connect = async ( ) => { const logger = Logger . getLogger ( 'database' ) ; try { logger . info ( '连接数据库...' ) ; this . app . browserManager . broadcast ( 'initDatabase' , 'loading' ) ; this . connection = await createConnection ( connectConfig ) ; logger . info ( '连接成功!' ) ; this . app . browserManager . broadcast ( 'initDatabase' , 'success' ) ; await this . connection . synchronize ( ) ; } catch ( err ) { logger . error ( '数据库初始化失败,错误信息:' ) ; logger . error ( err ) ; this . app . browserManager . broadcast ( 'initDatabase' , 'failed' ) ; } } ; getRepository < T > ( entity ) { return this . connection ?. getRepository ( entity ) ; } }