原文地址:koa+mysql+vue+socket.io全栈开发之数据访问篇javascript
后端搭起大致的框架后,接着涉及到的就是如何将数据持久化的问题,也就是对数据库进行 CURD 操做。前端
关于数据库方案, mongodb 和 mysql 都使用过,但我选用的是 mysql,缘由:vue
node-mysql 是用 sql 语句对 mysql 进行操做的库, 并无使用 Sequelize 这种 orm。由于我对 sql 熟悉,原生开发效率高。java
链接数据库我选用 链接池的方式,这种方式能高效的利用数据库链接node
//dbPool.js const mysql = require('mysql'); const dbconfig = require('../config/db'); const log = require('../common/logger'); let pool = null; /** * get the connection pool of database * 获取数据库链接池 */ exports.getPool = function () { if (!pool) { log.info("creating pool"); pool = mysql.createPool(dbconfig); } return pool; }
emoji 格式要用 utf8mb4 格式存储,因此这里链接字符集选用 utf8mb4,固然客户端和数据结果集 同样也要设置为 utf8mb4。mysql
module.exports={ host: "localhost", port: "3306", user: "root", password: "jeff", database: "chatdb", charset : 'utf8mb4',//utf8mb4才能保存emoji multipleStatements: true,// 可同时查询多条语句, 但不能参数化传值 connectionLimit: 100 //链接数量 };
基本的代码编写方式以下,每一个方法基本都是这么一种流程,获取数据库链接,执行 sql 语句,返回结果,处理异常。git
exports.queryInfo = function (params, callback){ pool.query('select ...', params, function (error, result, fields) { if (error) { log(error); callback(false); } else callback(result) }); }
这形成了一大堆重复的样板代码,咱们须要封装它,用 JavaScript 高阶函数特性 很容易就能实现,同时加上 Promise,调用时就能方便地用 async await 了,还有日志记录功能也加上。github
const pool = require("./dbPool").getPool(); const log = require('../common/logger'); /** * export named query function */ const exportDao = opts => Object.keys(opts).reduce((next, key) => { next[key] = (...args) => new Promise((resolve, reject) => { if (opts[key]) args.unshift(opts[key]); log.info('====== execute sql ======') log.info(args); pool.query(...args, (err, result, fields) => {// fields is useless if (err) reject(err) else resolve(result); }); }); return next; }, {});
userDao文件为例,使用 exportDao 直接就能把里面的 key-value 对象输出为 以key 为方法名的dao方法,挂载到 module.exports 下。sql
const { exportDao } = require('./common'); //直接就exports里面的key值对应的方法 module.exports = exportDao({ sql: null,// 有些时候须要直接写sql count: 'select count(*) as count from user where ?', getUser: 'select * from user where ?', insert: 'insert into user set ?', update: 'update user set ? where id = ?', delete: 'delete from user where ?' }); /* 最终输出格式 module.exports = { sql:() => {}, count:() => {}, ... }*/
还有事务 transaction 的功能须要用到,来看一下 node-mysql 官方的例子,层层回调😢,若是每一个业务都要这样编写,简直不能忍,咱们仍是手动封装下事务吧。mongodb
// 官方的事务样例 pool.getConnection(function(err, connection) { if (err) { throw err; } connection.beginTransaction(function(err) { if (err) { throw err; } connection.query('INSERT INTO posts SET title=?', title, function (error, results, fields) { if (error) { return connection.rollback(function() { throw error; }); } var log = 'Post ' + results.insertId + ' added'; connection.query('INSERT INTO log SET data=?', log, function (error, results, fields) { if (error) { return connection.rollback(function() { throw error; }); } connection.commit(function(err) { if (err) { return connection.rollback(function() { throw err; }); } console.log('success!'); }); }); }); }); });
下面就是封装好的事务方法,调用参数为数组,数组项既能够是 sql 字符串,也能够是 node-mysql 中的带参数传值的数组,这才是给人用的嘛,心情好多了。推荐仍是用 参数化传值,这样能够避免 sql 注入,若是确实要用sql字符串,能够调用 mysql.escape 方法对 参数 进行转换。
//调用封装后的事务 const rets = await transaction([ ["insert into user_group values (?,?)",[11,11]], ["insert into user_friend set ? ",{user_id:'12',friend_id:12}], 'select * from user' ]); /** * sql transaction 封装后的事务 * @param {Array} list */ const transaction = list => { return new Promise((resolve, reject) => { if (!Array.isArray(list) || !list.length) return reject('it needs a Array with sql') pool.getConnection((err, connection) => { if (err) return reject(err); connection.beginTransaction(err => { if (err) return reject(err); log.info('============ begin execute transaction ============') let rets = []; return (function dispatch(i) { let args = list[i]; if (!args) {//finally commit connection.commit(err => { if (err) { connection.rollback(); connection.release(); return reject(err); } log.info('============ success executed transaction ============') connection.release(); resolve(rets); }); } else { log.info(args); args = typeof args == 'string' ? [args] : args; connection.query(...args, (error, ret) => { if (error) { connection.rollback(); connection.release(); return reject(error); } rets.push(ret); dispatch(i + 1); }); } })(0); }); }); }) }
都封装完毕,最后就是调用, 就以apply控制器为例,其中的 apply 方法是普通调用,accept 方法则使用了事务进行处理。
const { stringFormat } = require('../common/util') const { transaction } = require('../daos/common') const applyDao = require('../daos/apply') exports.apply = async function (ctx) { const form = ctx.request.body; const token = await ctx.verify(); const ret = await applyDao.apply({ ...form, from_id: token.uid }); if (!ret.affectedRows) { return ctx.body = { code: 2, message: '申请失败' }; } ctx.body = { code: 0, message: '申请成功', data:ret.insertId }; } exports.accept = async function (ctx) { const { id, friend_id } = ctx.request.body; const token = await ctx.verify(); const ret = await transaction([// 非用户输入的 id,没有使用 escape 进行转换。 ['update apply set status = 1 where id = ? and to_id = ?', [id, token.uid]], stringFormat("replace into user_friend values ('$1','$2'),('$2','$1')", token.uid, friend_id) ]); if (!ret[0].affectedRows || !ret[1].affectedRows) { return ctx.body = { code: 2, message: '添加好友失败' }; } ctx.body = { code: 0, message: '添加好友成功' }; }
固然还须要定义数据结构,有不少工具能够方便建表和建生产sql,这里以部分表为例,项目使用到的表要多得多。我这里还写了些可有可无的触发器处理 数据插入时间 和数据修改时间,这是我的的习惯。彻底能够不用触发器,直接在代码里面赋值,不影响使用。有用到 emoji 的数据表,记得要用 utf8mb4 格式。
create database if not exists chatdb; use chatdb; drop table if exists `user`; CREATE TABLE `user` ( `id` char(36) NOT NULL DEFAULT '' COMMENT '主键', `name` varchar(50) DEFAULT NULL COMMENT '用户名', `num` int(8) DEFAULT NULL COMMENT '用户号码', `salt` varchar(13) DEFAULT NULL COMMENT '加密的盐', `hash_password` varchar(64) DEFAULT NULL COMMENT '加密后的密码', `email` varchar(50) NOT NULL COMMENT 'email地址', `nick` varchar(50) DEFAULT NULL COMMENT '昵称', `avatar` varchar(200) DEFAULT NULL COMMENT '头像', `signature` varchar(200) DEFAULT NULL COMMENT '个性签名', `status` tinyint(1) DEFAULT 0 COMMENT '状态(0 离线 1 在线 2 隐身)', `is_admin` tinyint(1) DEFAULT 0 COMMENT '是否管理员', `is_block` tinyint(1) DEFAULT 0 COMMENT '是否禁用', `create_date` int(10) unsigned DEFAULT NULL COMMENT '注册时间', `update_date` int(10) unsigned DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户表'; drop table if exists `message`; CREATE TABLE `message` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `content` text NOT NULL COMMENT '内容', `type` tinyint(1) DEFAULT 0 COMMENT '类型(0 用户 1 组群)', `send_id` char(36) NOT NULL COMMENT '发送用户id', `receive_id` char(36) DEFAULT NULL COMMENT '接收用户id', `group_id` int(11) DEFAULT NULL COMMENT '组id', `create_date` int(10) unsigned DEFAULT NULL COMMENT '建立时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='消息表'; drop table if exists `user_message`; CREATE TABLE `user_message` ( `user_id` char(36) DEFAULT NULL COMMENT '接收用户id', `send_id` char(36) NOT NULL COMMENT '发送用户id', `message_id` int(11) NOT NULL COMMENT '消息id', `is_read` tinyint(1) DEFAULT 0 COMMENT '是否读过(0 没有 1 读过)', PRIMARY KEY (`send_id`,`message_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户消息链接表'; -- user表insert触发器 delimiter $$ create trigger `user_insert` before insert on `user` for each ROW begin if (new.id = '' or new.id is null) then set new.id = uuid(); end if; if (new.num = 0 or new.num is null) then set new.num = 1000; end if; if (new.`create_date` = 0 or new.`create_date` is null) then set new.`create_date` = unix_timestamp(now()); end if; if (new.`update_date` = 0 or new.`update_date` is null) then set new.`update_date` = unix_timestamp(now()); end if; END $$ -- user表update触发器 delimiter $$ create trigger `user_update` before update on `user` for each ROW begin if ((new.`name` <> old.`name`) or (new.`name` is not null and old.`name` is null) or (new.`email` <> old.`email`) or (new.`email` is not null and old.`email` is null) or (new.`nick` <> old.`nick`) or (new.`nick` is not null and old.`nick` is null) or (new.`avatar` <> old.`avatar`) or (new.`avatar` is not null and old.`avatar` is null) or (new.`signature` <> old.`signature`) or (new.`signature` is not null and old.`signature` is null)) then set new.`update_date` = unix_timestamp(now()); end if; END $$ -- message表insert触发器 delimiter $$ create trigger `message_insert` before insert on `message` for each ROW begin if (new.`create_date` = 0 or new.`create_date` is null) then set new.`create_date` = unix_timestamp(now()); end if; END $$
接着就是咱们的大前端部分了,将会用到 vue,vuex,请继续关注。