ts-template.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. const _ = require('lodash');
  2. const getModelType = (type, interf = false) => {
  3. let modelType = 'string';
  4. if (type === 'Money' || type === 'money') {
  5. if (!interf) modelType = 'Decimal128';
  6. else modelType = 'number';
  7. } else if (type === 'ObjectId') modelType = 'ObjectId';
  8. else if (type === 'Array') modelType = 'Array';
  9. else modelType = _.lowerFirst(type);
  10. return modelType;
  11. };
  12. const ModelContext = (data) => {
  13. const result = [];
  14. const { name: table_name, columns } = data;
  15. const prefix = _.upperFirst(table_name);
  16. let fc = [];
  17. fc.push(`import { modelOptions, prop } from '@typegoose/typegoose';`);
  18. fc.push(`import { BaseModel } from 'free-midway-component';`);
  19. const has_money = columns.find((f) => f.type === 'Money' || f.type === 'money');
  20. if (has_money) fc.push(`import { Decimal128 } from 'mongoose';`);
  21. fc.push(`@modelOptions({`);
  22. fc.push(` schemaOptions: { collection: '${table_name}' },`);
  23. fc.push(`})`);
  24. fc.push(`export class ${prefix} extends BaseModel {`);
  25. // 处理字段
  26. for (const col of columns) {
  27. const { type, title, def } = col;
  28. const prop = _.pick(col, ['required', 'index', 'zh', 'remark']);
  29. if (def) prop.default = def;
  30. const modelType = getModelType(type);
  31. fc.push(` @prop(${JSON.stringify(prop)})`);
  32. fc.push(` ${title}: ${modelType}`);
  33. }
  34. fc.push(`}`);
  35. // 最后换行转为字符串
  36. const fileContext = fc.join('\n');
  37. return fileContext;
  38. };
  39. const ServiceContext = (data) => {
  40. const fc = [];
  41. const { name: table_name } = data;
  42. const prefix = _.upperFirst(table_name);
  43. fc.push(`import { Provide } from '@midwayjs/decorator';`);
  44. fc.push(`import { InjectEntityModel } from '@midwayjs/typegoose';`);
  45. fc.push(`import { ReturnModelType } from '@typegoose/typegoose';`);
  46. fc.push(`import { BaseService } from 'free-midway-component';`);
  47. fc.push(`import { ${prefix} } from '../entity/${table_name}';`);
  48. fc.push(`type modelType = ReturnModelType<typeof ${prefix}>;`);
  49. fc.push(`@Provide()`);
  50. fc.push(`export class ${prefix}Service extends BaseService<modelType> {`);
  51. fc.push(` @InjectEntityModel(${prefix})`);
  52. fc.push(` model: modelType;`);
  53. fc.push(`}`);
  54. return fc.join('\n');
  55. };
  56. const ControllerContext = (data) => {
  57. const fc = [];
  58. const { name, name_zh } = data;
  59. const prefix = _.upperFirst(name);
  60. fc.push(`import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/decorator';`);
  61. fc.push(`import { BaseController } from 'free-midway-component';`);
  62. fc.push(`import { ${prefix}Service } from '../service/${name}.service';`);
  63. fc.push(
  64. `import { CreateDTO_${name}, CreateVO_${name}, FetchVO_${name}, QueryDTO_${name}, QueryVO_${name}, UpdateDTO_${name}, UpdateVO_${name} } from '../interface/${name}.interface';`
  65. );
  66. fc.push(`import { ApiResponse, ApiTags } from '@midwayjs/swagger';`);
  67. fc.push(`import { Validate } from '@midwayjs/validate';`);
  68. fc.push(`@ApiTags(['${name_zh}'])`);
  69. fc.push(`@Controller('/${name}')`);
  70. fc.push(`export class ${prefix}Controller extends BaseController {`);
  71. fc.push(` @Inject()`);
  72. fc.push(` service: ${prefix}Service;`);
  73. fc.push('\n');
  74. // create
  75. fc.push(`@Post('/') @Validate() @ApiResponse({ type: CreateVO_${name} })`);
  76. fc.push(` async create(@Body() data: CreateDTO_${name}) {`);
  77. fc.push(` const result = await this.service.create(data);`);
  78. fc.push(` return result;`);
  79. fc.push(` }`);
  80. // query
  81. fc.push(`@Get('/')@ApiResponse({ type: QueryVO_${name} })`);
  82. fc.push(` async query(@Query('filter') filter: QueryDTO_${name}, @Query('skip') skip: number,@Query('limit') limit: number){`);
  83. fc.push(` const data = await this.service.query(filter, { skip, limit });`);
  84. fc.push(` const total = await this.service.count(filter);`);
  85. fc.push(` return { data, total };`);
  86. fc.push(` }`);
  87. fc.push(`\n`);
  88. // fetch
  89. fc.push(`@Get('/:id')@ApiResponse({ type: FetchVO_${name} })`);
  90. fc.push(` async fetch(@Param('id') id: string) {`);
  91. fc.push(` const data = await this.service.fetch(id);`);
  92. fc.push(` const result = new FetchVO_${name}(data);`);
  93. fc.push(` return result;`);
  94. fc.push(` }`);
  95. fc.push(`\n`);
  96. // update
  97. fc.push(`@Post('/:id')@Validate()@ApiResponse({ type: UpdateVO_${name} })`);
  98. fc.push(` async update(@Param('id') id: string, @Body() body: UpdateDTO_${name}) {`);
  99. fc.push(` const result = await this.service.updateOne(id, body);`);
  100. fc.push(` return result;`);
  101. fc.push(` }`);
  102. fc.push(`\n`);
  103. // delete
  104. fc.push(`@Del('/:id')@Validate()`);
  105. fc.push(` async delete(@Param('id') id: string) {`);
  106. fc.push(` await this.service.delete(id);`);
  107. fc.push(` return 'ok';`);
  108. fc.push(` }`);
  109. // other bat deal
  110. fc.push(` async createMany(...args: any[]) {`);
  111. fc.push(` throw new Error('Method not implemented.');`);
  112. fc.push(` }`);
  113. fc.push(`\n`);
  114. fc.push(` async updateMany(...args: any[]) {`);
  115. fc.push(` throw new Error('Method not implemented.');`);
  116. fc.push(` }`);
  117. fc.push(`\n`);
  118. fc.push(` async deleteMany(...args: any[]) {`);
  119. fc.push(` throw new Error('Method not implemented.');`);
  120. fc.push(` }`);
  121. fc.push(`}`);
  122. fc.push(`\n`);
  123. return fc.join(`\n`);
  124. };
  125. /**
  126. *
  127. * @param {Object} col 字段
  128. * @param {boolean} needReq 是否启用必须,针对post设置
  129. * @returns
  130. */
  131. const getInterfaceColumn = (col, needReq = false) => {
  132. const fc = [];
  133. const { type, zh, title, required } = col;
  134. const modelType = getModelType(type, true);
  135. fc.push(` @ApiProperty({ description: '${title}' })`);
  136. if (needReq) {
  137. let ruleStr = '';
  138. if (required) ruleStr = `@Rule(RuleType['${modelType}']().required().error(new ServiceError('缺少${zh}',FrameworkErrorEnum.${needReq ? 'NEED_BODY' : 'NEED_QUERY'})))`;
  139. else ruleStr = `@Rule(RuleType['${modelType}']().empty())`;
  140. fc.push(ruleStr);
  141. }
  142. fc.push(` '${title}': ${modelType} = undefined;`);
  143. return fc;
  144. };
  145. const interfaceContext = (data) => {
  146. const fc = [];
  147. const { columns = [], name } = data;
  148. fc.push(`import { Rule, RuleType } from '@midwayjs/validate';`);
  149. fc.push(`import { ApiProperty } from '@midwayjs/swagger';`);
  150. fc.push(`import _ = require('lodash');`);
  151. fc.push(`import { FrameworkErrorEnum, SearchBase, ServiceError } from 'free-midway-component';`);
  152. fc.push(`export class FetchVO_${name} {`);
  153. fc.push(` constructor(data: object) {`);
  154. fc.push(` for (const key of Object.keys(this)) {`);
  155. fc.push(` this[key] = _.get(data, key);`);
  156. fc.push(` }`);
  157. fc.push(` }`);
  158. for (const col of columns) {
  159. const cfc = getInterfaceColumn(col);
  160. fc.push(...cfc);
  161. }
  162. fc.push(`}`);
  163. fc.push(`\n`);
  164. fc.push(`export class QueryDTO_${name} extends SearchBase {`);
  165. fc.push(` constructor() {`);
  166. fc.push(` const like_prop = [];`);
  167. fc.push(` super({ like_prop });`);
  168. fc.push(` }`);
  169. for (const col of columns.filter((f) => f.index)) {
  170. const cfc = getInterfaceColumn(col);
  171. fc.push(...cfc);
  172. }
  173. fc.push(`}`);
  174. fc.push(`\n`);
  175. fc.push(`export class QueryVO_${name} extends FetchVO_${name} {}`);
  176. fc.push(`\n`);
  177. fc.push(`export class CreateDTO_${name} {`);
  178. for (const col of columns) {
  179. const cfc = getInterfaceColumn(col, true);
  180. fc.push(...cfc);
  181. }
  182. fc.push(`}`);
  183. fc.push(`\n`);
  184. fc.push(`export class CreateVO_${name} extends FetchVO_${name} {}`);
  185. fc.push(`\n`);
  186. fc.push(`export class UpdateDTO_${name} extends CreateDTO_${name} {}`);
  187. fc.push(`\n`);
  188. fc.push(`export class UpdateVO_${name} extends FetchVO_${name} {}`);
  189. return fc.join('\n');
  190. };
  191. module.exports = (data) => {
  192. let result = `\n========== Model ==========\n`;
  193. result += ModelContext(data);
  194. result += `\n========== Service ==========\n`;
  195. result += ServiceContext(data);
  196. result += `\n========== Controller ==========\n`;
  197. result += ControllerContext(data);
  198. result += `\n========== interface ==========\n`;
  199. result += interfaceContext(data);
  200. return result;
  201. };