Merge branch 'main' into develop
This commit is contained in:
commit
efd213d085
7 changed files with 939 additions and 1236 deletions
|
@ -1,10 +1,10 @@
|
|||
import { Column, DeepPartial, EntityTarget, getConnection, PrimaryColumn } from "typeorm";
|
||||
import { Column, DeepPartial, EntityTarget, getConnection, PrimaryColumn, ObjectLiteral, FindOptionsWhere } from "typeorm";
|
||||
import { v4 } from "uuid";
|
||||
|
||||
export default class BaseEntity {
|
||||
constructor() {
|
||||
this.Id = v4();
|
||||
|
||||
|
||||
this.WhenCreated = new Date();
|
||||
this.WhenUpdated = new Date();
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ export default class BaseEntity {
|
|||
@Column()
|
||||
WhenUpdated: Date;
|
||||
|
||||
public async Save<T>(target: EntityTarget<T>, entity: DeepPartial<T>): Promise<void> {
|
||||
public async Save<T extends BaseEntity>(target: EntityTarget<T>, entity: DeepPartial<T>): Promise<void> {
|
||||
this.WhenUpdated = new Date();
|
||||
|
||||
const connection = getConnection();
|
||||
|
@ -28,7 +28,7 @@ export default class BaseEntity {
|
|||
await repository.save(entity);
|
||||
}
|
||||
|
||||
public static async Remove<T>(target: EntityTarget<T>, entity: T): Promise<void> {
|
||||
public static async Remove<T extends BaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> {
|
||||
const connection = getConnection();
|
||||
|
||||
const repository = connection.getRepository<T>(target);
|
||||
|
@ -36,7 +36,7 @@ export default class BaseEntity {
|
|||
await repository.remove(entity);
|
||||
}
|
||||
|
||||
public static async FetchAll<T>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
|
||||
public static async FetchAll<T extends BaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
|
||||
const connection = getConnection();
|
||||
|
||||
const repository = connection.getRepository<T>(target);
|
||||
|
@ -46,17 +46,17 @@ export default class BaseEntity {
|
|||
return all;
|
||||
}
|
||||
|
||||
public static async FetchOneById<T>(target: EntityTarget<T>, id: string, relations?: string[]): Promise<T | undefined> {
|
||||
public static async FetchOneById<T extends BaseEntity>(target: EntityTarget<T>, id: string, relations?: string[]): Promise<T | null> {
|
||||
const connection = getConnection();
|
||||
|
||||
const repository = connection.getRepository<T>(target);
|
||||
|
||||
const single = await repository.findOne(id, { relations: relations || [] });
|
||||
const single = await repository.findOne({ where: ({ Id: id } as FindOptionsWhere<T>), relations: relations || {} });
|
||||
|
||||
return single;
|
||||
}
|
||||
|
||||
public static async Any<T>(target: EntityTarget<T>): Promise<boolean> {
|
||||
public static async Any<T extends ObjectLiteral>(target: EntityTarget<T>): Promise<boolean> {
|
||||
const connection = getConnection();
|
||||
|
||||
const repository = connection.getRepository<T>(target);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue