yapi/server/models/project.js

112 lines
2.3 KiB
JavaScript
Raw Normal View History

import yapi from '../yapi.js'
2017-07-06 19:21:54 +08:00
import baseModel from './base.js'
2017-07-06 19:21:54 +08:00
class projectModel extends baseModel{
getName(){
return 'project'
}
2017-07-06 19:21:54 +08:00
getSchema(){
return {
uid: {type: Number, required: true},
name: {type: String, required: true},
2017-07-10 20:02:44 +08:00
basepath: {type: String, required: true, validate: {
validator: (v) => {
return v && v[v.length - 1] === '/'
},
message: 'basepath字符串结尾必须是/'
}},
2017-07-06 19:21:54 +08:00
desc: String,
group_id: {type: Number, required: true},
members: Array,
prd_host: {type: String, required: true},
2017-07-10 20:02:44 +08:00
env: [
{name: String, domain: String}
],
2017-07-06 19:21:54 +08:00
add_time: Number,
up_time: Number
}
}
2017-07-06 19:21:54 +08:00
save(data) {
let m = new this.model(data);
return m.save();
2017-07-06 19:21:54 +08:00
}
get(id){
return this.model.findOne({
_id: id
}).exec()
}
2017-07-10 20:02:44 +08:00
getByDomain(domain){
return this.model.find({
prd_host: domain
}).exec()
}
2017-07-06 19:21:54 +08:00
checkNameRepeat(name){
return this.model.count({
name: name
})
}
checkDomainRepeat(domain, basepath){
return this.model.count({
prd_host: domain,
basepath: basepath
})
2017-07-06 19:21:54 +08:00
}
list (group_id){
return this.model.find({
group_id: group_id
}).exec()
}
2017-07-10 20:02:44 +08:00
countByGroupId(group_id){
return this.model.count({
group_id: group_id
})
}
2017-07-06 19:21:54 +08:00
del(id){
return this.model.deleteOne({
_id: id
})
2017-07-06 19:21:54 +08:00
}
up(id, data){
data.up_time = yapi.commons.time();
2017-07-06 19:21:54 +08:00
return this.model.update({
_id: id,
2017-07-06 20:53:26 +08:00
}, data, { runValidators: true })
}
2017-07-06 19:21:54 +08:00
addMember(id, uid){
return this.model.update({
2017-07-10 11:56:53 +08:00
_id: id
2017-07-06 19:21:54 +08:00
}, {
$push: {members: uid}
})
}
delMember(id, uid){
return this.model.update({
_id: id
}, {
$pull: {members: uid}
})
}
checkMemberRepeat(id, uid){
return this.model.count({
_id: id,
members:[uid]
})
}
}
module.exports = projectModel;