33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
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: {
|
||
|
|
locked: { type: Boolean, default: false },
|
||
|
|
lockAt: { type: Date },
|
||
|
|
lockBy: { type: Schema.Types.ObjectId, ref: 'User' },
|
||
|
|
deleted: { type: Boolean, default: false },
|
||
|
|
createAt: { type: Date, default: Date.now() },
|
||
|
|
deleteAt: { type: Date },
|
||
|
|
updateAt: { type: Date, default: Date.now() }
|
||
|
|
}
|
||
|
|
})
|
||
|
|
|
||
|
|
// TODO: IT CAN'T WORK ...
|
||
|
|
userSchema.pre('deleteOne', { 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)
|