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 createDatabase(name: string, config: DataBaseConfig): DataBase
abstract removeDatabase(name: string): boolean abstract removeDatabase(name: string): boolean
abstract getDatabase(name: string): DataBase abstract getDatabase(name: string): DataBase
abstract startWebManager(...args: string[])
abstract stopWebManager()
abstract shutdown()
} }
@injectable() @injectable()
export abstract class DataBase { export abstract class DataBase {

View File

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

View File

@ -1,5 +1,5 @@
import { database } from '@ccms/api' import { database } from '@ccms/api'
import { provideSingleton } from '@ccms/container' import { JSClass, provideSingleton } from '@ccms/container'
import { DataBase } from './database' import { DataBase } from './database'
@provideSingleton(database.DataBaseManager) @provideSingleton(database.DataBaseManager)
@ -7,11 +7,32 @@ export class DataBaseManager extends database.DataBaseManager {
private mainDatabase: DataBase private mainDatabase: DataBase
private databases = new Map<string, DataBase>() private databases = new Map<string, DataBase>()
@JSClass('org.h2.tools.Server')
private Server: any
private webManager: any
constructor() { constructor() {
super() 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 * @param mainDatabase