Skip to content

Commit e5b327c

Browse files
authored
Merge pull request #137 from devforth/polymorphic
Polymorphic
2 parents c412f75 + e7544c3 commit e5b327c

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

adminforth/modules/configValidator.ts

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

532532
if (col.foreignResource) {
533-
534533
if (!col.foreignResource.resourceId) {
535534
// resourceId is absent or empty
536535
if (!col.foreignResource.polymorphicResources && !col.foreignResource.polymorphicOn) {
@@ -550,11 +549,11 @@ export default class ConfigValidator implements IConfigValidator {
550549
}
551550
// we do || here because 'resourceId' might yet not be assigned from 'table'
552551
col.foreignResource.polymorphicResources.forEach((polymorphicResource, polymorphicResourceIndex) => {
553-
if (!polymorphicResource.resourceId) {
552+
if (polymorphicResource.resourceId === undefined) {
554553
errors.push(`Resource "${res.resourceId}" column "${col.name}" has polymorphic foreign resource without resourceId`);
555554
} else if (!polymorphicResource.whenValue) {
556555
errors.push(`Resource "${res.resourceId}" column "${col.name}" has polymorphic foreign resource without whenValue`);
557-
} else {
556+
} else if (polymorphicResource.resourceId !== null) {
558557
const resource = this.inputConfig.resources.find((r) => r.resourceId === polymorphicResource.resourceId || r.table === polymorphicResource.resourceId);
559558
if (!resource) {
560559
const similar = suggestIfTypo(this.inputConfig.resources.map((r) => r.resourceId || r.table), polymorphicResource.resourceId);

adminforth/modules/restApi.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
703703
const targetResourcePkFields = {};
704704
const pksUniques = {};
705705
col.foreignResource.polymorphicResources.forEach((pr) => {
706-
targetResources[pr.whenValue] = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
706+
if (pr.resourceId === null) {
707+
return;
708+
}
709+
const targetResource = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
710+
if (!targetResource) {
711+
return;
712+
}
713+
targetResources[pr.whenValue] = targetResource;
707714
targetConnectors[pr.whenValue] = this.adminforth.connectors[targetResources[pr.whenValue].dataSource];
708715
targetResourcePkFields[pr.whenValue] = targetResources[pr.whenValue].columns.find((col) => col.primaryKey).name;
709716
const pksUnique = [...new Set(data.data.filter((item) => item[col.foreignResource.polymorphicOn] === pr.whenValue).map((item) => item[col.name]))];
@@ -747,6 +754,13 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
747754

748755
data.data.forEach((item) => {
749756
item[col.name] = targetDataMap[item[col.name]];
757+
758+
if (!item[col.name]) {
759+
const systemResource = col.foreignResource.polymorphicResources.find(pr => pr.resourceId === null);
760+
if (systemResource) {
761+
item[col.foreignResource.polymorphicOn] = systemResource.whenValue;
762+
}
763+
}
750764
});
751765
})
752766
);
@@ -819,7 +833,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
819833
return { error: `Column '${column}' in resource '${resourceId}' is not a foreign key` };
820834
}
821835

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

825839
const responses = (await Promise.all(
@@ -974,7 +988,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
974988
const targetConnectors = {};
975989
const targetResourcePkFields = {};
976990
column.foreignResource.polymorphicResources.forEach((pr) => {
977-
targetResources[pr.whenValue] = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
991+
if (pr.resourceId === null) {
992+
return;
993+
}
994+
const targetResource = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
995+
if (!targetResource) {
996+
return;
997+
}
998+
targetResources[pr.whenValue] = targetResource;
978999
targetConnectors[pr.whenValue] = this.adminforth.connectors[targetResources[pr.whenValue].dataSource];
9791000
targetResourcePkFields[pr.whenValue] = targetResources[pr.whenValue].columns.find((col) => col.primaryKey).name;
9801001
});
@@ -1062,7 +1083,14 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
10621083
const targetConnectors = {};
10631084
const targetResourcePkFields = {};
10641085
column.foreignResource.polymorphicResources.forEach((pr) => {
1065-
targetResources[pr.whenValue] = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
1086+
if (pr.resourceId === null) {
1087+
return;
1088+
}
1089+
const targetResource = this.adminforth.config.resources.find((res) => res.resourceId == pr.resourceId);
1090+
if (!targetResource) {
1091+
return;
1092+
}
1093+
targetResources[pr.whenValue] = targetResource;
10661094
targetConnectors[pr.whenValue] = this.adminforth.connectors[targetResources[pr.whenValue].dataSource];
10671095
targetResourcePkFields[pr.whenValue] = targetResources[pr.whenValue].columns.find((col) => col.primaryKey).name;
10681096
});

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)