lrf 2 年之前
父節點
當前提交
33be2e5a79
共有 2 個文件被更改,包括 36 次插入28 次删除
  1. 2 0
      README.zh_CN.md
  2. 34 28
      lib/service/crud-service.js

+ 2 - 0
README.zh_CN.md

@@ -21,6 +21,8 @@ $~~~~~~$|`__`user.js
 在某model的字段关联user时,需要写成: `key: {..., ref: 'User.User', ...}`
 若要获取该关联的属性, 添加 `getProp` 属性 : `key: {..., ref: 'User.User', getProp: ['${prop}','${key}.${prop}'] ...}`
 
+在config设置中有 `config.queryRefs` 默认不启用 ref关联查询.需要设置该变量为`true`才可自动关联
+
 ps:自己写的话,随便
 
 ---

+ 34 - 28
lib/service/crud-service.js

@@ -128,7 +128,7 @@ class CrudService extends NafService {
     if (sort && _.isString(sort)) {
       sort = { [sort]: desc ? -1 : 1 };
     } else if (sort && _.isArray(sort)) {
-      sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
+      sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
     }
     let res = await this.model.findOne(filter, projection).exec();
     res = await this.afterFetch(filter, res);
@@ -147,40 +147,46 @@ class CrudService extends NafService {
     if (sort && _.isString(sort)) {
       sort = { [sort]: desc ? -1 : 1 };
     } else if (sort && _.isArray(sort)) {
-      sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
+      sort = sort.map((f) => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
     }
     let condition = _.cloneDeep(filter);
     condition = await this.beforeQuery(condition);
     condition = this.dealFilter(condition);
     // 过滤出ref字段
     const { refMods, populate } = this.getRefMods();
-    // 带ref查询
-    let rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).populate(populate).exec();
-    rs = JSON.parse(JSON.stringify(rs));
-    // 整理ref数据
-    rs = rs.map(i => {
-      for (const obj of refMods) {
-        const { col, prop, type } = obj;
-        if (!prop) continue;
-        if (_.isArray(prop)) {
-          for (const p of prop) {
-            if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
-            if (type === 'Array') {
-              const list = [];
-              const oList = _.get(i, `${col}`);
-              for (const d of oList) {
-                const obj = { _id: d._id };
-                obj[p] = _.get(d, p);
-                list.push(obj);
+    const queryRefs = _.get(this.app, 'config.queryRefs');
+    let rs;
+    if (queryRefs) {
+      // 带ref查询
+      rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).populate(populate).exec();
+      rs = JSON.parse(JSON.stringify(rs));
+      // 整理ref数据
+      rs = rs.map((i) => {
+        for (const obj of refMods) {
+          const { col, prop, type } = obj;
+          if (!prop) continue;
+          if (_.isArray(prop)) {
+            for (const p of prop) {
+              if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
+              if (type === 'Array') {
+                const list = [];
+                const oList = _.get(i, `${col}`);
+                for (const d of oList) {
+                  const obj = { _id: d._id };
+                  obj[p] = _.get(d, p);
+                  list.push(obj);
+                }
+                i[`${col}_${p}`] = list;
               }
-              i[`${col}_${p}`] = list;
             }
+            i[col] = _.get(i, `${col}._id`);
           }
-          i[col] = _.get(i, `${col}._id`);
         }
-      }
-      return i;
-    });
+        return i;
+      });
+    } else {
+      rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).exec();
+    }
     rs = await this.afterQuery(filter, rs);
     return rs;
   }
@@ -208,7 +214,7 @@ class CrudService extends NafService {
   turnFilter(filter) {
     const str = /^%\S*%$/;
     // $是mongodb固定条件,不用处理;大多为手写特殊处理过的条件
-    let keys = Object.keys(filter).filter(f => !f.includes('$'));
+    let keys = Object.keys(filter).filter((f) => !f.includes('$'));
     for (const key of keys) {
       const res = key.match(str);
       if (res) {
@@ -218,7 +224,7 @@ class CrudService extends NafService {
       }
     }
     // 再次过滤数据,将数组的数据都变成{$in:value},因为查询变成了聚合查询
-    keys = Object.keys(filter).filter(f => !f.includes('$'));
+    keys = Object.keys(filter).filter((f) => !f.includes('$'));
     for (const key of keys) {
       if (_.isArray(filter[key])) {
         filter[key] = { $in: filter[key] };
@@ -293,7 +299,7 @@ class CrudService extends NafService {
   // 格式化model路径
   formatModelPath(str) {
     let arr = str.split('.');
-    arr = arr.map(i => _.upperFirst(i));
+    arr = arr.map((i) => _.upperFirst(i));
     const modelPath = arr.join('.');
     return modelPath;
   }