lrf пре 2 година
родитељ
комит
49c0bcff19
1 измењених фајлова са 43 додато и 26 уклоњено
  1. 43 26
      lib/service/crud-service.js

+ 43 - 26
lib/service/crud-service.js

@@ -1,6 +1,6 @@
 'use strict';
 
-const { isString, isArray, cloneDeep, head: getHead, get, omit } = require('lodash');
+const { isString, isArray, cloneDeep, head: getHead, get, omit, last } = require('lodash');
 const { isNullOrUndefined, trimData } = require('naf-core').Util;
 const assert = require('assert');
 const { ObjectId } = require('mongoose').Types;
@@ -152,25 +152,8 @@ class CrudService extends NafService {
     let condition = cloneDeep(filter);
     condition = await this.beforeQuery(condition);
     condition = this.dealFilter(condition);
-    // const pipeline = [{ $match: condition }];
-    // // 先排序
-    // if (sort) pipeline.push({ $sort: sort });
-    // // 再进行分页
-    // if (limit) {
-    //   pipeline.push({ $skip: parseInt(skip) });
-    //   pipeline.push({ $limit: parseInt(limit) });
-    // }
-    // // 再将数据过滤
-    // if (projection) pipeline.push({ $project: projection });
-    // else {
-    //   const defaultProject = await this.getSelectFalse();
-    //   if (defaultProject) pipeline.push({ $project: defaultProject });
-    // }
-    // let rs = await this.model.aggregate(pipeline);
-
     // 过滤出ref字段
-    const refMods = await this.getRefMods();
-    const populate = refMods.map(i => i.col);
+    const { refMods, populate } = await this.getRefMods();
     // 带ref查询
     let rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).populate(populate).exec();
     rs = JSON.parse(JSON.stringify(rs));
@@ -205,11 +188,32 @@ class CrudService extends NafService {
   async getRefMods() {
     const mod = await this.getModel();
     const refMods = [];
+    const populate = [];
     for (const key in mod) {
       if (!mod[key].ref && !mod[key].refPath) continue;
-      refMods.push({ col: key, prop: mod[key].getProp, type: mod[key].type.name });
+      const obj = { col: key, prop: mod[key].getProp, type: mod[key].type.name };
+      if (mod[key].ref) {
+        const ref = mod[key].ref;
+        if (ref.includes('.')) {
+          // 说明是跨数据源
+          const arr = ref.split('.');
+          const conn = this.app.mongooseDB.get(getHead(arr));
+          const refModel = last(arr);
+          const schema = this.ctx.model[refModel].schema;
+          const model = conn.model(refModel, schema);
+          const p = { path: key, model };
+          populate.push(p);
+        } else {
+          const p = { path: key };
+          populate.push(p);
+        }
+      } else if (mod[key].refPath) {
+        const p = { path: key };
+        populate.push(p);
+      }
+      refMods.push(obj);
     }
-    return refMods;
+    return { refMods, populate };
   }
 
   async count(filter) {
@@ -217,11 +221,6 @@ class CrudService extends NafService {
     condition = await this.beforeQuery(condition);
     condition = this.dealFilter(condition);
     const count = await this.model.count(condition);
-    // const res = await this.model.aggregate([{ $match: condition }, { $count: 'id' }]);
-    // if (res && isArray(res)) {
-    //   const head = getHead(res);
-    //   count = get(head, 'id', 0);
-    // }
     return count;
   }
 
@@ -316,4 +315,22 @@ class CrudService extends NafService {
   }
 }
 
+// 聚合式查询
+// const pipeline = [{ $match: condition }];
+// // 先排序
+// if (sort) pipeline.push({ $sort: sort });
+// // 再进行分页
+// if (limit) {
+//   pipeline.push({ $skip: parseInt(skip) });
+//   pipeline.push({ $limit: parseInt(limit) });
+// }
+// // 再将数据过滤
+// if (projection) pipeline.push({ $project: projection });
+// else {
+//   const defaultProject = await this.getSelectFalse();
+//   if (defaultProject) pipeline.push({ $project: defaultProject });
+// }
+// let rs = await this.model.aggregate(pipeline);
+
+
 module.exports.CrudService = CrudService;