数据.数据历史
12003项目简介
此章节为JianghuJS 中级
advances模板项目中数据历史模块, 介绍以数据行维度记录数据的变更历史,主要用做数据恢复&数据备份
功能使用
概览

初始化sql
数据历史表_record_history
CREATE TABLE `_record_history` (`id` int(11) NOT NULL AUTO_INCREMENT,`table` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '表',`recordId` int(11) DEFAULT NULL COMMENT '数据在table中的主键id; recordContent.id',`recordContent` text COLLATE utf8mb4_bin NOT NULL COMMENT '数据JSON',`packageContent` text COLLATE utf8mb4_bin NOT NULL COMMENT '当时请求的 package JSON',`operation` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '操作; jhInsert, jhUpdate, jhDelete jhRestore',`operationByUserId` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '操作者userId; recordContent.operationByUserId',`operationByUser` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '操作者用户名; recordContent.operationByUser',`operationAt` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '操作时间; recordContent.operationAt; E.g: 2021-05-28T10:24:54+08:00 ',PRIMARY KEY (`id`),KEY `index_record_id` (`recordId`),KEY `index_table_action` (`table`,`operation`)) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='数据历史表';
搜索
后端处理
点击搜索,数据从后端返回全部数据,前端页面完成数据分页

搜索条件配置
搜索
数据表配置// 可操作数据表, 如有需要手动添加即可constantObj: {table: ["_user"],}
搜索
数据类型配置// 可操作数据类型constantObj: {dataType: [{"value": "onUse", "text": "使用中的数据"},{"value": "deleted", "text": "已删除的数据"},],}
前端处理
搜索框内数据需要搜索的关键字,前端直接在已有的数据中搜索,不会发起http请求

数据还原
用于恢复修改的数据, 操作恢复后,数据将恢复到当前数据状态

创建recordHistory
const operation = 'jhRestore';const operationAt = dayjs().format();const operationByUserId = userId;const operationByUser = username;const newData = { ...record, operation, operationAt, operationByUserId, operationByUser };await trx('_record_history').insert({table, recordId: newData.id, recordContent: JSON.stringify(newData),packageContent: '{}',operation, operationAt, operationByUserId, operationByUser,});
恢复数据
const recordListTemp = await trx(table).where({ id: recordId }).select();if (recordListTemp.length > 0) {await trx(table).where({ id: recordId }).update(newData);} else {await trx(table).insert(newData);}