yapi/server/models/project.js

133 lines
2.9 KiB
JavaScript
Raw Normal View History

import yapi from '../yapi.js';
import baseModel from './base.js';
class projectModel extends baseModel {
getName() {
return 'project';
2017-07-06 19:21:54 +08:00
}
getSchema() {
2017-07-06 19:21:54 +08:00
return {
2017-08-08 14:45:19 +08:00
uid: {type: Number, required: true},
name: { type: String, required: true },
basepath: {type: String },
2017-07-06 19:21:54 +08:00
desc: String,
group_id: { type: Number, required: true },
2017-08-08 14:45:19 +08:00
members: [
{uid: Number, role: {type: String, enum:['owner', 'dev'], username: String, email: String}}
],
protocol: { type: String, required: true },
prd_host: { type: String, required: true },
2017-07-10 20:02:44 +08:00
env: [
{ name: String, domain: String }
2017-07-10 20:02:44 +08:00
],
2017-07-06 19:21:54 +08:00
add_time: Number,
up_time: Number
};
2017-07-06 19:21:54 +08:00
}
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) {
2017-07-06 19:21:54 +08:00
return this.model.findOne({
_id: id
}).exec();
2017-07-06 19:21:54 +08:00
}
getByDomain(domain) {
2017-07-10 20:02:44 +08:00
return this.model.find({
prd_host: domain
}).exec();
2017-07-10 20:02:44 +08:00
}
checkNameRepeat(name) {
2017-07-06 19:21:54 +08:00
return this.model.count({
name: name
});
2017-07-06 19:21:54 +08:00
}
checkDomainRepeat(domain, basepath) {
2017-07-06 19:21:54 +08:00
return this.model.count({
prd_host: domain,
basepath: basepath
});
2017-07-06 19:21:54 +08:00
}
list(group_id) {
2017-07-06 19:21:54 +08:00
return this.model.find({
group_id: group_id
}).sort({ _id: -1 }).exec();
2017-07-06 19:21:54 +08:00
}
2017-07-18 16:37:28 +08:00
listWithPaging(group_id, page, limit) {
page = parseInt(page);
limit = parseInt(limit);
return this.model.find({
group_id: group_id
}).sort({ _id: -1 }).skip((page - 1) * limit).limit(limit).exec();
2017-07-18 16:37:28 +08:00
}
listCount(group_id) {
return this.model.count({
group_id: group_id
});
2017-07-18 16:37:28 +08:00
}
countByGroupId(group_id) {
2017-07-10 20:02:44 +08:00
return this.model.count({
group_id: group_id
});
2017-07-10 20:02:44 +08:00
}
del(id) {
2017-07-06 19:21:54 +08:00
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({
2017-07-27 19:49:26 +08:00
_id: id
}, data, { runValidators: true });
}
2017-07-06 19:21:54 +08:00
2017-08-08 14:45:19 +08:00
addMember(id, data) {
return this.model.update(
{
_id: id
}, {
2017-08-08 14:45:19 +08:00
$push: { members: data }
}
);
2017-07-06 19:21:54 +08:00
}
delMember(id, uid) {
return this.model.update(
{
_id: id
}, {
$pull: { members: uid }
}
);
2017-07-06 19:21:54 +08:00
}
checkMemberRepeat(id, uid) {
2017-07-06 19:21:54 +08:00
return this.model.count({
_id: id,
2017-08-08 14:45:19 +08:00
"members.uid": uid
});
2017-07-06 19:21:54 +08:00
}
search(keyword) {
return this.model.find({
name: new RegExp(keyword, 'ig')
})
.limit(10);
}
2017-07-06 19:21:54 +08:00
}
module.exports = projectModel;