mirror of
https://github.com/certd/certd.git
synced 2026-04-23 03:27:25 +08:00
chore: 1
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export interface SqlAdapter {
|
||||
date(columnName: string): string;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { SqliteAdapter } from './sqlite.js';
|
||||
import { PostgresqlAdapter } from './postgresql.js';
|
||||
import { Config, Init, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { SqlAdapter } from './d.js';
|
||||
|
||||
@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();
|
||||
} else {
|
||||
throw new Error(`dbType ${this.dbType} not support`);
|
||||
}
|
||||
}
|
||||
|
||||
isSqlite() {
|
||||
return this.dbType === 'better-sqlite3';
|
||||
}
|
||||
isPostgresql() {
|
||||
return this.dbType === 'postgres';
|
||||
}
|
||||
|
||||
date(columnName: string) {
|
||||
return this.adapter.date(columnName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { SqlAdapter } from './d.js';
|
||||
|
||||
export class PostgresqlAdapter implements SqlAdapter {
|
||||
date(columnName: string) {
|
||||
return `to_char(${columnName}, 'YYYY-MM-DD')`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { SqlAdapter } from './d.js';
|
||||
|
||||
export class SqliteAdapter implements SqlAdapter {
|
||||
date(columnName: string) {
|
||||
return `date(${columnName}, 'localtime')`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user