123456789101112131415161718192021222324252627282930313233 |
- 'use strict';
- const _ = require('lodash');
- module.exports = data => {
- const { name, name_zh, columns } = data;
- let columnStr = '';
- for (const column of columns) {
- const { title, type, required = false, remark } = column;
- const str = ` ${title}: { type: ${type}, required: ${required} }, // ${remark} \n`;
- columnStr += str;
- }
- const nameList = name.split('_');
- const modelName = nameList.map(i => _.capitalize(i)).join('_');
- return `
- 'use strict';
- const Schema = require('mongoose').Schema;
- const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
- // ${name_zh}
- const ${name} = {
- ${columnStr}
- }
- const schema = new Schema(${name}, { toJSON: { virtuals: true } });
- schema.index({ id: 1 });
- schema.index({ 'meta.createdAt': 1 });
- schema.plugin(metaPlugin);
- module.exports = app => {
- const { mongoose } = app;
- return mongoose.model('${modelName}', schema, ${name});
- };
- `;
- };
|