1.建模 model里面写你需要的字段:
import { Schema } from 'mongoose';
import { mongoClient } from '../core/mongo/mongodb';
const myBed = new Schema(
{
name: { type: String, required: true }
age: { type: String }, //规则组
children: { type: Array, default: [], ref: 'class' },
status: { type: Boolean, default: true } //状态
},
{
timestamps: {
createdAt: 'createdAt', //创建时间
updatedAt: 'updatedAt' //修改时间
},
},
);
export default mongoClient.model('name', myBed);
2.路由 router 里面写路由,路径
import * as Router from 'koa-router';
import cascade from '../controller/mybed';
import passAuth from '../middleware/passAuth';
import validate from '../middleware/validate';
import { createRule, listRule,updateRule } from '../validate/mybed';
const router = new Router({
prefix: '/api/mybed',
});
router.get('/list', passAuth(),validate(listRule), mybed.list);
router.post('/create', passAuth(),validate(createRule), mybed.create);
router.put('/:id', passAuth(),validate(updateRule), mybed.update);
export default router;
3.controller里面写逻辑
import { Context } from 'koa';
import MyService from '../service/mybed';
import res from '../extend/context';
class MyBed {
// 列表
async list(ctx: Context) {
const { page = 1, pageSize = 10, ...content } = ctx.request.query;
const filterQuery = {
...content
};
const data = await MyService.list(Number(page), Number(pageSize), filterQuery);
return res.success(ctx, data);
}
// 创建
async create(ctx: Context) {
const { body: { name, age } } = ctx.request;
const isExist = await MyService.exist({ name, age });
if (isExist) {
return res.failure(ctx, { msg: "名字已存在" })
}
const data = await MyService.create({ name});
return res.success(ctx, data);
}
// 修改
async update(ctx: Context) {
const { id} = ctx.params;
const { status } = ctx.request.body;
const data = await MyService.update(_id, status);
return res.success(ctx, data);
}
}
export default new MyBed();
4.service里面写数据库里的请求逻辑
import mybed from '../model/mybed';
export default class MyService {
/**
* @description: 分页查询
* @param {number} page 页数
* @param {number} pageSize 一页的数量
* @param {Object} query 查询条件
* @return {list, total} 列表、总数
*/
public static async list(page: number, pageSize: number, query: Object) {
const list = await mybed
.find(query)
.skip((page - 1) * pageSize)
.limit(pageSize)
.sort({ createdAt: -1 })
const total = await mybed.count(query);
return { list, total };
}
/**
* @description: 创建下发规则
* @param {Object} content
*/
public static async create(content) {
const data = await mybed.create(content);
return data;
}
/**
* @description: 判断探针名称和规则组是否存在
* @param {Object} query 查询条件
*/
public static async exist(query: Object) {
const data = await mybed.exists(query);
return data;
}
/**
* @description: 更新
* @param {string} id
* @param {Boolean} status 状态
*/
public static async update(id: string, status: Boolean) {
const data = await mybed.findByIdAndUpdate(id, { status }, { new: true, select: '_id, status' });
return data;
}
5.validate里面写请求的验证字段的方式
import { Validate } from 'interface/validate';
import * as Joi from 'joi';
export const listRule = {
query: {
name: Joi.string().trim().allow(''),
page: Joi.number(),
pageSize: Joi.number(),
},
} as Validate;
export const createRule = {
body: {
name: Joi.string().trim().required(),
age: Joi.string().trim().required()
},
} as Validate;
export const updateRule = {
params: {
id: Joi.string().trim().required(),
},
body: {
status: Joi.boolean().default(true)
},
};