ソースを参照

修改模板内容,添加默认值

lrf 3 年 前
コミット
35042fcf64
3 ファイル変更10 行追加6 行削除
  1. 4 3
      app/model/table.js
  2. 1 0
      app/public/options.js
  3. 5 3
      app/public/table-template.js

+ 4 - 3
app/model/table.js

@@ -6,9 +6,10 @@ const { ObjectId } = require('mongoose').Types;
 const column = new Schema(
   {
     title: { type: String, required: true }, // 字段名,必须是英文
-    type: { type: String, default: String }, // 字段类型,默认:String
+    type: { type: String, default: 'String' }, // 字段类型,默认:String
     required: { type: Boolean, default: false }, // 是否必填,默认:否
     index: { type: Boolean, default: false },
+    def: { type: String }, // 默认值
     remark: { type: String },
   },
   {
@@ -21,7 +22,7 @@ const table = {
   name: { type: String, required: true }, // 表名
   name_zh: { type: String, required: true }, // 表中文名
   project: { type: ObjectId, required: true }, // 项目
-  columns: [ column ], // 字段列表
+  columns: [column], // 字段列表
   remark: { type: String },
 };
 const schema = new Schema(table, { toJSON: { virtuals: true } });
@@ -30,7 +31,7 @@ schema.index({ name: 1 });
 schema.index({ project: 1 });
 schema.index({ 'meta.createdAt': 1 });
 schema.plugin(metaPlugin);
-module.exports = app => {
+module.exports = (app) => {
   const { mongoose } = app;
   return mongoose.model('Table', schema, 'table');
 };

+ 1 - 0
app/public/options.js

@@ -4,6 +4,7 @@ module.exports = {
     {
       label: '字符串',
       value: 'String',
+      default: true,
     },
     {
       label: '数字',

+ 5 - 3
app/public/table-template.js

@@ -10,8 +10,10 @@ module.exports = data => {
   const configArr = [];
   let configSearchStr = '';
   for (let i = 0; i < columns.length; i++) {
-    const { title, type, required = false, remark, index } = columns[i];
-    const str = `  ${title}: { type: ${type || 'String'}, required: ${required} ${type === 'Secret' ? ', select: false' : ''} }, // ${remark || ''} \n`;
+    const { title, type, required = false, remark, index, def } = columns[i];
+    const str = `  ${title}: { type: ${type || 'String'}, required: ${required} ${type === 'Secret' ? ', select: false' : ''} ${def ? `, default: '${def}'` : ''} }, // ${
+      remark || ''
+    } \n`;
     columnStr += str;
     configArr.push(`'${required ? '!' : ''}${title}'`);
     if (index) {
@@ -38,7 +40,7 @@ ${indexStr}
 schema.plugin(metaPlugin);
 module.exports = app => {
   const { mongoose } = app;
-  return mongoose.model('${modelName}', schema, ${name});
+  return mongoose.model('${modelName}', schema, '${name}');
 };
   
 module.exports = {