feat: add database web manager

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2022-05-21 11:54:09 +08:00
parent 7fc70a92d5
commit 880065495e
3 changed files with 33 additions and 8 deletions

View File

@ -35,6 +35,9 @@ export namespace database {
abstract createDatabase(name: string, config: DataBaseConfig): DataBase
abstract removeDatabase(name: string): boolean
abstract getDatabase(name: string): DataBase
abstract startWebManager(...args: string[])
abstract stopWebManager()
abstract shutdown()
}
@injectable()
export abstract class DataBase {

View File

@ -1,6 +1,7 @@
import { database } from '@ccms/api'
import { JSClass, postConstruct } from '@ccms/container'
const JavaString = Java.type('java.lang.String')
const Properties = Java.type('java.util.Properties')
/**
@ -47,11 +48,11 @@ export class DataBase extends database.DataBase {
} else {
this.dataSource = dbConfig.url
}
this.jdbcTemplate = new this.JdbcTemplate(this.dataSource)
}
@postConstruct()
private initialize() {
this.jdbcTemplate = new this.JdbcTemplate(this.dataSource)
getDataSource() {
return this.dataSource
}
/**
@ -62,7 +63,7 @@ export class DataBase extends database.DataBase {
query<T>(sql: string, ...args: any[]): Array<T> {
let startTime = Date.now()
let result = Java.from<any>(this.jdbcTemplate.queryForList(sql, args))
console.debug(java.lang.String.format(`\n[DB] query \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
console.debug(JavaString.format(`\n[DB] query \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
return result
}
@ -74,7 +75,7 @@ export class DataBase extends database.DataBase {
update(sql: string, ...args: any[]): number {
let startTime = Date.now()
let result = this.jdbcTemplate.update(sql, args)
console.debug(java.lang.String.format(`\n[DB] update \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
console.debug(JavaString.format(`\n[DB] update \nSQL : ${sql.replace(/\?/ig, '%s')} \nCOST : ${Date.now() - startTime}ms`, args))
return result
}
@ -85,7 +86,7 @@ export class DataBase extends database.DataBase {
execute(sql: string): void {
let startTime = Date.now()
this.jdbcTemplate.execute(sql)
console.debug(java.lang.String.format(`\n[DB] execute \nSQL : sql} \nCOST : ${Date.now() - startTime}ms`))
console.debug(`\n[DB] execute \nSQL : ${sql} \nCOST : ${Date.now() - startTime}ms`)
}
close() {

View File

@ -1,5 +1,5 @@
import { database } from '@ccms/api'
import { provideSingleton } from '@ccms/container'
import { JSClass, provideSingleton } from '@ccms/container'
import { DataBase } from './database'
@provideSingleton(database.DataBaseManager)
@ -7,11 +7,32 @@ export class DataBaseManager extends database.DataBaseManager {
private mainDatabase: DataBase
private databases = new Map<string, DataBase>()
@JSClass('org.h2.tools.Server')
private Server: any
private webManager: any
constructor() {
super()
process.on('exit', () => this.disable())
process.on('exit', () => this.shutdown())
}
startWebManager(...args: string[]) {
this.webManager = this.Server.createWebServer(args)
this.webManager.start()
}
stopWebManager() {
if (this.webManager) {
this.webManager.stop()
this.webManager.shutdown()
}
}
shutdown() {
this.stopWebManager()
this.disable()
}
/**
*
* @param mainDatabase