feat: add loadMavenDepend & optimize database
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
		@@ -1,9 +1,6 @@
 | 
			
		||||
import { JSClass } from '@ccms/container'
 | 
			
		||||
import { Model } from './model'
 | 
			
		||||
 | 
			
		||||
const HikariDataSource = Java.type('com.zaxxer.hikari.HikariDataSource')
 | 
			
		||||
const HikariConfig = Java.type('com.zaxxer.hikari.HikariConfig')
 | 
			
		||||
const JdbcTemplate = Java.type('org.springframework.jdbc.core.JdbcTemplate')
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 数据库配置
 | 
			
		||||
 */
 | 
			
		||||
@@ -33,6 +30,13 @@ export class DataBase {
 | 
			
		||||
    private dataSource: javax.sql.DataSource
 | 
			
		||||
    private jdbcTemplate: org.springframework.jdbc.core.JdbcTemplate
 | 
			
		||||
 | 
			
		||||
    @JSClass('com.zaxxer.hikari.HikariDataSource')
 | 
			
		||||
    private HikariDataSource: any
 | 
			
		||||
    @JSClass('com.zaxxer.hikari.HikariConfig')
 | 
			
		||||
    private HikariConfig: any
 | 
			
		||||
    @JSClass('org.springframework.jdbc.core.JdbcTemplate')
 | 
			
		||||
    private JdbcTemplate: typeof org.springframework.jdbc.core.JdbcTemplate
 | 
			
		||||
 | 
			
		||||
    constructor(dbConfig: DataBaseConfig) {
 | 
			
		||||
        if (!dbConfig.url) { throw new Error('DataBase url can\'t be null!') }
 | 
			
		||||
        this.createDataSource(dbConfig)
 | 
			
		||||
@@ -41,24 +45,25 @@ export class DataBase {
 | 
			
		||||
 | 
			
		||||
    private createDataSource(dbConfig: DataBaseConfig) {
 | 
			
		||||
        if (typeof dbConfig.url === "string") {
 | 
			
		||||
            if (!dbConfig.username || !dbConfig.password) {
 | 
			
		||||
                throw new Error('DataBase username or password can\'t be null!')
 | 
			
		||||
            }
 | 
			
		||||
            let config = new HikariConfig()
 | 
			
		||||
            let config = new this.HikariConfig()
 | 
			
		||||
            if (dbConfig.driverClassName) {
 | 
			
		||||
                config.setDriverClassName(dbConfig.driverClassName)
 | 
			
		||||
            }
 | 
			
		||||
            config.setUsername(dbConfig.username)
 | 
			
		||||
            config.setPassword(dbConfig.password)
 | 
			
		||||
            if (dbConfig.username) {
 | 
			
		||||
                config.setUsername(dbConfig.username)
 | 
			
		||||
            }
 | 
			
		||||
            if (dbConfig.password) {
 | 
			
		||||
                config.setPassword(dbConfig.password)
 | 
			
		||||
            }
 | 
			
		||||
            config.setJdbcUrl(dbConfig.url)
 | 
			
		||||
            this.dataSource = new HikariDataSource(config)
 | 
			
		||||
            this.dataSource = new this.HikariDataSource(config)
 | 
			
		||||
        } else {
 | 
			
		||||
            this.dataSource = dbConfig.url
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private initialize() {
 | 
			
		||||
        this.jdbcTemplate = new JdbcTemplate(this.dataSource)
 | 
			
		||||
        this.jdbcTemplate = new this.JdbcTemplate(this.dataSource)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -2,5 +2,11 @@
 | 
			
		||||
/// <reference types="@javatypes/jdk" />
 | 
			
		||||
/// <reference types="@javatypes/spring-jdbc" />
 | 
			
		||||
 | 
			
		||||
import { loadMavenDepend } from '@ccms/container'
 | 
			
		||||
 | 
			
		||||
loadMavenDepend('com.h2database', 'h2', '2.1.212')
 | 
			
		||||
loadMavenDepend("com.zaxxer", "HikariCP", "4.0.3")
 | 
			
		||||
loadMavenDepend("org.springframework", "spring-jdbc", "5.3.19", true)
 | 
			
		||||
 | 
			
		||||
export * from './database'
 | 
			
		||||
export * from './manager'
 | 
			
		||||
export * from './manager'
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +1,18 @@
 | 
			
		||||
import { plugin, database } from '@ccms/api'
 | 
			
		||||
import { provideSingleton, inject, postConstruct } from '@ccms/container'
 | 
			
		||||
import { database } from '@ccms/api'
 | 
			
		||||
import { provideSingleton } from '@ccms/container'
 | 
			
		||||
import { DataBase, DataBaseConfig } from './database'
 | 
			
		||||
 | 
			
		||||
@provideSingleton(database.DataBaseManager)
 | 
			
		||||
export class DataBaseManager {
 | 
			
		||||
    @inject(plugin.PluginInstance)
 | 
			
		||||
    private instance: any
 | 
			
		||||
 | 
			
		||||
    private beanFactory: any
 | 
			
		||||
    private mainDatabase: DataBase
 | 
			
		||||
    private databases: { [key: string]: DataBase } = {}
 | 
			
		||||
    private databases = new Map<string, DataBase>()
 | 
			
		||||
 | 
			
		||||
    @postConstruct()
 | 
			
		||||
    initialize() {
 | 
			
		||||
        try {
 | 
			
		||||
            this.beanFactory = this.instance.getAutowireCapableBeanFactory()
 | 
			
		||||
            let mainDatasource = this.beanFactory.getBean(Packages.javax.sql.DataSource.class)
 | 
			
		||||
            this.mainDatabase = new DataBase({ url: mainDatasource })
 | 
			
		||||
        } catch (error: any) {
 | 
			
		||||
            console.ex(error)
 | 
			
		||||
        }
 | 
			
		||||
    /**
 | 
			
		||||
     * 设置主数据库
 | 
			
		||||
     * @param mainDatabase 主数据库
 | 
			
		||||
     */
 | 
			
		||||
    setMainDatabase(mainDatabase: DataBase) {
 | 
			
		||||
        this.mainDatabase = mainDatabase
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -38,16 +31,19 @@ export class DataBaseManager {
 | 
			
		||||
     */
 | 
			
		||||
    createDatabase(name: string, config: DataBaseConfig) {
 | 
			
		||||
        Java.synchronized(() => {
 | 
			
		||||
            if (this.databases[name]) return this.databases[name]
 | 
			
		||||
            return this.databases[name] = new DataBase(config)
 | 
			
		||||
            if (!this.databases.has(name)) {
 | 
			
		||||
                this.databases.set(name, new DataBase(config))
 | 
			
		||||
            }
 | 
			
		||||
            return this.databases.get(name)
 | 
			
		||||
        }, this.databases)()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getDatabase(name: string) {
 | 
			
		||||
        return this.databases[name]
 | 
			
		||||
        return this.databases.get(name)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    disable() {
 | 
			
		||||
        Object.values(this.databases).forEach((ds) => ds?.close())
 | 
			
		||||
        this.databases.forEach((db) => db.close())
 | 
			
		||||
        this.databases.clear()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user