BaseService.ts 4.3 KB

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