egg-sequelize --- nodejs

项目
egg + sequelize + mysql2
项目结构

配置
安装模块
npm install --save egg-sequelize npm install --save egg-cors npm install --save mysql2
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: 'nodejs',
host: 'localhost',
port: '3306',
username: 'root',
password: 'root',
timezone: '+08:00',
};
//mysql配置结束
//cors配置开始
config.security = {
csrf: {
enable: false,
},
domainWhiteList: [ 'http://localhost:8080' ],
};
config.cors = {
credentials: true,
};
//cors配置结束
数据建模
mysql 建表
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='收藏表';
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;
}
(选配)启动时创建数据库表
// {work_dir}/app.js
module.exports = app => {
app.beforeStart(async function () {
await app.model.sync({ force: true });
});
};
service
service/collect.js
'use strict';
const Service = require('egg').Service;
class CollectService extends Service {
// 查询所有数据
async findAll() {
let ret = await this.ctx.model.Collect.findAll()
console.log(ret)
return ret
}
// 增加数据
async create(data) {
await this.ctx.model.Collect.create(data)
.then(res => {
console.log('res: '+ JSON.stringify(res))
}).catch(err => {
console.log('err: '+ JSON.stringify(err))
})
// try{
// let a= await this.ctx.model.Collect.create(data)
// console.log(a)
// }catch(err){
// console.log(err)
// }
}
// 修改数据
async update() {
const ret = await this.ctx.model.Collect.update({
author: '新作者'
}, {
where: {
id: 2
}
}).then(ok => {
console.log('ok')
console.log(ok)
}).catch(e => {
console.log('message:' + e)
})
}
// 删除数据
async delete(callback){
await this.ctx.model.Collect.destroy({
where: {
id: 20
}
}).then(res => {
console.log('res:' + JSON.stringify(res))
return callback(JSON.stringify(res))
})
}
// 自定义查询
async query(callback){
let sql = 'SELECT COUNT(1) FROM collect'
let ret = await this.ctx.model.query(sql, {type: 'SELECT'})
return ret
}
}
module.exports = CollectService;
controller
controller/collect.js
'use strict';
const Controller = require('egg').Controller;
class CollectController extends Controller {
async findAll(){
const {ctx} = this;
ctx.body = await ctx.service.collect.findAll()
}
async create(){
const ctx = this.ctx
const body = ctx.request.body
const ret = await ctx.service.collect.create(body)
ctx.body = '<h1>添加数据</h1>'
}
async update() {
await this.ctx.service.collect.update()
this.ctx.body = '<h1>修改数据</h1>'
}
async delete() {
await this.ctx.service.collect.delete(function(res, err) {
console.log('ret: ' + res)
})
console.log('哈哈哈哈')
this.ctx.body = '<h1>删除数据</h1>'
}
}
module.exports = CollectController;
router.js
router.get('/collect',controller.collect.findAll)
router.post('/create', controller.collect.create)
router.get('/update', controller.collect.update)
router.get('/delete', controller.collect.delete)
接下来,用postman 或者 其他测试软件自己跑一下...
egg-sequelize --- nodejs的更多相关文章
- 搭建项目vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize
vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize https://www.cnblogs.com/wuguanglin ...
- 【前端】nodejs的ORM框架sequelize的工厂化
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/sequelize_factory.html 一.什么是sequelize nodejs的后台在操作数据库的时候,需 ...
- Serverless + Egg.js 后台管理系统实战
本文将介绍如何基于 Egg.js 和 Serverless 实现一个后台管理系统 作为一名前端开发者,在选择 Nodejs 后端服务框架时,第一时间会想到 Egg.js,不得不说 Egg.js 是一个 ...
- Vue/Egg大型项目开发(一)搭建项目
项目Github地址:前端(https://github.com/14glwu/stuer)后端(https://github.com/14glwu/stuer-server) 项目线上预览:http ...
- egg.js 相关
egg sequelize 建表规范 CREATE TABLE `wx_member` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT 'primary key' ...
- egg源码浅析一npm init egg --type=simple
要egg文档最开始的时候,有这样的几条命令: 我们推荐直接使用脚手架,只需几条简单指令,即可快速生成项目: $ mkdir egg-example && cd egg-example ...
- nodejs项目mysql使用sequelize支持存储emoji
nodejs项目mysql使用sequelize支持存储emoji 本篇主要记录nodejs项目阿里云mysql如何支持存储emoji表情. 因由 最近项目遇到用户在文本输入emoji进行存储的时候导 ...
- koa2+log4js+sequelize搭建的nodejs服务
主要参考http://www.jianshu.com/p/6b816c609669这篇文章 npm安装使用国内taobao镜像,速度更快些 npm --registry https://registr ...
- 从 moment -> nodejs -> sequelize -> postgres,你都得设置好时区
背景 最近在做报表统计,因为 sequelize 的时区配置没加导致了统计数字对不上的问题. 问:大家都知道时区,但是你清楚 UTC 和 GMT 的区别吗? 答:UTC 是我们现在用的时间标准,GMT ...
随机推荐
- jenkins构建项目时报错缺少com.sun.image.codec.jpeg包解决方案
错误日志:error: package com.sun.image.codec.jpeg does not exist 网上找的一个项目,使用的是jdk1.7,除此之外其他服务器配置或是环境配置都是j ...
- 2019-ccpc秦皇岛现场赛
https://www.cnblogs.com/31415926535x/p/11625462.html 昨天和队友模拟了下今年秦皇岛的区域赛,,,(我全程在演 题目链接 D - Decimal 签到 ...
- Terminal MultipleXer---终端复用器tmux基本使用
Terminal MultipleXer---终端复用器tmux 使用场景:1.scp大文件 2:编译大文件 3:多窗口对比文件 1.安装tmux [root@localhost ~]# yum in ...
- [Note] Visual Studio Team Service 中的项目 转到 Git
Git-tf是微软发布的一个Git工具集的补充,用来让开发人员使用git命令与TFS交互,当然现在VSTS已经直接支持git了,现在讲讲以前用了VSTS的老项目如何转到git,保留所有的change ...
- ADB命令无法导出文件到物理机上处理办法
因为想查看一下脚本生成的sqlite文件.就想导出文件,,结果导出adb pull命令一直报错.使用su也是错误的..最后发现adb pull 不能再adb的命令状态下执行.需要退出adb命令.然后直 ...
- Flutter 错误捕获的正确姿势
背景 我们知道,在软件开发过程中,错误和异常总是在所难免. 不管是客户端的逻辑错误导致的,还是服务器的数据问题导致的,只要出现了异常,我们都需要一个机制来通知我们去处理. 在 APP 的开发过程中,我 ...
- asp.net core learn
.NET Core WebApi RESTful规范 RESTful API 最佳实践 理解RESTful架构 接口版本控制 Support multiple versions of ASP.NET ...
- B-概率论-极大似然估计
[TOC] 更新.更全的<机器学习>的更新网站,更有python.go.数据结构与算法.爬虫.人工智能教学等着你:https://www.cnblogs.com/nickchen121/ ...
- 非后端开发Mysql日常使用小结
数据库的五个概念 数据库服务器 数据库 数据表 数据字段 数据行 那么这里下面既是对上面几个概念进行基本的日常操作. 数据库引擎使用 这里仅仅只介绍常用的两种引擎,而InnoDB是从MySQL 5.6 ...
- idea破解版安装、配置jdk以及建立一个简单的maven工程
idea破解版安装.配置jdk,配置jdk环境变量以及建立一个简单的maven工程 一.idea破解版以及配置文件下载 下载网址:https://pan.baidu.com/s/1yojA51X1RU ...