|
@@ -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;
|
|
|
}
|