BaseService.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { ReturnModelType } from '@typegoose/typegoose';
  2. import { AnyParamConstructor } from '@typegoose/typegoose/lib/types';
  3. import { Application, Context } from '@midwayjs/koa';
  4. import { App, Inject } from '@midwayjs/decorator';
  5. import _ = require('lodash');
  6. import { FrameworkErrorEnum, ServiceError } from '../error/service.error';
  7. import { GetModel } from '../util/getModel';
  8. import { SearchBase } from '../interface/SearchBase';
  9. import { pageOptions, resultOptions } from './options';
  10. /**
  11. * Service基类,实现了一些基础的crud
  12. */
  13. export abstract class BaseService<T extends AnyParamConstructor<any>> {
  14. @App()
  15. app: Application;
  16. @Inject()
  17. ctx: Context;
  18. // 要求继承基类的service,必须给上默认的model
  19. abstract model: ReturnModelType<T>;
  20. /**
  21. * 列表查询
  22. * @param {SearchBase} filter 查询条件
  23. * @param {object} pageOptions 分页条件
  24. * @param {Boolean} lean 是否使用JavaScript形式数据;false为mongoose的模型实例数据
  25. * @param {Boolean} populate 是否进行ref关联数据
  26. * @returns {Promise<object>} 返回列表
  27. */
  28. async query(filter: SearchBase, pageOptions: pageOptions = {}, resultOptions: resultOptions = { lean: true, populate: true }): Promise<Array<any>> {
  29. const dup = _.cloneDeep(filter.getFilter());
  30. const { lean, populate } = resultOptions;
  31. let refs = [];
  32. if (populate) refs = this.getRefs();
  33. const data = await this.model
  34. .find(dup, {}, { ...pageOptions })
  35. .populate(refs)
  36. .lean(lean);
  37. return data;
  38. }
  39. /**
  40. * 数据总数查询
  41. * @param {SearchBase} filter 查询条件
  42. * @returns {number} 数据总数
  43. */
  44. async count(filter: SearchBase): Promise<number> {
  45. const dup = _.cloneDeep(filter.getFilter());
  46. const total = await this.model.count(dup);
  47. return total;
  48. }
  49. /**
  50. * 单查询-通过id
  51. * @param {string} id 查询对象数据id
  52. * @param {boolean} lean 是否使用JavaScript形式数据
  53. * @returns {object|undefined}
  54. */
  55. async fetch(id: string, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object | undefined> {
  56. const { lean, populate } = resultOptions;
  57. let refs = [];
  58. if (populate) refs = this.getRefs();
  59. const data = await this.model.findById(id).populate(refs).lean(lean);
  60. return data;
  61. }
  62. /**
  63. * 单查询-通过任意条件
  64. * @param query 查询条件
  65. * @param resultOptions 结果处理
  66. */
  67. async findOne(query: object = {}, resultOptions: resultOptions = { lean: true, populate: true }): Promise<object | undefined> {
  68. const { lean, populate } = resultOptions;
  69. let refs = [];
  70. if (populate) refs = this.getRefs();
  71. const data = await this.model.findOne(query).populate(refs).lean(lean);
  72. return data;
  73. }
  74. /**
  75. * 单修改-通过id
  76. * @param {string} id 修改对象数据id
  77. * @param {object} body 要修改的内容
  78. * @returns {object}
  79. */
  80. async updateOne(id: string, body: object): Promise<string> {
  81. if (!id) throw new ServiceError('缺少查询信息', FrameworkErrorEnum.NEED_PARAMS);
  82. const num = await this.model.count({ _id: id });
  83. if (num <= 0) throw new ServiceError('未找到要修改的数据', FrameworkErrorEnum.NOT_FOUND_DATA);
  84. await this.model.updateOne({ _id: id }, body);
  85. return 'ok';
  86. }
  87. /**
  88. * 多修改
  89. * @param {SearchBase} filter 要修改的对象查询条件
  90. * @param {object} body 要修改的内容
  91. * @returns {object}
  92. */
  93. async updateMany(filter: SearchBase, body: object): Promise<string> {
  94. if (!body) throw new ServiceError('缺少修改信息', FrameworkErrorEnum.NEED_BODY);
  95. const dup = _.cloneDeep(filter.getFilter());
  96. await this.model.updateMany(dup, body);
  97. return 'ok';
  98. }
  99. /**
  100. * 单删除-通过id
  101. * @param id 要删除的数据id
  102. * @returns {object}
  103. */
  104. async delete(id: string): Promise<string> {
  105. if (!id) throw new ServiceError('缺少数据信息', FrameworkErrorEnum.NEED_PARAMS);
  106. await this.model.deleteOne({ _id: id });
  107. return 'ok';
  108. }
  109. /**
  110. * 多删除
  111. * @param {SearchBase} filter 要删除的数据查询条件
  112. * @returns {object}
  113. */
  114. async deleteMany(filter: SearchBase): Promise<string> {
  115. const keys = Object.keys(filter);
  116. if (keys.length <= 0) throw new ServiceError('暂不提供清库服务', FrameworkErrorEnum.SERVICE_FAULT);
  117. const dup = _.cloneDeep(filter.getFilter());
  118. await this.model.deleteMany(dup);
  119. return 'ok';
  120. }
  121. /**
  122. * 单创建
  123. * @param body 要创建的数据内容
  124. * @returns {object}
  125. */
  126. async create(body: object): Promise<object> {
  127. const data = await this.model.create(body);
  128. return data;
  129. }
  130. /**
  131. * 多创建
  132. * @param body 要创建的多个数据
  133. * @returns {object[]}
  134. */
  135. async createMany(body: object[]): Promise<Array<object>> {
  136. const data = await this.model.insertMany(body);
  137. return data;
  138. }
  139. /**
  140. * 获取本服务默认表的ref关系
  141. */
  142. getRefs(): Array<object> {
  143. const schema = _.get(this.model, 'schema.tree');
  144. const refs = [];
  145. for (const key in schema) {
  146. const f = schema[key];
  147. const ref = _.get(f, 'ref');
  148. if (ref) {
  149. const model = GetModel(ref);
  150. const path = key;
  151. refs.push({ path, model });
  152. }
  153. }
  154. return refs;
  155. }
  156. }