import { Column, DeepPartial, EntityTarget, PrimaryColumn, ObjectLiteral, FindOptionsWhere } from "typeorm"; import { v4 } from "uuid"; import AppDataSource from "../database/dataSources/appDataSource"; export default class AppBaseEntity { constructor() { this.Id = v4(); this.WhenCreated = new Date(); this.WhenUpdated = new Date(); } @PrimaryColumn() Id: string; @Column() WhenCreated: Date; @Column() WhenUpdated: Date; public async Save(target: EntityTarget, entity: DeepPartial): Promise { this.WhenUpdated = new Date(); const repository = AppDataSource.getRepository(target); await repository.save(entity); } public static async Remove(target: EntityTarget, entity: T): Promise { const repository = AppDataSource.getRepository(target); await repository.remove(entity); } public static async FetchAll(target: EntityTarget, relations?: string[]): Promise { const repository = AppDataSource.getRepository(target); const all = await repository.find({ relations: relations || [] }); return all; } public static async FetchOneById(target: EntityTarget, id: string, relations?: string[]): Promise { const repository = AppDataSource.getRepository(target); const single = await repository.findOne({ where: ({ Id: id } as FindOptionsWhere), relations: relations || {} }); return single; } public static async Any(target: EntityTarget): Promise { const repository = AppDataSource.getRepository(target); const any = await repository.find(); return any.length > 0; } }