table-template.js 4.0 KB

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