Skip to content

Commit 67fba60

Browse files
committed
polymor
1 parent 79c5b65 commit 67fba60

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

adminforth/modules/configValidator.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,6 @@ export default class ConfigValidator implements IConfigValidator {
505505
}
506506

507507
if (col.foreignResource) {
508-
509508
if (!col.foreignResource.resourceId) {
510509
// resourceId is absent or empty
511510
if (!col.foreignResource.polymorphicResources && !col.foreignResource.polymorphicOn) {
@@ -525,11 +524,11 @@ export default class ConfigValidator implements IConfigValidator {
525524
}
526525
// we do || here because 'resourceId' might yet not be assigned from 'table'
527526
col.foreignResource.polymorphicResources.forEach((polymorphicResource, polymorphicResourceIndex) => {
528-
if (!polymorphicResource.resourceId) {
527+
if (polymorphicResource.resourceId === undefined) {
529528
errors.push(`Resource "${res.resourceId}" column "${col.name}" has polymorphic foreign resource without resourceId`);
530529
} else if (!polymorphicResource.whenValue) {
531530
errors.push(`Resource "${res.resourceId}" column "${col.name}" has polymorphic foreign resource without whenValue`);
532-
} else {
531+
} else if (polymorphicResource.resourceId !== null) {
533532
const resource = this.inputConfig.resources.find((r) => r.resourceId === polymorphicResource.resourceId || r.table === polymorphicResource.resourceId);
534533
if (!resource) {
535534
const similar = suggestIfTypo(this.inputConfig.resources.map((r) => r.resourceId || r.table), polymorphicResource.resourceId);

adminforth/modules/restApi.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,9 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
703703
const targetResourcePkFields = {};
704704
const pksUniques = {};
705705
col.foreignResource.polymorphicResources.forEach((pr) => {
706+
if (pr.resourceId === null) {
707+
return;
708+
}
706709
targetResources[pr.whenValue] = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
707710
targetConnectors[pr.whenValue] = this.adminforth.connectors[targetResources[pr.whenValue].dataSource];
708711
targetResourcePkFields[pr.whenValue] = targetResources[pr.whenValue].columns.find((col) => col.primaryKey).name;
@@ -747,6 +750,13 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
747750

748751
data.data.forEach((item) => {
749752
item[col.name] = targetDataMap[item[col.name]];
753+
754+
if (!item[col.name]) {
755+
const systemResource = col.foreignResource.polymorphicResources.find(pr => pr.resourceId === null);
756+
if (systemResource) {
757+
item[col.foreignResource.polymorphicOn] = systemResource.whenValue;
758+
}
759+
}
750760
});
751761
})
752762
);
@@ -819,7 +829,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
819829
return { error: `Column '${column}' in resource '${resourceId}' is not a foreign key` };
820830
}
821831

822-
const targetResourceIds = columnConfig.foreignResource.resourceId ? [columnConfig.foreignResource.resourceId] : columnConfig.foreignResource.polymorphicResources.map((pr) => pr.resourceId);
832+
const targetResourceIds = columnConfig.foreignResource.resourceId ? [columnConfig.foreignResource.resourceId] : columnConfig.foreignResource.polymorphicResources.filter(pr => pr.resourceId !== null).map((pr) => pr.resourceId);
823833
const targetResources = targetResourceIds.map((trId) => this.adminforth.config.resources.find((res) => res.resourceId == trId));
824834

825835
const responses = (await Promise.all(

dev-demo/resources/api_keys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export default {
5454
resourceId: 'providers',
5555
whenValue: 'provider',
5656
},
57+
{
58+
resourceId: null,
59+
whenValue: 'Syst1em',
60+
},
5761
],
5862
polymorphicOn: 'owner',
5963
},

0 commit comments

Comments
 (0)