lrf 3 éve
szülő
commit
67cb22cf56

+ 3 - 0
app/controller/.table.js

@@ -36,4 +36,7 @@ module.exports = {
       count: true,
     },
   },
+  toExport: {
+    requestBody: ['!ids'],
+  },
 };

+ 3 - 2
app/model/table.js

@@ -8,7 +8,7 @@ const column = new Schema(
     title: { type: String, required: true }, // 字段名,必须是英文
     type: { type: String, default: String }, // 字段类型,默认:String
     required: { type: Boolean, default: false }, // 是否必填,默认:否
-    maxLength: { type: Number }, // 最大长度限制
+    index: { type: Boolean, default: false },
     remark: { type: String },
   },
   {
@@ -18,7 +18,8 @@ const column = new Schema(
 
 // 字段表
 const table = {
-  name: { type: String, required: true },
+  name: { type: String, required: true }, // 表名
+  name_zh: { type: String, required: true }, // 表中文名
   project: { type: ObjectId, required: true }, // 项目
   columns: [ column ], // 字段列表
   remark: { type: String },

+ 8 - 0
app/public/options.js

@@ -9,6 +9,14 @@ module.exports = {
       label: '数字',
       value: 'Number',
     },
+    {
+      label: '对象',
+      value: 'Object',
+    },
+    {
+      label: '集合',
+      value: 'Array',
+    },
     {
       label: 'ObjectId',
       value: 'ObjectId',

+ 33 - 0
app/public/table-template.js

@@ -0,0 +1,33 @@
+'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});
+  };
+  `;
+
+};

+ 5 - 9
app/service/table.js

@@ -7,16 +7,12 @@ class TableService extends CrudService {
     this.model = this.ctx.model.Table;
   }
 
-  async query(condition) {
-    condition = this.turnFilter(this.turnDateRangeQuery(condition));
-    console.log(condition);
-    const res = await this.model.find(condition);
+  async toExport({ ids }) {
+    const res = await this.model.find({ _id: { $in: ids } });
     console.log(res);
-  }
-
-  async count(condition) {
-    console.log(condition);
-
+    const d1 = res[0];
+    console.log(JSON.stringify(d1, 1));
+    return JSON.stringify(d1, 1);
   }
 }
 

+ 1 - 0
app/z_router/table.js

@@ -4,6 +4,7 @@ module.exports = app => {
   const { router, controller, config } = app;
   const target = 'table';
   const prefix = config.routePrefix;
+  router.post(target, `${prefix}/${target}/export`, controller[target].toExport);
   router.resources(target, `${prefix}/${target}`, controller[target]); // index、create、show、destroy
   router.post(target, `${prefix}/${target}/:id`, controller[target].update);
 };

+ 1 - 1
package.json

@@ -11,7 +11,7 @@
     "egg-scripts": "^2.11.0",
     "lodash": "^4.17.21",
     "moment": "^2.29.1",
-    "naf-framework-mongoose-free": "^0.0.2"
+    "naf-framework-mongoose-free": "^0.0.8"
   },
   "devDependencies": {
     "autod": "^3.0.1",