crud-service.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. 'use strict';
  2. const { isString, isArray, cloneDeep, head: getHead, get } = 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. class CrudService extends NafService {
  9. async create(data) {
  10. assert(data);
  11. // TODO:保存数据
  12. const res = await this.model.create(data);
  13. return res;
  14. }
  15. async update(filter, update, { projection } = {}) {
  16. assert(filter);
  17. assert(update);
  18. const { _id, id } = filter;
  19. if (_id || id) filter = { _id: ObjectId(_id || id) };
  20. // TODO:检查数据是否存在
  21. const entity = await this.model.findOne(filter).exec();
  22. if (isNullOrUndefined(entity)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
  23. // TODO: 修改数据
  24. entity.set(trimData(update));
  25. await entity.save();
  26. return await this.model.findOne(filter, projection).exec();
  27. }
  28. async delete(filter) {
  29. assert(filter);
  30. const { _id, id } = filter;
  31. if (_id || id) {
  32. await this.model.findByIdAndDelete(_id || id).exec();
  33. } else {
  34. await this.model.deleteMany(filter).exec();
  35. }
  36. return 'deleted';
  37. }
  38. async fetch(filter, { sort, desc, projection } = {}) {
  39. assert(filter);
  40. const { _id, id } = filter;
  41. if (_id || id) filter = { _id: ObjectId(_id || id) };
  42. // 处理排序
  43. if (sort && isString(sort)) {
  44. sort = { [sort]: desc ? -1 : 1 };
  45. } else if (sort && isArray(sort)) {
  46. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  47. }
  48. return await this.model.findOne(filter, projection).exec();
  49. }
  50. async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
  51. // 处理排序
  52. if (sort && isString(sort)) {
  53. sort = { [sort]: desc ? -1 : 1 };
  54. } else if (sort && isArray(sort)) {
  55. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  56. }
  57. let condition = cloneDeep(filter);
  58. // 分站模式确认
  59. condition = this.dealFilter(condition);
  60. const isOpenMultiTenancy = get(this.app, 'plugins.multiTenancy');
  61. if (isOpenMultiTenancy) {
  62. const _tenant = this.isMultiTenancy();
  63. if (_tenant) condition._tenant = _tenant;
  64. }
  65. const pipeline = [{ $match: condition }];
  66. // 先排序
  67. if (sort) pipeline.push({ $sort: sort });
  68. // 再进行分页
  69. if (limit) {
  70. pipeline.push({ $skip: parseInt(skip) });
  71. pipeline.push({ $limit: parseInt(limit) });
  72. }
  73. // 再将数据过滤
  74. if (projection) pipeline.push({ $project: projection });
  75. else {
  76. const defaultProject = await this.getSelectFalse();
  77. if (defaultProject)pipeline.push({ $project: defaultProject });
  78. }
  79. const rs = await this.model.aggregate(pipeline);
  80. // const rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).exec();
  81. return rs;
  82. }
  83. async count(filter) {
  84. let condition = cloneDeep(filter);
  85. condition = this.dealFilter(condition);
  86. // 分站模式确认
  87. const isOpenMultiTenancy = get(this.app, 'plugins.multiTenancy');
  88. if (isOpenMultiTenancy) {
  89. const _tenant = this.isMultiTenancy();
  90. if (_tenant) condition._tenant = _tenant;
  91. }
  92. let count = 0;
  93. const res = await this.model.aggregate([{ $match: condition }, { $count: 'id' }]);
  94. if (res && isArray(res)) {
  95. const head = getHead(res);
  96. count = get(head, 'id', 0);
  97. }
  98. return count;
  99. }
  100. /**
  101. * 判断默认model是否是分站模式
  102. * 是分站模式且不是master,就返回分站标识
  103. */
  104. isMultiTenancy() {
  105. const is_multi = this.model.prototype.schema.options['multi-tenancy'];
  106. const tenant = this.model.prototype.schema.options['x-tenant'];
  107. if (is_multi && tenant !== 'master') return tenant;
  108. }
  109. async queryAndCount(filter, options) {
  110. filter = this.dealFilter(filter);
  111. const total = await this.count(filter);
  112. if (total === 0) return { total, data: [] };
  113. const rs = await this.query(filter, options);
  114. return { total, data: rs };
  115. }
  116. dealFilter(filter) {
  117. return this.turnFilter(this.turnDateRangeQuery(filter));
  118. }
  119. turnFilter(filter) {
  120. const str = /^%\S*%$/;
  121. let keys = Object.keys(filter);
  122. for (const key of keys) {
  123. const res = key.match(str);
  124. if (res) {
  125. const newKey = key.slice(1, key.length - 1);
  126. if (!ObjectId.isValid(filter[key])) filter[newKey] = new RegExp(filter[key]);
  127. delete filter[key];
  128. }
  129. }
  130. // 再次过滤数据,将数组的数据都变成{$in:value},因为查询变成了聚合查询
  131. keys = Object.keys(filter);
  132. for (const key of keys) {
  133. if (isArray(filter[key])) {
  134. filter[key] = { $in: filter[key] };
  135. }
  136. }
  137. return filter;
  138. }
  139. turnDateRangeQuery(filter) {
  140. const keys = Object.keys(filter);
  141. const times = [];
  142. for (const k of keys) {
  143. if (k.includes('@')) {
  144. const karr = k.split('@');
  145. if (karr.length === 2) {
  146. const prefix = karr[0];
  147. const type = karr[1];
  148. if (type === 'start') {
  149. if (filter[k] && filter[k] !== '') {
  150. const obj = { key: prefix, opera: '$gte', value: new Date(filter[k]) };
  151. times.push(obj);
  152. }
  153. } else {
  154. if (filter[k] && filter[k] !== '') {
  155. const obj = { key: prefix, opera: '$lte', value: new Date(filter[k]) };
  156. times.push(obj);
  157. }
  158. }
  159. delete filter[k];
  160. }
  161. }
  162. }
  163. for (const i of times) {
  164. const { key, opera, value } = i;
  165. if (!filter[key]) {
  166. filter[key] = {};
  167. }
  168. filter[key][opera] = value;
  169. }
  170. return filter;
  171. }
  172. /**
  173. * 获取model的配置
  174. */
  175. async getModel() {
  176. const obj = this.model.prototype.schema.obj;
  177. return obj;
  178. }
  179. /**
  180. * 读取model中不显示的字段
  181. */
  182. async getSelectFalse() {
  183. const obj = await this.getModel();
  184. const project = {};
  185. for (const k in obj) {
  186. const v = obj[k];
  187. if (v && v.select === false) project[k] = 0;
  188. }
  189. if (Object.keys(project).length > 0) return project;
  190. }
  191. }
  192. module.exports.CrudService = CrudService;