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()
|
2024-12-23 00:24:31 +08:00
|
|
|
|
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
2024-10-31 22:35:05 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|