table-template.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { columnType } = require('./options');
  4. module.exports = (data) => {
  5. let { name, name_zh, columns } = data;
  6. const configArr = [];
  7. const updateArr = [];
  8. // 将插件和正常字段分开
  9. let pluginColumns = columns.filter((f) => {
  10. const r = columnType.find((cf) => cf.value === f.type && cf.plugins);
  11. if (r) return true;
  12. return false;
  13. });
  14. let pluginHead = '';
  15. let pluginArea = '';
  16. for (const p of pluginColumns) {
  17. const { title, type, required = false, remark, index, def, zh, ref, getProp } = p;
  18. const params = _.pick(p, ['zh', 'required', 'def']);
  19. params.key = title;
  20. const r = columnType.find((cf) => cf.value === type);
  21. if (!r) continue;
  22. const { path } = r;
  23. if (path) {
  24. const pluginStr = `const ${type}Plugin = require('${path}');\n`;
  25. if (pluginHead.indexOf(pluginStr) <= -1) pluginHead += pluginStr;
  26. pluginArea += `schema.plugin(${type}Plugin(${JSON.stringify(params)}));\n`;
  27. configArr.push(`'${title}'`);
  28. updateArr.push(`'${title}'`);
  29. }
  30. }
  31. columns = columns.filter((f) => {
  32. const r = columnType.find((cf) => cf.value === f.type && cf.plugins);
  33. if (r) return false;
  34. return true;
  35. });
  36. let columnStr = `const ${name} = { \n`;
  37. let indexStr = '';
  38. let has_ObjectId = false;
  39. let has_Secret = false;
  40. let configSearchStr = '';
  41. for (let i = 0; i < columns.length; i++) {
  42. const { title, type, required = false, remark, index, def, zh, ref, getProp } = columns[i];
  43. let refStr;
  44. if (ref) {
  45. const r = columns.find((f) => f.title === ref);
  46. refStr = `${r ? 'refPath' : 'ref'}: '${ref}'`;
  47. if (getProp) {
  48. const propList = getProp.split(';');
  49. let propStr = '[';
  50. for (const p of propList) {
  51. if (p !== _.head(propList)) propStr = `${propStr}, `;
  52. propStr = `${propStr}'${p}'`;
  53. }
  54. propStr += ']';
  55. refStr += `, getProp: ${propStr}`;
  56. }
  57. }
  58. const str = ` ${title}: { type: ${type || 'String'}, required: ${required} ${type === 'Secret' ? ', select: false' : ''} ${def ? `, default: '${def}'` : ''}${
  59. zh ? `, zh: '${zh}'` : ''
  60. }${refStr ? `,${refStr}` : ''} }, // ${remark || ''} \n`;
  61. columnStr += str;
  62. configArr.push(`'${required ? '!' : ''}${title}'`);
  63. updateArr.push(`'${title}'`);
  64. if (index) {
  65. indexStr += `schema.index({ '${title}': 1 });\n`;
  66. configSearchStr += `'${title}': '${title}' ,\n`;
  67. }
  68. if (!has_ObjectId && type === 'ObjectId') has_ObjectId = true;
  69. else if (!has_Secret && type === 'Secret') has_Secret = true;
  70. }
  71. columnStr += '}';
  72. const nameList = name.split('_');
  73. const modelName = nameList.map((i) => _.upperFirst(i)).join('_');
  74. return `'use strict';
  75. const Schema = require('mongoose').Schema;
  76. const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
  77. ${has_ObjectId ? "const { ObjectId } = require('mongoose').Types;" : ''}
  78. ${has_Secret ? "const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');" : ''}
  79. ${pluginHead}
  80. // ${name_zh || ''}
  81. ${columnStr}
  82. const schema = new Schema(${name}, { toJSON: { getters: true, virtuals: true } });
  83. schema.index({ id: 1 });
  84. schema.index({ 'meta.createdAt': 1 });
  85. ${indexStr}
  86. schema.plugin(metaPlugin);
  87. ${pluginArea}
  88. module.exports = app => {
  89. const { mongoose } = app;
  90. return mongoose.model('${modelName}', schema, '${name}');
  91. };
  92. module.exports = {
  93. create: {
  94. requestBody: [${configArr.join(',')}],
  95. },
  96. destroy: {
  97. params: ["!id"],
  98. service: "delete",
  99. },
  100. update: {
  101. params: ["!id"],
  102. requestBody: [${updateArr.join(',')}],
  103. },
  104. show: {
  105. parameters: {
  106. params: ["!id"],
  107. },
  108. service: "fetch",
  109. },
  110. index: {
  111. parameters: {
  112. query: {
  113. "meta.createdAt@start": "meta.createdAt@start",
  114. "meta.createdAt@end": "meta.createdAt@end",
  115. ${configSearchStr}
  116. },
  117. // options: {
  118. // "meta.state": 0 // 默认条件
  119. // },
  120. },
  121. service: "query",
  122. options: {
  123. query: ["skip", "limit"],
  124. sort: ["meta.createdAt"],
  125. desc: true,
  126. count: true,
  127. },
  128. },
  129. };
  130. `;
  131. };