vylbot-app/src/contracts/BaseEntity.ts
Vylpes e6c845e3b2
Some checks failed
continuous-integration/drone/push Build is failing
Switch to TypeORM's DataSource API (#299)
- Switch to TypeORM's DataSource API, rather than using the now deprecated ormconfig.json
- This will fix stage deployment not knowing how to deploy the database migrations

#297

> **NOTE:** This change requires the deployment scripts to be updated, please update them on the server before merging

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/vylbot-app/pulls/299
2023-05-26 17:59:22 +01:00

59 lines
No EOL
1.8 KiB
TypeScript

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