import { Model, ModelOptions, QueryContext, snakeCaseMappers } from 'objection';
import { v4 as uuidv4 } from 'uuid';

export class BaseModel extends Model {
  id!: string;
  createdAt!: Date;
  updatedAt!: Date;

  static get columnNameMappers() {
    return snakeCaseMappers();
  }

  static get modelPaths(): string[] {
    return [__dirname];
  }

  async $beforeInsert(queryContext: QueryContext): Promise<void> {
    await super.$beforeInsert(queryContext);
    if (!this.id) {
      this.id = uuidv4();
    }
    this.createdAt = new Date();
    this.updatedAt = new Date();
  }

  async $beforeUpdate(opt: ModelOptions, queryContext: QueryContext): Promise<void> {
    await super.$beforeUpdate(opt, queryContext);
    this.updatedAt = new Date();
  }

  static get useLimitInFirst(): boolean {
    return true;
  }
}
