import {
  BeforeInsert,
  BeforeUpdate,
  Column,
  PrimaryGeneratedColumn,
} from 'typeorm';

export class BaseEntity<T> {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'create_date', type: 'bigint' })
  createDate: number;

  @Column({ name: 'update_date', type: 'bigint' })
  updateDate: number;

  @BeforeInsert()
  beforeInsert() {
    this.createDate = Date.now();
    this.updateDate = Date.now();
  }

  @BeforeUpdate()
  beforeUpdate() {
    this.updateDate = Date.now();
  }

  constructor(partial: Partial<T>) {
    Object.assign(this, partial);
  }
}
