table-template.js 3.9 KB

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