Browse Source

导出表整理数据

lrf 3 năm trước cách đây
mục cha
commit
0f57733c58
4 tập tin đã thay đổi với 41 bổ sung29 xóa
  1. 3 2
      app/controller/.table.js
  2. 4 0
      app/public/options.js
  3. 27 22
      app/public/table-template.js
  4. 7 5
      app/service/table.js

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

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['!name', '!project', 'columns', 'remark'],
+    requestBody: ['!name_zh', '!name', '!project', 'columns', 'remark'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['!name', '!project', 'columns', 'remark'],
+    requestBody: ['!name_zh', '!name', '!project', 'columns', 'remark'],
   },
   show: {
     parameters: {
@@ -19,6 +19,7 @@ module.exports = {
   index: {
     parameters: {
       query: {
+        name_zh: 'name_zh',
         name: '%name%',
         project: '%project%',
         'meta.createdAt@start': 'meta.createdAt@start',

+ 4 - 0
app/public/options.js

@@ -21,6 +21,10 @@ module.exports = {
       label: 'ObjectId',
       value: 'ObjectId',
     },
+    {
+      label: 'Secret',
+      value: 'Secret',
+    },
   ],
   required: [
     {

+ 27 - 22
app/public/table-template.js

@@ -3,31 +3,36 @@ 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`;
+  let columnStr = `const ${name} = { \n`;
+  let indexStr = '';
+  let has_ObjectId = false;
+  let has_Secret = false;
+  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`;
     columnStr += str;
+    if (index) indexStr += `schema.index({ '${title}': 1 });\n`;
+    if (!has_ObjectId && type === 'ObjectId') has_ObjectId = true;
+    else if (!has_Secret && type === 'Secret') has_Secret = true;
   }
-
+  columnStr += '}';
   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});
-  };
+  return `'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+${has_ObjectId ? "const { ObjectId } = require('mongoose').Types;" : ''}
+${has_Secret ? "const { Secret } = require('naf-framework-mongoose-free/lib/model/schema');" : ''}
+// ${name_zh || ''}
+${columnStr}
+const schema = new Schema(${name}, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ 'meta.createdAt': 1 });
+${indexStr}
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('${modelName}', schema, ${name});
+};
   `;
-
 };

+ 7 - 5
app/service/table.js

@@ -1,6 +1,6 @@
 'use strict';
 const { CrudService } = require('naf-framework-mongoose-free/lib/service');
-
+const template = require('../public/table-template');
 class TableService extends CrudService {
   constructor(ctx) {
     super(ctx, 'table');
@@ -9,10 +9,12 @@ class TableService extends CrudService {
 
   async toExport({ ids }) {
     const res = await this.model.find({ _id: { $in: ids } });
-    console.log(res);
-    const d1 = res[0];
-    console.log(JSON.stringify(d1, 1));
-    return JSON.stringify(d1, 1);
+    const data = {};
+    for (const i of res) {
+      const d = template(i);
+      data[i.name_zh ? i.name_zh : i.name] = d;
+    }
+    return data;
   }
 }