yapi/server/models/user.js

71 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-07-05 17:59:53 +08:00
import yapi from '../yapi.js'
2017-07-06 18:25:53 +08:00
import mongoose from 'mongoose'
import baseModel from './base.js'
2017-07-05 17:59:53 +08:00
2017-07-06 18:25:53 +08:00
class userModel extends baseModel{
getName(){
return 'user'
}
2017-07-05 17:59:53 +08:00
2017-07-06 18:25:53 +08:00
getSchema(){
return{
2017-07-10 11:11:35 +08:00
username: {
type: String,
required: true
},
password:{
type:String,
required: true
},
email: {
type: String,
required: true
},
2017-07-10 20:51:04 +08:00
token: String,
2017-07-06 18:25:53 +08:00
passsalt: String,
role: String,
add_time: Number,
up_time: Number
}
}
save(data){
let user = new this.model(data);
2017-07-05 17:59:53 +08:00
return user.save();
2017-07-06 18:25:53 +08:00
}
checkRepeat(name){
return this.model.count({
username: name
})
}
list(){
return this.model.find().select("username_id username email role add_time up_time").exec() //显示id name email role
}
2017-07-10 11:11:35 +08:00
findByName(username){
return this.model.findOne({username})
2017-07-07 12:04:14 +08:00
}
findById(id){
2017-07-06 18:25:53 +08:00
return this.model.findById({
_id: id
2017-07-05 17:59:53 +08:00
})
2017-07-06 18:25:53 +08:00
}
del (id) {
return this.model.deleteOne({
_id: id
})
}
update(id,data){
return this.model.update({
2017-07-05 17:59:53 +08:00
_id: id,
},{
2017-07-06 18:25:53 +08:00
username: data.username,
password: data.password,
email: data.email,
role: data.role,
2017-07-05 17:59:53 +08:00
up_time: yapi.commons.time()
})
}
}
2017-07-06 18:25:53 +08:00
module.exports = userModel