table-template.js 960 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. const _ = require('lodash');
  3. module.exports = data => {
  4. const { name, name_zh, columns } = data;
  5. let columnStr = '';
  6. for (const column of columns) {
  7. const { title, type, required = false, remark } = column;
  8. const str = ` ${title}: { type: ${type}, required: ${required} }, // ${remark} \n`;
  9. columnStr += str;
  10. }
  11. const nameList = name.split('_');
  12. const modelName = nameList.map(i => _.capitalize(i)).join('_');
  13. return `
  14. 'use strict';
  15. const Schema = require('mongoose').Schema;
  16. const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
  17. // ${name_zh}
  18. const ${name} = {
  19. ${columnStr}
  20. }
  21. const schema = new Schema(${name}, { toJSON: { virtuals: true } });
  22. schema.index({ id: 1 });
  23. schema.index({ 'meta.createdAt': 1 });
  24. schema.plugin(metaPlugin);
  25. module.exports = app => {
  26. const { mongoose } = app;
  27. return mongoose.model('${modelName}', schema, ${name});
  28. };
  29. `;
  30. };