فهرست منبع

更新secret;导出json schema

lrf 1 سال پیش
والد
کامیت
cf0be3d617
5فایلهای تغییر یافته به همراه90 افزوده شده و 4 حذف شده
  1. 3 0
      app/controller/.table.js
  2. 59 0
      app/public/schema-template.js
  3. 17 2
      app/public/ts-template.js
  4. 10 2
      app/service/table.js
  5. 1 0
      app/z_router/table.js

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

@@ -44,4 +44,7 @@ module.exports = {
     requestBody: ['!ids'],
     operation: '导出',
   },
+  getSchema: {
+    params: ['!id'],
+  },
 };

+ 59 - 0
app/public/schema-template.js

@@ -0,0 +1,59 @@
+const { ObjectId } = require('mongoose').Types;
+const _ = require('lodash');
+const getModelTypeMock = (type) => {
+  let obj = {};
+  switch (type) {
+    case 'String':
+    case 'Boolean':
+      obj.type = _.lowerCase(type);
+      break;
+    case 'Number':
+      obj.type = _.lowerCase(type);
+      obj.mock = 0;
+      break;
+    case 'Money':
+      obj.type = 'integer';
+      obj.mock = 1.1;
+      break;
+    case 'Object':
+      obj.type = 'object';
+      obj.mock = {};
+      break;
+    case 'Array':
+      obj.type = 'array';
+      obj.mock = [];
+      break;
+    case 'ObjectId':
+      obj.type = 'string';
+      obj.mock = ObjectId().toString();
+      break;
+    case 'Secret':
+      obj.type = 'string';
+      obj.mock = 'secret';
+      break;
+    default:
+      break;
+  }
+  return obj;
+};
+
+module.exports = (tables) => {
+  const result = {};
+  for (const t of tables) {
+    const { name, columns = [] } = t;
+    const schema = { type: 'object' };
+    const properties = {};
+    for (const c of columns) {
+      const { zh, type: ot, title } = c;
+      const obj = {};
+      const { type, mock } = getModelTypeMock(ot);
+      obj['type'] = type;
+      obj['title'] = zh;
+      if (mock) obj['mock'] = { mock };
+      Object.assign(properties, { [title]: obj });
+    }
+    schema.properties = properties;
+    result[name] = schema;
+  }
+  return result;
+};

+ 17 - 2
app/public/ts-template.js

@@ -17,6 +17,9 @@ const ModelContext = (data) => {
   fc.push(`import { BaseModel } from 'free-midway-component';`);
   const has_money = columns.find((f) => f.type === 'Money' || f.type === 'money');
   if (has_money) fc.push(`import { Decimal128 } from 'mongoose';`);
+  // 添加secret需要isString函数
+  const has_secret = columns.find((f) => f.type === 'Secret' || f.type === 'secret');
+  if (has_secret) fc.push(`import { isString } from 'lodash';`);
   fc.push(`@modelOptions({`);
   fc.push(`  schemaOptions: { collection: '${table_name}' },`);
   fc.push(`})`);
@@ -27,8 +30,20 @@ const ModelContext = (data) => {
     const prop = _.pick(col, ['required', 'index', 'zh', 'ref', 'remark']);
     if (def) prop.default = def;
     const modelType = getModelType(type);
-    fc.push(`  @prop(${JSON.stringify(prop)})`);
-    fc.push(`  ${title}: ${modelType}`);
+    switch (modelType) {
+      case 'secret':
+        prop.select = false;
+        fc.push(`  // 手动删除set前的大括号,处理太麻烦了.就手动删除吧`);
+        fc.push(`  @prop(${JSON.stringify(prop)}, set: (val) => { if (isString(val)) { return { secret: val }; } return val; } })`);
+        fc.push(`  ${title}: string`);
+        break;
+    
+      default:
+        fc.push(`  @prop(${JSON.stringify(prop)})`);
+        fc.push(`  ${title}: ${modelType}`);
+        break;
+    }
+    
   }
 
   fc.push(`}`);

+ 10 - 2
app/service/table.js

@@ -2,6 +2,7 @@
 const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const template = require('../public/table-template');
 const TSTemplate = require('../public/ts-template');
+const SchemaTemplate = require('../public/schema-template');
 const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
 class TableService extends CrudService {
@@ -10,6 +11,13 @@ class TableService extends CrudService {
     this.model = this.ctx.model.Table;
     this.dirModel = this.ctx.model.Dir;
   }
+  // 导出json 模型
+  async getSchema({ id }) {
+    const list = await this.model.find({ project: id }, { name: 1, columns: 1 }).lean();
+    const allSchema = SchemaTemplate(list);
+    return allSchema;
+  }
+
   async query(query) {
     const project = _.get(query, 'project');
     let dirs = [];
@@ -130,8 +138,8 @@ class TableService extends CrudService {
           i.getPath = (type) => `${str}${type}${dirObj.path}/${_.lowerFirst(name)}.${type}`;
         }
       } else i.getPath = (type) => `../${type}/${_.lowerFirst(name)}.${type}`;
-        const d = TSTemplate(i);
-        data[i.name_zh ? i.name_zh : i.name] = d;
+      const d = TSTemplate(i);
+      data[i.name_zh ? i.name_zh : i.name] = d;
     }
     return data;
   }

+ 1 - 0
app/z_router/table.js

@@ -9,4 +9,5 @@ module.exports = (app) => {
   router.post(`targetExportTS`, `${prefix}/${target}/exportTS`, controller[target].toExportTS);
   router.resources(target, `${prefix}/${target}`, controller[target]); // index、create、show、destroy
   router.post(target, `${prefix}/${target}/:id`, controller[target].update);
+  router.post(target, `${prefix}/${target}/schema/:id`, controller[target].getSchema);
 };