crud-service.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { isNullOrUndefined, trimData } = require('naf-core').Util;
  4. const assert = require('assert');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const { NafService } = require('./naf-service');
  8. const mongoose = require('mongoose');
  9. class CrudService extends NafService {
  10. /**
  11. * 创建前处理函数
  12. * @param {Object} data 请求参数体
  13. */
  14. async beforeCreate(data) {
  15. return data;
  16. }
  17. /**
  18. * 创建后处理函数
  19. * @param {Object} body 请求参数体
  20. * @param {Object} data 数据
  21. */
  22. async afterCreate(body, data) {
  23. return data;
  24. }
  25. async create(data) {
  26. assert(data);
  27. // TODO:保存数据
  28. data = await this.beforeCreate(data);
  29. let res = await this.model.create(data);
  30. res = await this.afterCreate(data, res);
  31. return res;
  32. }
  33. /**
  34. * 修改前处理函数;需要将 查询条件和数据返回 (提供重写用,免去中间件)
  35. * @param {Object} filter 查询条件
  36. * @param {Object} update 数据
  37. * @return {Object} 返回查询条件和数据
  38. */
  39. async beforeUpdate(filter, update) {
  40. return { filter, update };
  41. }
  42. /**
  43. * 修改后处理函数;需要将 数据返回 (提供重写用,免去中间件)
  44. * @param {Object} filter 查询条件
  45. * @param {Object} body 请求参数体
  46. * @param {Object} data 数据
  47. * @return {Object} 返回修改后的数据
  48. */
  49. async afterUpdate(filter, body, data) {
  50. return data;
  51. }
  52. async update(filter, update, { projection } = {}) {
  53. assert(filter);
  54. assert(update);
  55. const beforeUpdateResult = await this.beforeUpdate(filter, update);
  56. filter = beforeUpdateResult.filter;
  57. update = beforeUpdateResult.update;
  58. const { _id, id } = filter;
  59. if (_id || id) filter = { _id: ObjectId(_id || id) };
  60. // TODO:检查数据是否存在
  61. const entity = await this.model.findOne(filter).exec();
  62. if (isNullOrUndefined(entity)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  63. // TODO: 修改数据
  64. entity.set(trimData(update));
  65. await entity.save();
  66. let reSearchData = await this.model.findOne(filter, projection).exec();
  67. reSearchData = await this.afterUpdate(filter, update, reSearchData);
  68. return reSearchData;
  69. }
  70. /**
  71. * 删除前处理函数
  72. * @param {Object} filter 查询条件
  73. * @return {Object} 查询条件
  74. */
  75. async beforeDelete(filter) {
  76. return filter;
  77. }
  78. /**
  79. * 删除后处理函数
  80. * @param {Object} filter 查询条件
  81. */
  82. async afterDelete(filter) {}
  83. async delete(filter) {
  84. assert(filter);
  85. filter = await this.beforeDelete(filter);
  86. const { _id, id } = filter;
  87. if (_id || id) {
  88. await this.model.findByIdAndDelete(_id || id).exec();
  89. } else {
  90. await this.model.deleteMany(filter).exec();
  91. }
  92. await this.afterDelete(filter);
  93. return 'deleted';
  94. }
  95. /**
  96. * 查询前处理函数
  97. * @param {Object} filter 查询条件
  98. * @return {Object} 查询条件
  99. */
  100. async beforeFetch(filter) {
  101. return filter;
  102. }
  103. /**
  104. * 查询后处理函数
  105. * @param {Object} filter 查询条件
  106. * @param {Object} data 数据
  107. * @return {Object} 查询条件
  108. */
  109. async afterFetch(filter, data) {
  110. return data;
  111. }
  112. async fetch(filter, { sort, desc, projection } = {}) {
  113. assert(filter);
  114. filter = await this.beforeFetch(filter);
  115. const { _id, id } = filter;
  116. if (_id || id) filter = { _id: ObjectId(_id || id) };
  117. // 处理排序
  118. if (sort && _.isString(sort)) {
  119. sort = { [sort]: desc ? -1 : 1 };
  120. } else if (sort && _.isArray(sort)) {
  121. sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  122. }
  123. let res = await this.model.findOne(filter, projection).exec();
  124. res = await this.afterFetch(filter, res);
  125. return res;
  126. }
  127. async beforeQuery(filter) {
  128. return filter;
  129. }
  130. async afterQuery(filter, data) {
  131. return data;
  132. }
  133. async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
  134. // 处理排序
  135. if (sort && _.isString(sort)) {
  136. sort = { [sort]: desc ? -1 : 1 };
  137. } else if (sort && _.isArray(sort)) {
  138. sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  139. }
  140. let condition = _.cloneDeep(filter);
  141. condition = await this.beforeQuery(condition);
  142. condition = this.dealFilter(condition);
  143. // 过滤出ref字段
  144. const { refMods, populate } = this.getRefMods();
  145. const queryRefs = _.get(this.app, 'config.queryRefs');
  146. let rs;
  147. if (queryRefs) {
  148. // 带ref查询
  149. rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).populate(populate).exec();
  150. rs = JSON.parse(JSON.stringify(rs));
  151. // 整理ref数据
  152. rs = rs.map((i) => {
  153. for (const obj of refMods) {
  154. const { col, prop, type } = obj;
  155. if (!prop) continue;
  156. if (_.isArray(prop)) {
  157. for (const p of prop) {
  158. if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
  159. if (type === 'Array') {
  160. const list = [];
  161. const oList = _.get(i, `${col}`);
  162. for (const d of oList) {
  163. const obj = { _id: d._id };
  164. obj[p] = _.get(d, p);
  165. list.push(obj);
  166. }
  167. i[`${col}_${p}`] = list;
  168. }
  169. }
  170. i[col] = _.get(i, `${col}._id`);
  171. }
  172. }
  173. return i;
  174. });
  175. } else {
  176. rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).exec();
  177. }
  178. rs = await this.afterQuery(filter, rs);
  179. return rs;
  180. }
  181. async count(filter) {
  182. let condition = _.cloneDeep(filter);
  183. condition = await this.beforeQuery(condition);
  184. condition = this.dealFilter(condition);
  185. const count = await this.model.count(condition);
  186. return count;
  187. }
  188. async queryAndCount(filter, options) {
  189. filter = this.dealFilter(filter);
  190. const total = await this.count(filter);
  191. if (total === 0) return { total, data: [] };
  192. const rs = await this.query(filter, options);
  193. return { total, data: rs };
  194. }
  195. dealFilter(filter) {
  196. return this.turnFilter(this.turnDateRangeQuery(filter));
  197. }
  198. turnFilter(filter) {
  199. const str = /^%\S*%$/;
  200. // $是mongodb固定条件,不用处理;大多为手写特殊处理过的条件
  201. let keys = Object.keys(filter).filter((f) => !f.includes('$'));
  202. for (const key of keys) {
  203. const res = key.match(str);
  204. if (res) {
  205. const newKey = key.slice(1, key.length - 1);
  206. if (!ObjectId.isValid(filter[key])) filter[newKey] = new RegExp(filter[key]);
  207. delete filter[key];
  208. }
  209. }
  210. // 再次过滤数据,将数组的数据都变成{$in:value},因为查询变成了聚合查询
  211. keys = Object.keys(filter).filter((f) => !f.includes('$'));
  212. for (const key of keys) {
  213. if (_.isArray(filter[key])) {
  214. filter[key] = { $in: filter[key] };
  215. } else if (filter[key] === 'true' || filter[key] === 'false') {
  216. // 布尔类型的值检查,如果是布尔类型,则将字符串转为布尔
  217. filter[key] = filter[key] === 'true';
  218. }
  219. }
  220. return filter;
  221. }
  222. turnDateRangeQuery(filter) {
  223. const keys = Object.keys(filter);
  224. const times = [];
  225. for (const k of keys) {
  226. if (k.includes('@')) {
  227. const karr = k.split('@');
  228. if (karr.length === 2) {
  229. const prefix = karr[0];
  230. const type = karr[1];
  231. if (type === 'start') {
  232. if (filter[k] && filter[k] !== '') {
  233. const obj = { key: prefix, opera: '$gte', value: filter[k] };
  234. times.push(obj);
  235. }
  236. } else {
  237. if (filter[k] && filter[k] !== '') {
  238. const obj = { key: prefix, opera: '$lte', value: filter[k] };
  239. times.push(obj);
  240. }
  241. }
  242. delete filter[k];
  243. }
  244. }
  245. }
  246. for (const i of times) {
  247. const { key, opera, value } = i;
  248. if (!filter[key]) {
  249. filter[key] = {};
  250. }
  251. filter[key][opera] = value;
  252. }
  253. return filter;
  254. }
  255. /**
  256. * 读取model中不显示的字段
  257. */
  258. async getSelectFalse() {
  259. const obj = await this.getModel();
  260. const project = {};
  261. for (const k in obj) {
  262. const v = obj[k];
  263. if (v && v.select === false) project[k] = 0;
  264. }
  265. if (Object.keys(project).length > 0) return project;
  266. }
  267. getRefMods() {
  268. // 找到该表的schema(表结构)
  269. const mod = this.getSchema();
  270. const populate = this.resetPopulate(mod);
  271. const refMods = [];
  272. for (const key in mod) {
  273. if (!mod[key].ref && !mod[key].refPath) continue;
  274. const obj = { col: key, prop: mod[key].getProp, type: mod[key].type.name };
  275. refMods.push(obj);
  276. }
  277. return { refMods, populate };
  278. }
  279. // 格式化model路径
  280. formatModelPath(str) {
  281. let arr = str.split('.');
  282. arr = arr.map((i) => _.upperFirst(i));
  283. const modelPath = arr.join('.');
  284. return modelPath;
  285. }
  286. // 获取model的模式
  287. getSchema(path) {
  288. const model = this.getModel(path);
  289. let schema = null;
  290. let version = mongoose.version;
  291. version = parseInt(version.split('.').join(''));
  292. if(version<573) schema = _.get(model, 'prototype.schema.obj');
  293. else schema = _.get(model, 'prototype.schema.obj.obj');
  294. return schema;
  295. }
  296. // 获取model实例
  297. getModel(path) {
  298. if (!path) return this.model;
  299. let model = _.get(this.ctx.model, path);
  300. const clients = this.app.mongooseDB.clients;
  301. if (clients && !model) {
  302. model = _.get(this.ctx.model, `${this.app.config.defaultModule}.${path}`);
  303. }
  304. return model;
  305. }
  306. // 针对每个表进行检查
  307. resetPopulate(schema) {
  308. const arr = [];
  309. for (const key in schema) {
  310. const e = schema[key];
  311. const { ref } = e;
  312. if (!ref) continue;
  313. const obj = { path: key };
  314. const modelPath = this.formatModelPath(ref);
  315. const model = this.getModel(modelPath);
  316. obj.model = model;
  317. const msch = this.getSchema(modelPath);
  318. const popu = this.resetPopulate(msch);
  319. if (popu.length > 0) obj.populate = popu;
  320. arr.push(obj);
  321. }
  322. return arr;
  323. }
  324. }
  325. // 聚合式查询
  326. // const pipeline = [{ $match: condition }];
  327. // // 先排序
  328. // if (sort) pipeline.push({ $sort: sort });
  329. // // 再进行分页
  330. // if (limit) {
  331. // pipeline.push({ $skip: parseInt(skip) });
  332. // pipeline.push({ $limit: parseInt(limit) });
  333. // }
  334. // // 再将数据过滤
  335. // if (projection) pipeline.push({ $project: projection });
  336. // else {
  337. // const defaultProject = await this.getSelectFalse();
  338. // if (defaultProject) pipeline.push({ $project: defaultProject });
  339. // }
  340. // let rs = await this.model.aggregate(pipeline);
  341. module.exports.CrudService = CrudService;