import { prop, pre, modelOptions, plugin } from '@typegoose/typegoose';
import { TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';
const mongooseLeanVirtuals = require('mongoose-lean-virtuals');
const mongooseLeanGetters = require('mongoose-lean-getters');

/**
  * BaseModel
*/
@pre<BaseModel>('save', function(next) {
  if (!this.createdAt || this.isNew) {
    this.createdAt = this.updatedAt = new Date();
  } else {
    this.updatedAt = new Date();
  }
  next();
})


@modelOptions({schemaOptions: {id: false}})
@plugin(mongooseLeanVirtuals)
@plugin(mongooseLeanGetters)
export default class BaseModel extends TimeStamps {

  _id?: string

  @prop()
  createdAt: Date

  @prop()
  updatedAt: Date

  @prop()
  createdBy: number;             // 后台创建人

  @prop()
  updatedBy: number;             // 最近更新人
}
