feat: add @ccms/database
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
843b65f13b
commit
2582d51a98
@ -21,6 +21,7 @@
|
||||
"dependencies": {
|
||||
"@ccms/container": "^0.7.0",
|
||||
"@ccms/ployfill": "^0.7.0",
|
||||
"@ccms/common": "^0.7.0",
|
||||
"base64-js": "^1.3.1",
|
||||
"source-map-builder": "^0.0.7"
|
||||
},
|
||||
|
5
packages/api/src/interfaces/database.ts
Normal file
5
packages/api/src/interfaces/database.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export namespace database {
|
||||
export const DataBaseManager = Symbol("DataBaseManager");
|
||||
export const DataSource = Symbol("DataSource");
|
||||
export const DataBase = Symbol("DataBase");
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
export * from './plugin'
|
||||
export * from './server'
|
||||
export * from './database'
|
1
packages/database/.npmignore
Symbolic link
1
packages/database/.npmignore
Symbolic link
@ -0,0 +1 @@
|
||||
../../.npmignore
|
30
packages/database/package.json
Normal file
30
packages/database/package.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "@ccms/database",
|
||||
"version": "0.6.7",
|
||||
"description": "MiaoScript database package",
|
||||
"keywords": [
|
||||
"miaoscript",
|
||||
"minecraft",
|
||||
"bukkit",
|
||||
"sponge"
|
||||
],
|
||||
"author": "MiaoWoo <admin@yumc.pw>",
|
||||
"homepage": "https://github.com/circlecloud/ms.git",
|
||||
"license": "ISC",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"clean": "rimraf dist",
|
||||
"watch": "tsc --watch",
|
||||
"build": "yarn clean && tsc",
|
||||
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rimraf": "^3.0.2",
|
||||
"typescript": "^3.9.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ccms/api": "^0.7.0",
|
||||
"@ccms/container": "^0.7.0"
|
||||
}
|
||||
}
|
51
packages/database/src/database.ts
Normal file
51
packages/database/src/database.ts
Normal file
@ -0,0 +1,51 @@
|
||||
/// <reference types="@ccms/types/dist/typings/spring" />
|
||||
|
||||
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')
|
||||
|
||||
/**
|
||||
* 数据库封装类
|
||||
*/
|
||||
export class DataBase {
|
||||
private dataSource: javax.sql.DataSource
|
||||
private jdbcTemplate: org.springframework.jdbc.core.JdbcTemplate
|
||||
|
||||
constructor(url: string | javax.sql.DataSource, username?: string, password?: string) {
|
||||
if (!url) { throw new Error('DataBase url can\'t be null!') }
|
||||
this.createDataSource(url, username, password)
|
||||
this.initialize()
|
||||
}
|
||||
|
||||
private createDataSource(url: string | javax.sql.DataSource, username?: string, password?: string) {
|
||||
if (typeof url === "string") {
|
||||
if (!username || !password) {
|
||||
throw new Error('DataBase username or password can\'t be null!')
|
||||
}
|
||||
let config = new HikariConfig()
|
||||
config.setUsername(username)
|
||||
config.setPassword(password)
|
||||
config.setJdbcUrl(url)
|
||||
this.dataSource = new HikariDataSource(config)
|
||||
} else {
|
||||
this.dataSource = url
|
||||
}
|
||||
}
|
||||
|
||||
private initialize() {
|
||||
this.jdbcTemplate = new JdbcTemplate(this.dataSource)
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行SQL查询
|
||||
* @param sql SQL语句
|
||||
*/
|
||||
query<T>(sql: string, ...args: any[]): Array<T> {
|
||||
return this.jdbcTemplate.queryForList(sql, args)
|
||||
}
|
||||
|
||||
close() {
|
||||
//@ts-ignore
|
||||
this.dataSource.close()
|
||||
}
|
||||
}
|
5
packages/database/src/index.ts
Normal file
5
packages/database/src/index.ts
Normal file
@ -0,0 +1,5 @@
|
||||
/// <reference types="@ccms/nashorn" />
|
||||
/// <reference types="@ccms/types/dist/typings/jdk/index" />
|
||||
|
||||
export * from './database'
|
||||
export * from './manager'
|
45
packages/database/src/manager.ts
Normal file
45
packages/database/src/manager.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import { plugin, database } from '@ccms/api'
|
||||
import { provideSingleton, inject, postConstruct, Container, ContainerInstance } from '@ccms/container'
|
||||
import { DataBase } from './database'
|
||||
|
||||
@provideSingleton(database.DataBaseManager)
|
||||
export class DataBaseManager {
|
||||
@inject(ContainerInstance)
|
||||
private container: Container
|
||||
@inject(plugin.PluginInstance)
|
||||
private instance: any
|
||||
|
||||
private beanFactory: any
|
||||
private mainDatabase: DataBase
|
||||
private databases: { [key: string]: DataBase } = {}
|
||||
|
||||
@postConstruct()
|
||||
initialize() {
|
||||
try {
|
||||
this.beanFactory = this.instance.getAutowireCapableBeanFactory()
|
||||
let mainDatasource = this.beanFactory.getBean(Packages.javax.sql.DataSource.class)
|
||||
this.mainDatabase = new DataBase(mainDatasource)
|
||||
} catch (error) {
|
||||
console.ex(error)
|
||||
}
|
||||
}
|
||||
|
||||
getMainDatabase() {
|
||||
return this.mainDatabase
|
||||
}
|
||||
|
||||
createDatabase(name: string, url: string, username: string, password: string) {
|
||||
Java.synchronized(() => {
|
||||
if (this.databases[name]) return this.databases[name]
|
||||
return this.databases[name] = new DataBase(url, username, password)
|
||||
}, this.databases)()
|
||||
}
|
||||
|
||||
getDatabase(name: string) {
|
||||
return this.databases[name]
|
||||
}
|
||||
|
||||
disable() {
|
||||
Object.values(this.databases).forEach((ds) => ds?.close())
|
||||
}
|
||||
}
|
7
packages/database/tsconfig.json
Normal file
7
packages/database/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "src",
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user