Files
certd/packages/ui/certd-server/src/modules/db/index.ts
T

41 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-10-31 22:35:05 +08:00
import { SqliteAdapter } from './sqlite.js';
import { PostgresqlAdapter } from './postgresql.js';
import { Config, Init, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { SqlAdapter } from './d.js';
2024-12-09 17:47:01 +08:00
import { MysqlAdapter } from './mysql.js';
2024-10-31 22:35:05 +08:00
@Provide()
@Scope(ScopeEnum.Singleton)
export class DbAdapter implements SqlAdapter {
adapter: SqlAdapter;
@Config('typeorm.dataSource.default.type')
dbType: string;
@Init()
async init() {
if (this.isSqlite()) {
this.adapter = new SqliteAdapter();
} else if (this.isPostgresql()) {
this.adapter = new PostgresqlAdapter();
2024-12-09 17:47:01 +08:00
} else if (this.isMysql()) {
this.adapter = new MysqlAdapter();
2024-10-31 22:35:05 +08:00
} else {
2024-12-09 17:47:01 +08:00
throw new Error(`dbType ${this.dbType} not support 请实现Adapter`);
2024-10-31 22:35:05 +08:00
}
}
isSqlite() {
return this.dbType === 'better-sqlite3';
}
isPostgresql() {
return this.dbType === 'postgres';
}
2024-12-09 17:47:01 +08:00
isMysql() {
return this.dbType === 'mysql' || this.dbType === 'mariadb';
}
2024-10-31 22:35:05 +08:00
date(columnName: string) {
return this.adapter.date(columnName);
}
}