Ethan Lane
836a1d341f
# Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Added a timer helper to run timers on a cron job - Added a currency timer function to give a user 5 currency every 30 minutes #204 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: #219 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
65 lines
No EOL
2.1 KiB
TypeScript
65 lines
No EOL
2.1 KiB
TypeScript
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<T extends AppBaseEntity>(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 SaveAll<T extends AppBaseEntity>(target: EntityTarget<T>, entities: DeepPartial<T>[]): Promise<void> {
|
|
const repository = AppDataSource.getRepository<T>(target);
|
|
|
|
await repository.save(entities);
|
|
}
|
|
|
|
public static async Remove<T extends AppBaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> {
|
|
const repository = AppDataSource.getRepository<T>(target);
|
|
|
|
await repository.remove(entity);
|
|
}
|
|
|
|
public static async FetchAll<T extends AppBaseEntity>(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 AppBaseEntity>(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;
|
|
}
|
|
} |