mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 03:07:54 +00:00
48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
import { Exclude } from 'class-transformer';
|
|
import {
|
|
Column,
|
|
CreateDateColumn,
|
|
Generated,
|
|
PrimaryGeneratedColumn,
|
|
UpdateDateColumn,
|
|
} from 'typeorm';
|
|
|
|
import { AbstractDto } from '../dtos';
|
|
import { Constructor } from '../../../../../common/src/util/types';
|
|
|
|
export abstract class AbstractEntity<
|
|
T extends AbstractDto = AbstractDto,
|
|
O = never,
|
|
> {
|
|
@PrimaryGeneratedColumn('increment')
|
|
@Exclude()
|
|
public id: number;
|
|
|
|
@Column()
|
|
@Generated('uuid')
|
|
public uuid: string;
|
|
|
|
@CreateDateColumn({ type: 'timestamp' })
|
|
@Exclude()
|
|
public createdAt: Date;
|
|
|
|
@UpdateDateColumn({ type: 'timestamp' })
|
|
@Exclude()
|
|
public updatedAt: Date;
|
|
|
|
private dtoClass: Constructor<T, [AbstractEntity, O?]>;
|
|
|
|
toDto(options?: O): T {
|
|
const dtoClass = this.dtoClass;
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
if (!dtoClass) {
|
|
throw new Error(
|
|
`You need to use @UseDto on class (${this.constructor.name}) be able to call toDto function`,
|
|
);
|
|
}
|
|
|
|
return new this.dtoClass(this, options);
|
|
}
|
|
}
|