table-template.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. const _ = require('lodash');
  3. module.exports = (data) => {
  4. const { name, name_zh, columns } = data;
  5. let columnStr = `const ${name} = { \n`;
  6. let indexStr = '';
  7. let has_ObjectId = false;
  8. let has_Secret = false;
  9. const configArr = [];
  10. const updateArr = [];
  11. let configSearchStr = '';
  12. for (let i = 0; i < columns.length; i++) {
  13. const { title, type, required = false, remark, index, def, zh, ref, getProp } = columns[i];
  14. let refStr;
  15. if (ref) {
  16. refStr = ` ref: '${_.capitalize(ref)}'`;
  17. if (getProp) {
  18. const propList = getProp.split(';');
  19. let propStr = '[';
  20. for (const p of propList) {
  21. if (p !== _.head(propList)) propStr = `${propStr}, `;
  22. propStr = `${propStr}'${p}'`;
  23. }
  24. propStr += ']';
  25. refStr += `, getProp: ${propStr}`;
  26. }
  27. }
  28. const str = ` ${title}: { type: ${type || 'String'}, required: ${required} ${type === 'Secret' ? ', select: false' : ''} ${def ? `, default: '${def}'` : ''}${
  29. zh ? `, zh: '${zh}'` : ''
  30. }${refStr ? `,${refStr}` : ''} }, // ${remark || ''} \n`;
  31. columnStr += str;
  32. configArr.push(`'${required ? '!' : ''}${title}'`);
  33. updateArr.push(`'${title}'`);
  34. if (index) {
  35. indexStr += `schema.index({ '${title}': 1 });\n`;
  36. configSearchStr += `'${title}': '${title}' ,\n`;
  37. }
  38. if (!has_ObjectId && type === 'ObjectId') has_ObjectId = true;
  39. else if (!has_Secret && type === 'Secret') has_Secret = true;
  40. }
  41. columnStr += '}';
  42. const nameList = name.split('_');
  43. const modelName = nameList.map((i) => _.capitalize(i)).join('_');
  44. return `'use strict';
  45. const Schema = require('mongoose').Schema;
  46. const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
  47. ${has_ObjectId ? "const { ObjectId } = require('mongoose').Types;" : ''}
  48. ${has_Secret ? "const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');" : ''}
  49. // ${name_zh || ''}
  50. ${columnStr}
  51. const schema = new Schema(${name}, { toJSON: { virtuals: true } });
  52. schema.index({ id: 1 });
  53. schema.index({ 'meta.createdAt': 1 });
  54. ${indexStr}
  55. schema.plugin(metaPlugin);
  56. module.exports = app => {
  57. const { mongoose } = app;
  58. return mongoose.model('${modelName}', schema, '${name}');
  59. };
  60. module.exports = {
  61. create: {
  62. requestBody: [${configArr.join(',')}],
  63. },
  64. destroy: {
  65. params: ["!id"],
  66. service: "delete",
  67. },
  68. update: {
  69. params: ["!id"],
  70. requestBody: [${updateArr.join(',')}],
  71. },
  72. show: {
  73. parameters: {
  74. params: ["!id"],
  75. },
  76. service: "fetch",
  77. },
  78. index: {
  79. parameters: {
  80. query: {
  81. "meta.createdAt@start": "meta.createdAt@start",
  82. "meta.createdAt@end": "meta.createdAt@end",
  83. ${configSearchStr}
  84. },
  85. // options: {
  86. // "meta.state": 0 // 默认条件
  87. // },
  88. },
  89. service: "query",
  90. options: {
  91. query: ["skip", "limit"],
  92. sort: ["meta.createdAt"],
  93. desc: true,
  94. count: true,
  95. },
  96. },
  97. };
  98. `;
  99. };