import { DataSource } from 'typeorm'; import { ModelConfigProvider } from '@/common/config'; export class BaseModel { dataSource: DataSource; protected constructor( private readonly configProvider: ModelConfigProvider, private readonly entities: any[], ) {} protected async init() { const config = typeof this.configProvider === 'function' ? await this.configProvider() : this.configProvider; this.dataSource = new DataSource({ ...config, // Overwrite unsafe fields synchronize: false, dropSchema: false, // Override with entities defined in each module entities: this.entities, }); await this.dataSource.initialize(); } async synchronize(dropBeforeSync: boolean) { await this.dataSource.synchronize(dropBeforeSync); } async close() { await this.dataSource.destroy(); } entityManager() { return this.dataSource.manager; } }