This commit is contained in:
parent
952f65132b
commit
ccce584a17
|
|
@ -458,168 +458,165 @@ var CascadeStore = /** @class */ (function (_super) {
|
||||||
var _g = projection2[attr], subProjection_1 = _g.data, subFilter_1 = _g.filter, indexFrom_1 = _g.indexFrom, count_1 = _g.count, subSorter_1 = _g.sorter;
|
var _g = projection2[attr], subProjection_1 = _g.data, subFilter_1 = _g.filter, indexFrom_1 = _g.indexFrom, count_1 = _g.count, subSorter_1 = _g.sorter;
|
||||||
var _h = tslib_1.__read(relation, 2), entity2_1 = _h[0], foreignKey_1 = _h[1];
|
var _h = tslib_1.__read(relation, 2), entity2_1 = _h[0], foreignKey_1 = _h[1];
|
||||||
var isAggr = attr.endsWith('$$aggr');
|
var isAggr = attr.endsWith('$$aggr');
|
||||||
if (context instanceof AsyncRowStore_1.AsyncContext) {
|
if (foreignKey_1) {
|
||||||
// cascade的聚合运算只有后台需要执行
|
// 基于属性的一对多
|
||||||
if (foreignKey_1) {
|
if (isAggr) {
|
||||||
// 基于属性的一对多
|
// 是聚合运算,只有后台才需要执行
|
||||||
if (isAggr) {
|
(context instanceof AsyncRowStore_1.AsyncContext) && cascadeSelectionFns.push(function (result) {
|
||||||
// 是聚合运算
|
var aggrResults = result.map(function (row) {
|
||||||
cascadeSelectionFns.push(function (result) {
|
var _a, _b;
|
||||||
var aggrResults = result.map(function (row) {
|
var aggrResult = aggregateFn.call(_this, entity2_1, {
|
||||||
var _a, _b;
|
data: subProjection_1,
|
||||||
var aggrResult = aggregateFn.call(_this, entity2_1, {
|
filter: (0, filter_1.combineFilters)([(_a = {},
|
||||||
data: subProjection_1,
|
_a[foreignKey_1] = row.id,
|
||||||
filter: (0, filter_1.combineFilters)([(_a = {},
|
_a), subFilter_1]),
|
||||||
_a[foreignKey_1] = row.id,
|
sorter: subSorter_1,
|
||||||
_a), subFilter_1]),
|
indexFrom: indexFrom_1,
|
||||||
sorter: subSorter_1,
|
count: count_1
|
||||||
indexFrom: indexFrom_1,
|
}, context, option);
|
||||||
count: count_1
|
if (aggrResult instanceof Promise) {
|
||||||
}, context, option);
|
return aggrResult.then(function (aggrResultResult) {
|
||||||
if (aggrResult instanceof Promise) {
|
var _a;
|
||||||
return aggrResult.then(function (aggrResultResult) {
|
return Object.assign(row, (_a = {},
|
||||||
var _a;
|
_a[attr] = aggrResultResult,
|
||||||
return Object.assign(row, (_a = {},
|
|
||||||
_a[attr] = aggrResultResult,
|
|
||||||
_a));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Object.assign(row, (_b = {},
|
|
||||||
_b[attr] = aggrResult,
|
|
||||||
_b));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
|
||||||
return Promise.all(aggrResults).then(function () { return undefined; });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// 是一对多查询
|
|
||||||
cascadeSelectionFns.push(function (result) {
|
|
||||||
var _a;
|
|
||||||
var ids = result.map(function (ele) { return ele.id; });
|
|
||||||
var dealWithSubRows = function (subRows) {
|
|
||||||
var _a;
|
|
||||||
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
|
||||||
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
|
||||||
if (result.length == 1) {
|
|
||||||
Object.assign(result[0], (_a = {},
|
|
||||||
_a[attr] = subRows,
|
|
||||||
_a));
|
_a));
|
||||||
}
|
});
|
||||||
else {
|
}
|
||||||
result.forEach(function (ele) {
|
else {
|
||||||
var _a;
|
Object.assign(row, (_b = {},
|
||||||
var subRowss = subRows.filter(function (ele2) { return ele2[foreignKey_1] === ele.id; });
|
_b[attr] = aggrResult,
|
||||||
(0, assert_1.default)(subRowss);
|
_b));
|
||||||
Object.assign(ele, (_a = {},
|
|
||||||
_a[attr] = subRowss,
|
|
||||||
_a));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (ids.length > 0) {
|
|
||||||
var subRows = cascadeSelectFn.call(_this, entity2_1, {
|
|
||||||
data: subProjection_1,
|
|
||||||
filter: (0, filter_1.combineFilters)([(_a = {},
|
|
||||||
_a[foreignKey_1] = {
|
|
||||||
$in: ids,
|
|
||||||
},
|
|
||||||
_a), subFilter_1]),
|
|
||||||
sorter: subSorter_1,
|
|
||||||
indexFrom: indexFrom_1,
|
|
||||||
count: count_1
|
|
||||||
}, context, option);
|
|
||||||
if (subRows instanceof Promise) {
|
|
||||||
return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
|
|
||||||
}
|
|
||||||
dealWithSubRows(subRows);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
||||||
|
return Promise.all(aggrResults).then(function () { return undefined; });
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 基于entity的多对一
|
// 是一对多查询
|
||||||
if (isAggr) {
|
cascadeSelectionFns.push(function (result) {
|
||||||
// 是聚合运算
|
var _a;
|
||||||
cascadeSelectionFns.push(function (result) {
|
var ids = result.map(function (ele) { return ele.id; });
|
||||||
var aggrResults = result.map(function (row) {
|
var dealWithSubRows = function (subRows) {
|
||||||
var _a;
|
var _a;
|
||||||
var aggrResult = aggregateFn.call(_this, entity2_1, {
|
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
||||||
data: subProjection_1,
|
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
||||||
filter: (0, filter_1.combineFilters)([{
|
if (result.length == 1) {
|
||||||
entity: entity,
|
Object.assign(result[0], (_a = {},
|
||||||
entityId: row.id,
|
_a[attr] = subRows,
|
||||||
}, subFilter_1]),
|
_a));
|
||||||
sorter: subSorter_1,
|
}
|
||||||
indexFrom: indexFrom_1,
|
else {
|
||||||
count: count_1
|
result.forEach(function (ele) {
|
||||||
}, context, option);
|
var _a;
|
||||||
if (aggrResult instanceof Promise) {
|
var subRowss = subRows.filter(function (ele2) { return ele2[foreignKey_1] === ele.id; });
|
||||||
return aggrResult.then(function (aggrResultResult) {
|
(0, assert_1.default)(subRowss);
|
||||||
var _a;
|
Object.assign(ele, (_a = {},
|
||||||
return Object.assign(row, (_a = {},
|
_a[attr] = subRowss,
|
||||||
_a[attr] = aggrResultResult,
|
|
||||||
_a));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Object.assign(row, (_a = {},
|
|
||||||
_a[attr] = aggrResult,
|
|
||||||
_a));
|
_a));
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
};
|
||||||
return Promise.all(aggrResults).then(function () { return undefined; });
|
if (ids.length > 0) {
|
||||||
|
var subRows = cascadeSelectFn.call(_this, entity2_1, {
|
||||||
|
data: subProjection_1,
|
||||||
|
filter: (0, filter_1.combineFilters)([(_a = {},
|
||||||
|
_a[foreignKey_1] = {
|
||||||
|
$in: ids,
|
||||||
|
},
|
||||||
|
_a), subFilter_1]),
|
||||||
|
sorter: subSorter_1,
|
||||||
|
indexFrom: indexFrom_1,
|
||||||
|
count: count_1
|
||||||
|
}, context, option);
|
||||||
|
if (subRows instanceof Promise) {
|
||||||
|
return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
|
||||||
|
}
|
||||||
|
dealWithSubRows(subRows);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 基于entity的多对一
|
||||||
|
if (isAggr) {
|
||||||
|
// 是聚合运算,只有后台才需要执行
|
||||||
|
(context instanceof AsyncRowStore_1.AsyncContext) && cascadeSelectionFns.push(function (result) {
|
||||||
|
var aggrResults = result.map(function (row) {
|
||||||
|
var _a;
|
||||||
|
var aggrResult = aggregateFn.call(_this, entity2_1, {
|
||||||
|
data: subProjection_1,
|
||||||
|
filter: (0, filter_1.combineFilters)([{
|
||||||
|
entity: entity,
|
||||||
|
entityId: row.id,
|
||||||
|
}, subFilter_1]),
|
||||||
|
sorter: subSorter_1,
|
||||||
|
indexFrom: indexFrom_1,
|
||||||
|
count: count_1
|
||||||
|
}, context, option);
|
||||||
|
if (aggrResult instanceof Promise) {
|
||||||
|
return aggrResult.then(function (aggrResultResult) {
|
||||||
|
var _a;
|
||||||
|
return Object.assign(row, (_a = {},
|
||||||
|
_a[attr] = aggrResultResult,
|
||||||
|
_a));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Object.assign(row, (_a = {},
|
||||||
|
_a[attr] = aggrResult,
|
||||||
|
_a));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
||||||
else {
|
return Promise.all(aggrResults).then(function () { return undefined; });
|
||||||
// 是一对多查询
|
}
|
||||||
cascadeSelectionFns.push(function (result) {
|
});
|
||||||
var ids = result.map(function (ele) { return ele.id; });
|
}
|
||||||
var dealWithSubRows = function (subRows) {
|
else {
|
||||||
var _a;
|
// 是一对多查询
|
||||||
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
cascadeSelectionFns.push(function (result) {
|
||||||
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
var ids = result.map(function (ele) { return ele.id; });
|
||||||
if (result.length === 1) {
|
var dealWithSubRows = function (subRows) {
|
||||||
Object.assign(result[0], (_a = {},
|
var _a;
|
||||||
_a[attr] = subRows,
|
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
||||||
_a));
|
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
||||||
}
|
if (result.length === 1) {
|
||||||
else {
|
Object.assign(result[0], (_a = {},
|
||||||
result.forEach(function (ele) {
|
_a[attr] = subRows,
|
||||||
var _a;
|
_a));
|
||||||
var subRowss = subRows.filter(function (ele2) { return ele2.entityId === ele.id; });
|
|
||||||
(0, assert_1.default)(subRowss);
|
|
||||||
Object.assign(ele, (_a = {},
|
|
||||||
_a[attr] = subRowss,
|
|
||||||
_a));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (ids.length > 0) {
|
|
||||||
var subRows = cascadeSelectFn.call(_this, entity2_1, {
|
|
||||||
data: subProjection_1,
|
|
||||||
filter: (0, filter_1.combineFilters)([{
|
|
||||||
entity: entity,
|
|
||||||
entityId: {
|
|
||||||
$in: ids,
|
|
||||||
}
|
|
||||||
}, subFilter_1]),
|
|
||||||
sorter: subSorter_1,
|
|
||||||
indexFrom: indexFrom_1,
|
|
||||||
count: count_1
|
|
||||||
}, context, option);
|
|
||||||
if (subRows instanceof Promise) {
|
|
||||||
return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
|
|
||||||
}
|
|
||||||
dealWithSubRows(subRows);
|
|
||||||
}
|
}
|
||||||
});
|
else {
|
||||||
}
|
result.forEach(function (ele) {
|
||||||
|
var _a;
|
||||||
|
var subRowss = subRows.filter(function (ele2) { return ele2.entityId === ele.id; });
|
||||||
|
(0, assert_1.default)(subRowss);
|
||||||
|
Object.assign(ele, (_a = {},
|
||||||
|
_a[attr] = subRowss,
|
||||||
|
_a));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if (ids.length > 0) {
|
||||||
|
var subRows = cascadeSelectFn.call(_this, entity2_1, {
|
||||||
|
data: subProjection_1,
|
||||||
|
filter: (0, filter_1.combineFilters)([{
|
||||||
|
entity: entity,
|
||||||
|
entityId: {
|
||||||
|
$in: ids,
|
||||||
|
}
|
||||||
|
}, subFilter_1]),
|
||||||
|
sorter: subSorter_1,
|
||||||
|
indexFrom: indexFrom_1,
|
||||||
|
count: count_1
|
||||||
|
}, context, option);
|
||||||
|
if (subRows instanceof Promise) {
|
||||||
|
return subRows.then(function (subRowss) { return dealWithSubRows(subRowss); });
|
||||||
|
}
|
||||||
|
dealWithSubRows(subRows);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -564,194 +564,191 @@ export abstract class CascadeStore<ED extends EntityDict & BaseEntityDict> exten
|
||||||
const { data: subProjection, filter: subFilter, indexFrom, count, sorter: subSorter } = projection2[attr];
|
const { data: subProjection, filter: subFilter, indexFrom, count, sorter: subSorter } = projection2[attr];
|
||||||
const [entity2, foreignKey] = relation;
|
const [entity2, foreignKey] = relation;
|
||||||
const isAggr = attr.endsWith('$$aggr');
|
const isAggr = attr.endsWith('$$aggr');
|
||||||
if (context instanceof AsyncContext) {
|
if (foreignKey) {
|
||||||
// cascade的聚合运算只有后台需要执行
|
// 基于属性的一对多
|
||||||
if (foreignKey) {
|
if (isAggr) {
|
||||||
// 基于属性的一对多
|
// 是聚合运算,只有后台才需要执行
|
||||||
if (isAggr) {
|
(context instanceof AsyncContext) && cascadeSelectionFns.push(
|
||||||
// 是聚合运算
|
(result) => {
|
||||||
cascadeSelectionFns.push(
|
const aggrResults = result.map(
|
||||||
(result) => {
|
(row) => {
|
||||||
const aggrResults = result.map(
|
const aggrResult = aggregateFn.call(this, entity2, {
|
||||||
(row) => {
|
|
||||||
const aggrResult = aggregateFn.call(this, entity2, {
|
|
||||||
data: subProjection,
|
|
||||||
filter: combineFilters([{
|
|
||||||
[foreignKey]: row.id,
|
|
||||||
}, subFilter]),
|
|
||||||
sorter: subSorter,
|
|
||||||
indexFrom,
|
|
||||||
count
|
|
||||||
}, context, option);
|
|
||||||
if (aggrResult instanceof Promise) {
|
|
||||||
return aggrResult.then(
|
|
||||||
(aggrResultResult) => Object.assign(row, {
|
|
||||||
[attr]: aggrResultResult,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Object.assign(row, {
|
|
||||||
[attr]: aggrResult,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
|
||||||
return Promise.all(aggrResults).then(
|
|
||||||
() => undefined
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// 是一对多查询
|
|
||||||
cascadeSelectionFns.push(
|
|
||||||
(result) => {
|
|
||||||
const ids = result.map(
|
|
||||||
ele => ele.id
|
|
||||||
) as string[];
|
|
||||||
|
|
||||||
const dealWithSubRows = (subRows: Partial<ED[keyof ED]['Schema']>[]) => {
|
|
||||||
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
|
||||||
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
|
||||||
if (result.length == 1) {
|
|
||||||
Object.assign(result[0], {
|
|
||||||
[attr]: subRows,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.forEach(
|
|
||||||
(ele) => {
|
|
||||||
const subRowss = subRows.filter(
|
|
||||||
ele2 => ele2[foreignKey] === ele.id
|
|
||||||
);
|
|
||||||
assert(subRowss);
|
|
||||||
Object.assign(ele, {
|
|
||||||
[attr]: subRowss,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (ids.length > 0) {
|
|
||||||
const subRows = cascadeSelectFn.call(this, entity2, {
|
|
||||||
data: subProjection,
|
data: subProjection,
|
||||||
filter: combineFilters([{
|
filter: combineFilters([{
|
||||||
[foreignKey]: {
|
[foreignKey]: row.id,
|
||||||
$in: ids,
|
|
||||||
}
|
|
||||||
}, subFilter]),
|
}, subFilter]),
|
||||||
sorter: subSorter,
|
sorter: subSorter,
|
||||||
indexFrom,
|
indexFrom,
|
||||||
count
|
count
|
||||||
}, context, option);
|
}, context, option);
|
||||||
if (subRows instanceof Promise) {
|
if (aggrResult instanceof Promise) {
|
||||||
return subRows.then(
|
return aggrResult.then(
|
||||||
(subRowss) => dealWithSubRows(subRowss)
|
(aggrResultResult) => Object.assign(row, {
|
||||||
|
[attr]: aggrResultResult,
|
||||||
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
dealWithSubRows(subRows as any);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// 基于entity的多对一
|
|
||||||
if (isAggr) {
|
|
||||||
// 是聚合运算
|
|
||||||
cascadeSelectionFns.push(
|
|
||||||
(result) => {
|
|
||||||
const aggrResults = result.map(
|
|
||||||
(row) => {
|
|
||||||
const aggrResult = aggregateFn.call(this, entity2, {
|
|
||||||
data: subProjection,
|
|
||||||
filter: combineFilters([{
|
|
||||||
entity,
|
|
||||||
entityId: row.id,
|
|
||||||
}, subFilter]),
|
|
||||||
sorter: subSorter,
|
|
||||||
indexFrom,
|
|
||||||
count
|
|
||||||
}, context, option);
|
|
||||||
if (aggrResult instanceof Promise) {
|
|
||||||
return aggrResult.then(
|
|
||||||
(aggrResultResult) => Object.assign(row, {
|
|
||||||
[attr]: aggrResultResult,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Object.assign(row, {
|
|
||||||
[attr]: aggrResult,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
|
||||||
return Promise.all(aggrResults).then(
|
|
||||||
() => undefined
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// 是一对多查询
|
|
||||||
cascadeSelectionFns.push(
|
|
||||||
(result) => {
|
|
||||||
const ids = result.map(
|
|
||||||
ele => ele.id
|
|
||||||
) as string[];
|
|
||||||
const dealWithSubRows = (subRows: Partial<ED[T]['Schema']>[]) => {
|
|
||||||
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
|
||||||
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
|
||||||
if (result.length === 1) {
|
|
||||||
Object.assign(result[0], {
|
|
||||||
[attr]: subRows,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
result.forEach(
|
Object.assign(row, {
|
||||||
(ele) => {
|
[attr]: aggrResult,
|
||||||
const subRowss = subRows.filter(
|
});
|
||||||
ele2 => ele2.entityId === ele.id
|
|
||||||
);
|
|
||||||
assert(subRowss);
|
|
||||||
Object.assign(ele, {
|
|
||||||
[attr]: subRowss,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
);
|
||||||
|
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
||||||
|
return Promise.all(aggrResults).then(
|
||||||
|
() => undefined
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 是一对多查询
|
||||||
|
cascadeSelectionFns.push(
|
||||||
|
(result) => {
|
||||||
|
const ids = result.map(
|
||||||
|
ele => ele.id
|
||||||
|
) as string[];
|
||||||
|
|
||||||
if (ids.length > 0) {
|
const dealWithSubRows = (subRows: Partial<ED[keyof ED]['Schema']>[]) => {
|
||||||
const subRows = cascadeSelectFn.call(this, entity2, {
|
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
||||||
|
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
||||||
|
if (result.length == 1) {
|
||||||
|
Object.assign(result[0], {
|
||||||
|
[attr]: subRows,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.forEach(
|
||||||
|
(ele) => {
|
||||||
|
const subRowss = subRows.filter(
|
||||||
|
ele2 => ele2[foreignKey] === ele.id
|
||||||
|
);
|
||||||
|
assert(subRowss);
|
||||||
|
Object.assign(ele, {
|
||||||
|
[attr]: subRowss,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (ids.length > 0) {
|
||||||
|
const subRows = cascadeSelectFn.call(this, entity2, {
|
||||||
|
data: subProjection,
|
||||||
|
filter: combineFilters([{
|
||||||
|
[foreignKey]: {
|
||||||
|
$in: ids,
|
||||||
|
}
|
||||||
|
}, subFilter]),
|
||||||
|
sorter: subSorter,
|
||||||
|
indexFrom,
|
||||||
|
count
|
||||||
|
}, context, option);
|
||||||
|
if (subRows instanceof Promise) {
|
||||||
|
return subRows.then(
|
||||||
|
(subRowss) => dealWithSubRows(subRowss)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
dealWithSubRows(subRows as any);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 基于entity的多对一
|
||||||
|
if (isAggr) {
|
||||||
|
// 是聚合运算,只有后台才需要执行
|
||||||
|
(context instanceof AsyncContext) && cascadeSelectionFns.push(
|
||||||
|
(result) => {
|
||||||
|
const aggrResults = result.map(
|
||||||
|
(row) => {
|
||||||
|
const aggrResult = aggregateFn.call(this, entity2, {
|
||||||
data: subProjection,
|
data: subProjection,
|
||||||
filter: combineFilters([{
|
filter: combineFilters([{
|
||||||
entity,
|
entity,
|
||||||
entityId: {
|
entityId: row.id,
|
||||||
$in: ids,
|
|
||||||
}
|
|
||||||
}, subFilter]),
|
}, subFilter]),
|
||||||
sorter: subSorter,
|
sorter: subSorter,
|
||||||
indexFrom,
|
indexFrom,
|
||||||
count
|
count
|
||||||
}, context, option);
|
}, context, option);
|
||||||
if (subRows instanceof Promise) {
|
if (aggrResult instanceof Promise) {
|
||||||
return subRows.then(
|
return aggrResult.then(
|
||||||
(subRowss) => dealWithSubRows(subRowss)
|
(aggrResultResult) => Object.assign(row, {
|
||||||
|
[attr]: aggrResultResult,
|
||||||
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
dealWithSubRows(subRows as any);
|
else {
|
||||||
|
Object.assign(row, {
|
||||||
|
[attr]: aggrResult,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
if (aggrResults.length > 0 && aggrResults[0] instanceof Promise) {
|
||||||
|
return Promise.all(aggrResults).then(
|
||||||
|
() => undefined
|
||||||
|
);
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// 是一对多查询
|
||||||
|
cascadeSelectionFns.push(
|
||||||
|
(result) => {
|
||||||
|
const ids = result.map(
|
||||||
|
ele => ele.id
|
||||||
|
) as string[];
|
||||||
|
const dealWithSubRows = (subRows: Partial<ED[T]['Schema']>[]) => {
|
||||||
|
// 这里如果result只有一行,则把返回结果直接置上,不对比外键值
|
||||||
|
// 这样做的原因是有的对象的filter会被改写掉(userId),只能临时这样处理
|
||||||
|
if (result.length === 1) {
|
||||||
|
Object.assign(result[0], {
|
||||||
|
[attr]: subRows,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.forEach(
|
||||||
|
(ele) => {
|
||||||
|
const subRowss = subRows.filter(
|
||||||
|
ele2 => ele2.entityId === ele.id
|
||||||
|
);
|
||||||
|
assert(subRowss);
|
||||||
|
Object.assign(ele, {
|
||||||
|
[attr]: subRowss,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (ids.length > 0) {
|
||||||
|
const subRows = cascadeSelectFn.call(this, entity2, {
|
||||||
|
data: subProjection,
|
||||||
|
filter: combineFilters([{
|
||||||
|
entity,
|
||||||
|
entityId: {
|
||||||
|
$in: ids,
|
||||||
|
}
|
||||||
|
}, subFilter]),
|
||||||
|
sorter: subSorter,
|
||||||
|
indexFrom,
|
||||||
|
count
|
||||||
|
}, context, option);
|
||||||
|
if (subRows instanceof Promise) {
|
||||||
|
return subRows.then(
|
||||||
|
(subRowss) => dealWithSubRows(subRowss)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
dealWithSubRows(subRows as any);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue