|
@@ -1,8 +1,36 @@
|
|
'use strict';
|
|
'use strict';
|
|
const _ = require('lodash');
|
|
const _ = require('lodash');
|
|
|
|
+const { columnType } = require('./options');
|
|
|
|
|
|
module.exports = (data) => {
|
|
module.exports = (data) => {
|
|
- const { name, name_zh, columns } = data;
|
|
|
|
|
|
+ let { name, name_zh, columns } = data;
|
|
|
|
+
|
|
|
|
+ // 将插件和正常字段分开
|
|
|
|
+ let pluginColumns = columns.filter((f) => {
|
|
|
|
+ const r = columnType.find((cf) => cf.value === f.type && cf.plugins);
|
|
|
|
+ if (r) return true;
|
|
|
|
+ return false;
|
|
|
|
+ });
|
|
|
|
+ console.log(pluginColumns);
|
|
|
|
+ let pluginHead = '';
|
|
|
|
+ let pluginArea = '';
|
|
|
|
+ for (const p of pluginColumns) {
|
|
|
|
+ const { title, type, required = false, remark, index, def, zh, ref, getProp } = p;
|
|
|
|
+ const params = _.omit(p, ['type']);
|
|
|
|
+ const r = columnType.find((cf) => cf.value === type);
|
|
|
|
+ if (!r) continue;
|
|
|
|
+ const { path } = r;
|
|
|
|
+ if (path) {
|
|
|
|
+ pluginHead += `const ${type}Plugin = require('${path}');\n`;
|
|
|
|
+ pluginArea += `schema.plugin(${type}Plugin(${params}));\n`;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ columns = columns.filter((f) => {
|
|
|
|
+ const r = columnType.find((cf) => cf.value === f.type && cf.plugins);
|
|
|
|
+ if (r) return false;
|
|
|
|
+ return true;
|
|
|
|
+ });
|
|
let columnStr = `const ${name} = { \n`;
|
|
let columnStr = `const ${name} = { \n`;
|
|
let indexStr = '';
|
|
let indexStr = '';
|
|
let has_ObjectId = false;
|
|
let has_ObjectId = false;
|
|
@@ -47,6 +75,7 @@ const Schema = require('mongoose').Schema;
|
|
const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
|
|
const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
|
|
${has_ObjectId ? "const { ObjectId } = require('mongoose').Types;" : ''}
|
|
${has_ObjectId ? "const { ObjectId } = require('mongoose').Types;" : ''}
|
|
${has_Secret ? "const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');" : ''}
|
|
${has_Secret ? "const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');" : ''}
|
|
|
|
+${pluginHead}
|
|
// ${name_zh || ''}
|
|
// ${name_zh || ''}
|
|
${columnStr}
|
|
${columnStr}
|
|
const schema = new Schema(${name}, { toJSON: { virtuals: true } });
|
|
const schema = new Schema(${name}, { toJSON: { virtuals: true } });
|
|
@@ -54,6 +83,7 @@ schema.index({ id: 1 });
|
|
schema.index({ 'meta.createdAt': 1 });
|
|
schema.index({ 'meta.createdAt': 1 });
|
|
${indexStr}
|
|
${indexStr}
|
|
schema.plugin(metaPlugin);
|
|
schema.plugin(metaPlugin);
|
|
|
|
+${pluginArea}
|
|
module.exports = app => {
|
|
module.exports = app => {
|
|
const { mongoose } = app;
|
|
const { mongoose } = app;
|
|
return mongoose.model('${modelName}', schema, '${name}');
|
|
return mongoose.model('${modelName}', schema, '${name}');
|