|
@@ -152,8 +152,7 @@ class MatchSmallGroupScheduleService extends CrudService {
|
|
|
} else if (sort && _.isArray(sort)) {
|
|
|
sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
}
|
|
|
- const { refMods, populate } = await this.getRefMods();
|
|
|
-
|
|
|
+ const { refMods, populate } = this.getRefMods();
|
|
|
let res = await this.model.findOne(filter, projection).populate(populate).exec();
|
|
|
res = JSON.parse(JSON.stringify(res));
|
|
|
for (const obj of refMods) {
|
|
@@ -205,79 +204,61 @@ class MatchSmallGroupScheduleService extends CrudService {
|
|
|
return d;
|
|
|
}
|
|
|
|
|
|
- // async getRefMods() {
|
|
|
- // const mod = await this.getModel();
|
|
|
- // const refMods = [];
|
|
|
- // const populate = [];
|
|
|
- // for (const key in mod) {
|
|
|
- // const { ref, refPath } = mod[key];
|
|
|
- // if (!ref && !refPath) continue;
|
|
|
- // const obj = { col: key, prop: mod[key].getProp, type: mod[key].type.name };
|
|
|
- // refMods.push(obj);
|
|
|
- // // 检查ref/refPath是否还有嵌套
|
|
|
- // if (ref) {
|
|
|
- // // 递归查找ref的关系,构造ref关系树图
|
|
|
- // const res = this.checkSchema(ref);
|
|
|
- // // console.log(res);
|
|
|
- // }
|
|
|
- // }
|
|
|
- // return { refMods, populate };
|
|
|
- // }
|
|
|
- // 组成子 populate => {path,model}
|
|
|
- checkSchema(thisRef, path) {
|
|
|
- const { schema } = this.findModel(thisRef, path);
|
|
|
+ getRefMods() {
|
|
|
+ // 找到该表的schema(表结构)
|
|
|
+ const mod = this.getSchema();
|
|
|
+ const populate = this.resetPopulate(mod);
|
|
|
+ const refMods = [];
|
|
|
+ for (const key in mod) {
|
|
|
+ if (!mod[key].ref && !mod[key].refPath) continue;
|
|
|
+ const obj = { col: key, prop: mod[key].getProp, type: mod[key].type.name };
|
|
|
+ refMods.push(obj);
|
|
|
+ }
|
|
|
+ return { refMods, populate };
|
|
|
+ }
|
|
|
+
|
|
|
+ // 针对每个表进行检查
|
|
|
+ resetPopulate(schema) {
|
|
|
+ const arr = [];
|
|
|
for (const key in schema) {
|
|
|
- const { ref, refPath } = schema[key];
|
|
|
- if (!ref && !refPath) continue;
|
|
|
- const obj = {};
|
|
|
- if (ref) {
|
|
|
- const arr = ref.split('.').map(i => _.upperFirst(i));
|
|
|
- obj.ref = _.upperFirst(_.last(arr));
|
|
|
- let mpath = path;
|
|
|
- // 如果有path,就应该不会有arr.length>0的情况
|
|
|
- if (arr.length > 0) {
|
|
|
- // 说明是有路径的,需要将路径也填充
|
|
|
- arr.pop();
|
|
|
- mpath = arr.join('.');
|
|
|
- }
|
|
|
- // 递归寻找ref
|
|
|
- const { schema: sch } = this.checkSchema(obj.ref, mpath);
|
|
|
- console.log(key, ref);
|
|
|
- console.log(sch);
|
|
|
- // for (const key in sch) {
|
|
|
- // const obj = sch[key];
|
|
|
- // const populate = this.dealSchema(obj);
|
|
|
- // }
|
|
|
- }
|
|
|
+ const e = schema[key];
|
|
|
+ const { ref } = e;
|
|
|
+ if (!ref) continue;
|
|
|
+ const obj = { path: key };
|
|
|
+ const modelPath = this.formatModelPath(ref);
|
|
|
+ const model = this.getModel(modelPath);
|
|
|
+ obj.model = model;
|
|
|
+ const msch = this.getSchema(modelPath);
|
|
|
+ const popu = this.resetPopulate(msch);
|
|
|
+ if (popu.length > 0) obj.populate = popu;
|
|
|
+ arr.push(obj);
|
|
|
}
|
|
|
- return schema;
|
|
|
+ return arr;
|
|
|
}
|
|
|
|
|
|
- // dealSchema(schema) {
|
|
|
- // }
|
|
|
+ // 格式化model路径
|
|
|
+ formatModelPath(str) {
|
|
|
+ let arr = str.split('.');
|
|
|
+ arr = arr.map(i => _.upperFirst(i));
|
|
|
+ const modelPath = arr.join('.');
|
|
|
+ return modelPath;
|
|
|
+ }
|
|
|
|
|
|
- findModel(modelName, path = this.defaultModule) {
|
|
|
- // 有没有默认路径,将路径整合进去
|
|
|
- const modelPath = [];
|
|
|
- if (path) modelPath.push(...path.split('.'));
|
|
|
- modelPath.push(_.upperFirst(modelName));
|
|
|
- const modelPathStr = modelPath.join('.');
|
|
|
- const m = _.get(this.ctx.model, modelPathStr);
|
|
|
- if (m) return { schema: _.get(m, 'prototype.schema.obj'), path: modelPathStr };
|
|
|
- let returnData;
|
|
|
- // 去掉要找到的目标model
|
|
|
- modelPath.pop();
|
|
|
- const dir = modelPath.join('.');
|
|
|
- const rootModel = _.get(this.ctx.model, dir, this.ctx.model);
|
|
|
- for (const mn in rootModel) {
|
|
|
- const type = typeof rootModel[mn];
|
|
|
- if (type === 'object') {
|
|
|
- // rootModel[mn] 是目录,要进入目录里面找内容
|
|
|
- returnData = this.findModel(modelName, mn);
|
|
|
- }
|
|
|
- // 没有 else 的原因是,对于model来说;路径是object类型,model是function类型,而model在 上面 有关m变量 部分已经处理完,不会有其他形式
|
|
|
+ // 获取model的模式
|
|
|
+ getSchema(path) {
|
|
|
+ const model = this.getModel(path);
|
|
|
+ return _.get(model, 'prototype.schema.obj');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取model实例
|
|
|
+ getModel(path) {
|
|
|
+ if (!path) return this.model;
|
|
|
+ let model = _.get(this.ctx.model, path);
|
|
|
+ const clients = this.app.mongooseDB.clients;
|
|
|
+ if (clients && !model) {
|
|
|
+ model = _.get(this.ctx.model, `${this.defaultModule}.${path}`);
|
|
|
}
|
|
|
- return returnData;
|
|
|
+ return model;
|
|
|
}
|
|
|
}
|
|
|
|