table-template.js 2.9 KB

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