聚名网登录,做企业网站排名优化要多少钱,建设项目查询网站,企业营销网站怎样做前言
接上文 Egg框架搭建后台服务【1】#xff0c;继续优化后台服务#xff0c;之前直接用 SQL 语句调用的数据库数据#xff0c;既不安全#xff0c;也比较麻烦#xff0c;当然最重要的是“显着不专业”。
所以本文仍然是增删改查#xff0c;重点是将原本 SQL 语句操作…前言
接上文 Egg框架搭建后台服务【1】继续优化后台服务之前直接用 SQL 语句调用的数据库数据既不安全也比较麻烦当然最重要的是“显着不专业”。
所以本文仍然是增删改查重点是将原本 SQL 语句操作改为 ORM 框架 sequelize 操作。
开发
初始化
安装
安装 sequelize 框架及 mysql2 库我这里用的是MySQL所以安装 mysql2库如果使用的是其他数据库参考 官方文档 安装相应连接器库。
npm install --save egg-sequelize mysql2配置
在 config/plugin.js 中引入egg-sequelize 插件。
module.exports {sequelize: {enable: true,package: egg-sequelize},
};在 config/config.default.js 中编写配置
/* eslint valid-jsdoc: off *//*** param {Egg.EggAppInfo} appInfo app info*/
module.exports appInfo {/*** built-in config* type {Egg.EggAppConfig}**/const config exports {};// use for cookie sign key, should change to your own and keep securityconfig.keys appInfo.name 123;// add your middleware config hereconfig.middleware [];// 关闭安全配置config.security {csrf: {enable: false,}};// 添加sequelize配置config.sequelize {dialect: mysql,host: 127.0.0.1,port: 3306,database: egg-blogs,username: root,password: 12345678}// add your user config hereconst userConfig {// myAppName: egg,};return {...config,...userConfig,};
};
开发逻辑
Model
创建 app/model/Tag.js
module.exports (app) {const {STRING} app.Sequelize;const Tag app.model.define(tag, {id: {type: STRING, primaryKey: true, autoIncrement: true},tag_name: STRING,tag_color: STRING,remark: STRING,}, {timestamps: true,createdAt: create_time,updatedAt: update_time,tableName: tag});return Tag;
}注意这里一定要指定主键配置部分如果数据库中表明为 tags可以不指定 tableName sequelize 会自动推定表名。表字段如果不具备 createAt、updateAt可以设置为 false 去除也可以映射为其他字段。
Service
修改原本的 app/service/tag.js
const {Service} require(egg);class TagsService extends Service {async getTagList(params) {const {ctx, app} this;const {Op} app.Sequelize;const query {where: {tag_name: {[Op.like]: %${params.tagName || }%}}}const tagList await ctx.model.Tag.findAll(query);ctx.status 200;ctx.body {code: 200,success: true,data: tagList,msg: 获取标签列表成功,show: false}}async createTag(params) {const {ctx, app} this;const tagInfo {id: ctx.helper.snowflakeId(),tagName: params.tagName,tagColor: params.tagColor,remark: params.remark}const result await app.model.Tag.create(tagInfo)ctx.status 200;ctx.body {code: 200,success: true,data: result,msg: 创建标签成功,show: true}}async updateTag(params) {const {ctx, app} this;const tagInfo {tagName: params.tagName,tagColor: params.tagColor,remark: params.remark}const result await ctx.model.Tag.update(tagInfo, {where: {id: params.id}});ctx.status 200;ctx.body {code: 200,success: true,data: result,msg: 更新标签成功,show: true}}async deleteTag(params) {const {ctx, app} this;const ids params.ids.split(,);await ctx.model.Tag.destroy({where: {id: ids}});ctx.status 200;ctx.body {code: 200,success: true,msg: 删除标签成功,show: true}}
}module.exports TagsService;注意这里因为是初学仅实现就收手了还有可优化的地方。
总结
相较于 MySQL 直连操作数据库的方案ORM 的方案无疑更加安全也更加专业。后端在实现数据库操作的时候基本上都使用的 ORM 框架这种实现方式在多人开发的时候不至于 SQL 语句写的满天飞。
问题
sequelize 框架具体逻辑并不太清楚很多地方仍存在优化空间尤其是错误处理。数据库字段和前后端数据对接方面问题仍然不清楚有些字段使用 tagName 的小驼峰方式仍可以走通但是有些地方必须和数据库一致写 tag_name。数据库配置项数据应该是动态的独立于项目外的实现配置化此处仍不太清楚。