ts-template.js 8.1 KB

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