后台
项目
egg+mysql(sequelize)+vue实现curd
项目结构
mysql建表(collect)
CREATE TABLE `collect` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '收藏id',
`author` varchar(255) DEFAULT NULL COMMENT '作者',
`date` varchar(255) DEFAULT NULL COMMENT '日期',
`link` varchar(255) DEFAULT NULL COMMENT '链接',
`title` varchar(255) DEFAULT NULL COMMENT '标题',
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
`updated_at` datetime DEFAULT NULL COMMENT '更改时间',
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='收藏表';
配置
egg安装模块
cnpm install egg-sequelize egg-cors --save
config/pulgin.js
exports.sequelize = {
enable: true,
package: 'egg-sequelize',
};
exports.cors = {
enable: true,
package: 'egg-cors',
};
config/config.default.js
//mysql配置开始
config.sequelize = {
dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
dialectOptions: {
charset: 'utf8mb4',
},
database: 'egg04',
host: 'localhost',
port: '3306',
username: 'root',
password: '123456',
timezone: '+08:00',
};
//mysql配置结束
//cors配置开始
config.security = {
csrf: {
enable: false,
},
domainWhiteList: [ 'http://localhost:8080' ],
};
config.cors = {
credentials: true,
};
//cors配置结束
数据建模
model/collect.js
'use strict';
module.exports = app => {
const {
INTEGER,
STRING,
DATE
} = app.Sequelize;
const Collect = app.model.define('collect',{
id:{
type:INTEGER,
primaryKey:true,
autoIncrement:true
},
author:STRING,
date:STRING,
link:STRING,
title:STRING,
created_at:DATE,
updated_at:DATE
},{
freezeTableName: true, //使用默认表名,不会变以collects
})
return Collect;
}
controller
controller/collect.js
'use strict';
const Controller = require('egg').Controller;
class CollectController extends Controller {
async create(){
const {ctx} = this;
const body = ctx.request.body;
ctx.body = await ctx.service.collect.create(body);
}
async destroy(){
const {ctx} = this;
const id = +ctx.params.id;
ctx.body = await ctx.service.collect.destroy(id)
}
async update(){
const {ctx} = this;
const id = +ctx.params.id;
const body = ctx.request.body;
ctx.body = await ctx.service.collect.update({
id,
body
})
}
async find() {
const {ctx} = this;
const id = +ctx.params.id;
ctx.body = await ctx.service.collect.find(id)
}
async list(){
const {ctx} = this;
const query = ctx.query;
ctx.body = await ctx.service.collect.list(query)
}
}
module.exports = CollectController;
service
service/collect.js
'use strict';
const Service = require('egg').Service;
const{ERROR,SUCCESS} = require('../util/util')
class CollectService extends Service {
async create(collect){
const {ctx} = this;
try{
collect = await ctx.model.Collect.create(collect);
if(!collect){
ctx.status = 400;
return Object.assign(ERROR,{
msg:`expectd collect,but got ${JSON.stringify(collect)}`
})
}
ctx.status = 200;
return Object.assign(SUCCESS,{
data:collect
})
}catch(error){
ctx.status = 500;
throw(error)
}
}
async destroy(id){
const {ctx} = this;
try{
const collect = await ctx.model.Collect.findById(id);
if(!collect){
ctx.status = 400;
return Object.assign(ERROR,{
msg:'not found collect'
})
}
const res = await collect.destroy();
ctx.status = 200;
return Object.assign(SUCCESS,{
data:res
})
}catch(error){
ctx.status = 500;
throw(error)
}
}
async update({
id,
body
}){
const {ctx} = this;
try{
const collect = await ctx.model.Collect.findById(id);
if(!collect){
ctx.status = 400;
return Object.assign(ERROR,{
msg:'not fount collect'
})
}
const res = await collect.update(body);
ctx.status = 200;
return Object.assign(SUCCESS,{
data:res
})
}catch(error){
ctx.status = 500;
throw(error)
}
}
async find(id) {
const {ctx} = this;
try{
const collect = await ctx.model.Collect.findById(id);
if(!collect){
ctx.status = 400;
return Object.assign(ERROR,{
msg:'not found collection'
})
}
ctx.status = 200;
return Object.assign(SUCCESS,{
data:collect
})
}catch(error){
ctx.status = 500;
throw(error)
}
}
async list({
offset = 0,
limit = 10,
order_by ='created_at',
order = 'DESC'
}){
const {ctx} = this;
const options = {
offset:parseInt(offset),
limit:parseInt(limit),
order:[
[order_by,order.toUpperCase()]
]
}
try{
const res = await ctx.model.Collect.findAndCountAll(options);
if(!res){
ctx.status = 400;
return Object.assign(ERROR,{
msg:'not fount collect'
})
}
ctx.status = 200;
return Object.assign(SUCCESS,{
data:res
})
}catch(error){
ctx.status = 500;
throw(error)
}
}
}
module.exports = CollectService;
router.js
router.post('/api/collect',controller.collect.create)
router.delete('/api/collect/:id',controller.collect.destroy)
router.put('/api/collect/:id',controller.collect.update)
router.get('/api/collect',controller.collect.list)
router.get('/api/collect/:id',controller.collect.find)
前端
查找,删除
新建收藏
增加
修改