ts-template.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. // 添加secret需要isString函数
  21. const has_secret = columns.find((f) => f.type === 'Secret' || f.type === 'secret');
  22. if (has_secret) fc.push(`import { isString } from 'lodash';`);
  23. fc.push(`@modelOptions({`);
  24. fc.push(` schemaOptions: { collection: '${table_name}' },`);
  25. fc.push(`})`);
  26. fc.push(`export class ${prefix} extends BaseModel {`);
  27. // 处理字段
  28. for (const col of columns) {
  29. const { type, title, def } = col;
  30. const prop = _.pick(col, ['required', 'index', 'zh', 'ref', 'remark', 'esType']);
  31. if (def) prop.default = def;
  32. const modelType = getModelType(type);
  33. switch (modelType) {
  34. case 'secret':
  35. prop.select = false;
  36. // 手动删除引号.变成方法.这里处理不了
  37. prop.set = `(val) => { if (isString(val)) { return { secret: val }; } return val; }`;
  38. fc.push(` // 手动删除set前的大括号,处理太麻烦了.就手动删除吧`);
  39. fc.push(` @prop(${JSON.stringify(prop)})`);
  40. fc.push(` ${title}: object`);
  41. break;
  42. default:
  43. fc.push(` @prop(${JSON.stringify(prop)})`);
  44. fc.push(` ${title}: ${modelType}`);
  45. break;
  46. }
  47. }
  48. fc.push(`}`);
  49. // 最后换行转为字符串
  50. const fileContext = fc.join('\n');
  51. return fileContext;
  52. };
  53. const ServiceContext = (data) => {
  54. const fc = [];
  55. const { name: table_name, getPath } = data;
  56. const prefix = _.upperFirst(table_name);
  57. const entityPath = getPath('entity');
  58. fc.push(`import { Provide } from '@midwayjs/decorator';`);
  59. fc.push(`import { InjectEntityModel } from '@midwayjs/typegoose';`);
  60. fc.push(`import { ReturnModelType } from '@typegoose/typegoose';`);
  61. fc.push(`import { BaseService } from 'free-midway-component';`);
  62. fc.push(`import { ${prefix} } from '${entityPath}';`);
  63. fc.push(`type modelType = ReturnModelType<typeof ${prefix}>;`);
  64. fc.push(`@Provide()`);
  65. fc.push(`export class ${prefix}Service extends BaseService<modelType> {`);
  66. fc.push(` @InjectEntityModel(${prefix})`);
  67. fc.push(` model: modelType;`);
  68. fc.push(`}`);
  69. return fc.join('\n');
  70. };
  71. const ControllerContext = (data) => {
  72. const fc = [];
  73. const { name, name_zh, getPath } = data;
  74. const prefix = _.upperFirst(name);
  75. const servicePath = getPath('service');
  76. const interfacePath = getPath('interface');
  77. fc.push(`import { Body, Controller, Del, Get, Inject, Param, Post, Query } from '@midwayjs/decorator';`);
  78. fc.push(`import { BaseController } from 'free-midway-component';`);
  79. fc.push(`import { ${prefix}Service } from '${servicePath}';`);
  80. fc.push(`import { CDTO_${name}, CVO_${name}, FVO_${name},QDTO_${name}, QVO_${name}, UDTO_${name}, UVAO_${name} } from '${interfacePath}';`);
  81. fc.push(`import { ApiResponse, ApiTags, ApiQuery } from '@midwayjs/swagger';`);
  82. fc.push(`import { Validate } from '@midwayjs/validate';`);
  83. fc.push(`@ApiTags(['${name_zh}'])`);
  84. fc.push(`@Controller('/${name}')`);
  85. fc.push(`export class ${prefix}Controller extends BaseController {`);
  86. fc.push(` @Inject()`);
  87. fc.push(` service: ${prefix}Service;`);
  88. fc.push('\n');
  89. // create
  90. fc.push(`@Post('/') @Validate() @ApiResponse({ type: CVO_${name} })`);
  91. fc.push(` async create(@Body() data: CDTO_${name}) {`);
  92. fc.push(` const dbData = await this.service.create(data);`);
  93. fc.push(` const result = new CVO_${name}(dbData);`);
  94. fc.push(` return result;`);
  95. fc.push(` }`);
  96. // query
  97. fc.push(`@Get('/')@ApiQuery({name:'query'})@ApiResponse({ type: QVO_${name} })`);
  98. fc.push(` async query(@Query() filter:QDTO_${name}, @Query('skip') skip: number,@Query('limit') limit: number){`);
  99. fc.push(` const list = await this.service.query(filter, { skip, limit });`);
  100. fc.push(` const data = [];`);
  101. fc.push(` for (const i of list) {`);
  102. fc.push(` const newData = new QVO_${name}(i);`);
  103. fc.push(` data.push(newData);`);
  104. fc.push(` }`);
  105. fc.push(` const total = await this.service.count(filter);`);
  106. fc.push(` return { data, total };`);
  107. fc.push(` }`);
  108. fc.push(`\n`);
  109. // fetch
  110. fc.push(`@Get('/:id')@ApiResponse({ type: FVO_${name} })`);
  111. fc.push(` async fetch(@Param('id') id: string) {`);
  112. fc.push(` const data = await this.service.fetch(id);`);
  113. fc.push(` const result = new FVO_${name}(data);`);
  114. fc.push(` return result;`);
  115. fc.push(` }`);
  116. fc.push(`\n`);
  117. // update
  118. fc.push(`@Post('/:id')@Validate()@ApiResponse({ type: UVAO_${name} })`);
  119. fc.push(` async update(@Param('id') id: string, @Body() body: UDTO_${name}) {`);
  120. fc.push(` const result = await this.service.updateOne(id, body);`);
  121. fc.push(` return result;`);
  122. fc.push(` }`);
  123. fc.push(`\n`);
  124. // delete
  125. fc.push(`@Del('/:id')@Validate()`);
  126. fc.push(` async delete(@Param('id') id: string) {`);
  127. fc.push(` await this.service.delete(id);`);
  128. fc.push(` return 'ok';`);
  129. fc.push(` }`);
  130. // other bat deal
  131. fc.push(` async createMany(...args: any[]) {`);
  132. fc.push(` throw new Error('Method not implemented.');`);
  133. fc.push(` }`);
  134. fc.push(`\n`);
  135. fc.push(` async updateMany(...args: any[]) {`);
  136. fc.push(` throw new Error('Method not implemented.');`);
  137. fc.push(` }`);
  138. fc.push(`\n`);
  139. fc.push(` async deleteMany(...args: any[]) {`);
  140. fc.push(` throw new Error('Method not implemented.');`);
  141. fc.push(` }`);
  142. fc.push(`}`);
  143. fc.push(`\n`);
  144. return fc.join(`\n`);
  145. };
  146. /**
  147. *
  148. * @param {Object} col 字段
  149. * @param {boolean} needReq 是否启用必须,针对post设置
  150. * @returns
  151. */
  152. const getInterfaceColumn = (col, needReq = false) => {
  153. const fc = [];
  154. const { type, zh, title, required } = col;
  155. const modelType = getModelType(type, true);
  156. fc.push(` @ApiProperty({ description: '${zh}' })`);
  157. if (needReq) {
  158. let ruleStr = '';
  159. let rt = '';
  160. if (type === 'Money' || type === 'money') rt = 'number';
  161. else rt = _.lowerFirst(type);
  162. if (required) ruleStr = `@Rule(RuleType['${rt}']().required().error(new ServiceError('缺少${zh}',FrameworkErrorEnum.${needReq ? 'NEED_BODY' : 'NEED_QUERY'})))`;
  163. else ruleStr = `@Rule(RuleType['${rt}']().empty(''))`;
  164. fc.push(ruleStr);
  165. }
  166. fc.push(` '${title}': ${modelType} = undefined;`);
  167. return fc;
  168. };
  169. const InterfaceContext = (data) => {
  170. const fc = [];
  171. const { columns = [], name } = data;
  172. const have_required = columns.find((f) => f.required);
  173. fc.push(`import { Rule, RuleType } from '@midwayjs/validate';`);
  174. fc.push(`import { ApiProperty } from '@midwayjs/swagger';`);
  175. if (have_required) fc.push(`import { FrameworkErrorEnum, SearchBase, ServiceError } from 'free-midway-component';`);
  176. else fc.push(`import { SearchBase } from 'free-midway-component';`);
  177. fc.push(`import get = require('lodash/get');`);
  178. fc.push(`const dealVO = (cla, data) => {`);
  179. fc.push(` for (const key in cla) {`);
  180. fc.push(` const val = get(data, key);`);
  181. fc.push(` if (val || val === 0) cla[key] = val;`);
  182. fc.push(` }`);
  183. fc.push(`};`);
  184. fc.push(`export class FVO_${name} {`);
  185. fc.push(` constructor(data: object) {`);
  186. fc.push(` dealVO(this, data);`);
  187. fc.push(` }`);
  188. fc.push(` @ApiProperty({ description: '数据id' })`);
  189. fc.push(` _id: string = undefined;`);
  190. for (const col of columns) {
  191. const cfc = getInterfaceColumn(col);
  192. fc.push(...cfc);
  193. }
  194. fc.push(`}`);
  195. fc.push(`\n`);
  196. const indexs = columns.filter((f) => f.index);
  197. const props = indexs.map((i) => `'${i.title}'`).join(', ');
  198. fc.push(`export class QDTO_${name} extends SearchBase {`);
  199. fc.push(` constructor() {`);
  200. fc.push(` const like_prop = [];`);
  201. fc.push(` const props = [${props}];`);
  202. fc.push(` const mapping = [];`);
  203. fc.push(` super({ like_prop, props, mapping });`);
  204. fc.push(` }`);
  205. for (const col of indexs) {
  206. const cfc = getInterfaceColumn(col);
  207. fc.push(...cfc);
  208. }
  209. fc.push(`}`);
  210. fc.push(`\n`);
  211. fc.push(`export class QVO_${name} extends FVO_${name} {`);
  212. fc.push(` constructor(data: object) {`);
  213. fc.push(` super(data);`);
  214. fc.push(` dealVO(this, data);`);
  215. fc.push(` }`);
  216. fc.push(`}`);
  217. fc.push(`\n`);
  218. fc.push(`export class CDTO_${name} {`);
  219. for (const col of columns) {
  220. const cfc = getInterfaceColumn(col, true);
  221. fc.push(...cfc);
  222. }
  223. fc.push(`}`);
  224. fc.push(`\n`);
  225. fc.push(`export class CVO_${name} extends FVO_${name} {`);
  226. fc.push(` constructor(data: object) {`);
  227. fc.push(` super(data);`);
  228. fc.push(` dealVO(this, data);`);
  229. fc.push(` }`);
  230. fc.push(`}`);
  231. fc.push(`\n`);
  232. fc.push(`export class UDTO_${name} extends CDTO_${name} {
  233. @ApiProperty({ description: '数据id' })
  234. @Rule(RuleType['string']().empty(''))
  235. _id: string = undefined;
  236. }`);
  237. fc.push(`\n`);
  238. fc.push(`export class UVAO_${name} extends FVO_${name} {`);
  239. fc.push(` constructor(data: object) {`);
  240. fc.push(` super(data);`);
  241. fc.push(` dealVO(this, data);`);
  242. fc.push(` }`);
  243. fc.push(`}`);
  244. return fc.join('\n');
  245. };
  246. module.exports = (data) => {
  247. // let result = `\n========== Model ==========\n`;
  248. // result += ModelContext(data);
  249. // result += `\n========== Service ==========\n`;
  250. // result += ServiceContext(data);
  251. // result += `\n========== Controller ==========\n`;
  252. // result += ControllerContext(data);
  253. // result += `\n========== interface ==========\n`;
  254. // result += InterfaceContext(data);
  255. const result = {};
  256. result.m = ModelContext(data);
  257. result.s = ServiceContext(data);
  258. result.c = ControllerContext(data);
  259. result.i = InterfaceContext(data);
  260. result.n = data.name;
  261. return result;
  262. };