25 lines
796 B
JavaScript
25 lines
796 B
JavaScript
import { Schema, model } from 'mongoose'
|
|
|
|
// 声明一个数据集 对象
|
|
const userSchema = new Schema({
|
|
username: { type: String, unique: true, required: true },
|
|
password: { type: String, required: true },
|
|
truename: { type: String, required: true }, // 真实名字
|
|
role: { type: Schema.Types.ObjectId, ref: 'Role' },
|
|
lastLogin: { type: Date },
|
|
status: { type: Schema.Types.ObjectId, ref: 'Status' }
|
|
})
|
|
|
|
// TODO: IT CAN'T WORK ...
|
|
userSchema.pre(['deleteOne', 'findByIdAndDelete'], { document: true }, () => {
|
|
this.status.deleted = true
|
|
this.status.deleteAt = Date.now()
|
|
})
|
|
|
|
userSchema.pre(['findByIdAndUpdate', 'findOneAndUpdate'], (next) => {
|
|
this.status.updateAt = Date.now()
|
|
console.log("gdfg")
|
|
next()
|
|
})
|
|
|
|
export default model('User', userSchema) |