import { Index, PrimaryKey, Property, Filter, OptionalProps } from '@mikro-orm/core';
import { ulid } from 'ulid';

@Filter({ name: 'notDeleted', cond: { deletedAt: null }, default: true })
@Index({ properties: ['deletedAt'] })
@Index({ properties: ['createdAt'] })
export abstract class BaseEntity {
  [OptionalProps]?: 'id' | 'createdAt' | 'updatedAt' | 'deletedAt';

  @PrimaryKey({ type: 'string', columnType: 'char(26)' })
  id: string = ulid();

  @Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
  createdAt: Date = new Date();

  @Property({
    onUpdate: () => new Date(),
    defaultRaw: 'now()',
    columnType: 'timestamptz',
  })
  updatedAt: Date = new Date();

  @Property({ nullable: true })
  deletedAt?: Date;
}
