@ruiapp/rapid-core 0.7.4 → 0.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +21 -9
- package/dist/types.d.ts +4 -0
- package/package.json +1 -1
- package/src/dataAccess/entityManager.ts +20 -9
- package/src/types.ts +5 -0
package/dist/index.js
CHANGED
|
@@ -2815,18 +2815,30 @@ async function findEntities(server, dataAccessor, options) {
|
|
|
2815
2815
|
continue;
|
|
2816
2816
|
}
|
|
2817
2817
|
if (isManyRelation) {
|
|
2818
|
-
const
|
|
2818
|
+
const selectRelationOptions = relationOptions[relationProperty.code];
|
|
2819
|
+
const { relationLinks, targetEntities } = await findManyRelationLinksViaLinkTable({
|
|
2819
2820
|
server,
|
|
2820
2821
|
routeContext,
|
|
2821
2822
|
mainModel: relationModel,
|
|
2822
2823
|
relationProperty,
|
|
2823
2824
|
mainEntityIds: entityIds,
|
|
2824
|
-
selectRelationOptions
|
|
2825
|
+
selectRelationOptions,
|
|
2825
2826
|
});
|
|
2827
|
+
// 如果查询关联实体时指定了orderBy,则按照targetEntities的顺序,否则按照relationLinks的顺序。
|
|
2828
|
+
const selectRelationOrderByOptionsSpecified = selectRelationOptions && lodash.isObject(selectRelationOptions) && selectRelationOptions.orderBy;
|
|
2826
2829
|
lodash.forEach(rows, (row) => {
|
|
2827
|
-
|
|
2828
|
-
|
|
2829
|
-
|
|
2830
|
+
if (selectRelationOrderByOptionsSpecified) {
|
|
2831
|
+
row[relationProperty.code] = lodash.filter(targetEntities, (targetEntity) => {
|
|
2832
|
+
return lodash.find(relationLinks, (link) => {
|
|
2833
|
+
return link[relationProperty.selfIdColumnName] == row["id"] && link[relationProperty.targetIdColumnName] == targetEntity["id"];
|
|
2834
|
+
});
|
|
2835
|
+
}).map((targetEntity) => mapDbRowToEntity(server, relationModel, targetEntity, options.keepNonPropertyFields));
|
|
2836
|
+
}
|
|
2837
|
+
else {
|
|
2838
|
+
row[relationProperty.code] = lodash.filter(relationLinks, (link) => {
|
|
2839
|
+
return link[relationProperty.selfIdColumnName] == row["id"];
|
|
2840
|
+
}).map((link) => mapDbRowToEntity(server, relationModel, link.targetEntity, options.keepNonPropertyFields));
|
|
2841
|
+
}
|
|
2830
2842
|
});
|
|
2831
2843
|
}
|
|
2832
2844
|
}
|
|
@@ -3139,8 +3151,8 @@ async function findManyRelationLinksViaLinkTable(options) {
|
|
|
3139
3151
|
ORDER BY id
|
|
3140
3152
|
`;
|
|
3141
3153
|
const params = [mainEntityIds];
|
|
3142
|
-
const
|
|
3143
|
-
const targetEntityIds =
|
|
3154
|
+
const relationLinks = await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
|
|
3155
|
+
const targetEntityIds = relationLinks.map((link) => link[relationProperty.targetIdColumnName]);
|
|
3144
3156
|
const dataAccessor = server.getDataAccessor({
|
|
3145
3157
|
namespace: relationModel.namespace,
|
|
3146
3158
|
singularCode: relationModel.singularCode,
|
|
@@ -3178,10 +3190,10 @@ async function findManyRelationLinksViaLinkTable(options) {
|
|
|
3178
3190
|
}
|
|
3179
3191
|
}
|
|
3180
3192
|
const targetEntities = await findEntities(server, dataAccessor, findEntityOptions);
|
|
3181
|
-
lodash.forEach(
|
|
3193
|
+
lodash.forEach(relationLinks, (link) => {
|
|
3182
3194
|
link.targetEntity = lodash.find(targetEntities, (e) => e["id"] == link[relationProperty.targetIdColumnName]);
|
|
3183
3195
|
});
|
|
3184
|
-
return
|
|
3196
|
+
return { relationLinks, targetEntities };
|
|
3185
3197
|
}
|
|
3186
3198
|
async function findManyRelatedEntitiesViaIdPropertyCode(options) {
|
|
3187
3199
|
const { server, relationProperty, mainEntityIds, selectRelationOptions } = options;
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -270,19 +270,30 @@ async function findEntities(server: IRpdServer, dataAccessor: IRpdDataAccessor,
|
|
|
270
270
|
}
|
|
271
271
|
|
|
272
272
|
if (isManyRelation) {
|
|
273
|
-
const
|
|
273
|
+
const selectRelationOptions: FindEntityFindManyRelationEntitiesOptions | undefined = relationOptions[relationProperty.code];
|
|
274
|
+
const { relationLinks, targetEntities } = await findManyRelationLinksViaLinkTable({
|
|
274
275
|
server,
|
|
275
276
|
routeContext,
|
|
276
277
|
mainModel: relationModel,
|
|
277
278
|
relationProperty,
|
|
278
279
|
mainEntityIds: entityIds,
|
|
279
|
-
selectRelationOptions
|
|
280
|
+
selectRelationOptions,
|
|
280
281
|
});
|
|
281
282
|
|
|
283
|
+
// 如果查询关联实体时指定了orderBy,则按照targetEntities的顺序,否则按照relationLinks的顺序。
|
|
284
|
+
const selectRelationOrderByOptionsSpecified = selectRelationOptions && isObject(selectRelationOptions) && selectRelationOptions.orderBy;
|
|
282
285
|
forEach(rows, (row: any) => {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
+
if (selectRelationOrderByOptionsSpecified) {
|
|
287
|
+
row[relationProperty.code] = filter(targetEntities, (targetEntity: any) => {
|
|
288
|
+
return find(relationLinks, (link: any) => {
|
|
289
|
+
return link[relationProperty.selfIdColumnName!] == row["id"] && link[relationProperty.targetIdColumnName!] == targetEntity["id"];
|
|
290
|
+
});
|
|
291
|
+
}).map((targetEntity) => mapDbRowToEntity(server, relationModel, targetEntity, options.keepNonPropertyFields));
|
|
292
|
+
} else {
|
|
293
|
+
row[relationProperty.code] = filter(relationLinks, (link: any) => {
|
|
294
|
+
return link[relationProperty.selfIdColumnName!] == row["id"];
|
|
295
|
+
}).map((link) => mapDbRowToEntity(server, relationModel, link.targetEntity, options.keepNonPropertyFields));
|
|
296
|
+
}
|
|
286
297
|
});
|
|
287
298
|
}
|
|
288
299
|
} else {
|
|
@@ -628,8 +639,8 @@ async function findManyRelationLinksViaLinkTable(options: FindManyRelationEntiti
|
|
|
628
639
|
ORDER BY id
|
|
629
640
|
`;
|
|
630
641
|
const params = [mainEntityIds];
|
|
631
|
-
const
|
|
632
|
-
const targetEntityIds =
|
|
642
|
+
const relationLinks = await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
|
|
643
|
+
const targetEntityIds = relationLinks.map((link) => link[relationProperty.targetIdColumnName!]);
|
|
633
644
|
|
|
634
645
|
const dataAccessor = server.getDataAccessor({
|
|
635
646
|
namespace: relationModel.namespace,
|
|
@@ -672,11 +683,11 @@ async function findManyRelationLinksViaLinkTable(options: FindManyRelationEntiti
|
|
|
672
683
|
|
|
673
684
|
const targetEntities = await findEntities(server, dataAccessor, findEntityOptions);
|
|
674
685
|
|
|
675
|
-
forEach(
|
|
686
|
+
forEach(relationLinks, (link: any) => {
|
|
676
687
|
link.targetEntity = find(targetEntities, (e: any) => e["id"] == link[relationProperty.targetIdColumnName!]);
|
|
677
688
|
});
|
|
678
689
|
|
|
679
|
-
return
|
|
690
|
+
return { relationLinks, targetEntities };
|
|
680
691
|
}
|
|
681
692
|
|
|
682
693
|
async function findManyRelatedEntitiesViaIdPropertyCode(options: FindManyRelationEntitiesOptions) {
|