Skip to content
8 changes: 7 additions & 1 deletion adminforth/modules/configValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,13 @@ export default class ConfigValidator implements IConfigValidator {
errors.push(`Resource "${res.resourceId}" column "${col.name}" isArray is enabled but suggestOnCreate is not an array`);
}
}

if (col.foreignResource){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You already have this check below
you can add your validation below
Also you can merge your two checks in one
like
if i have onDelete and onDelete is not cascade or setNull
throw error

if (col.foreignResource.onDelete){
if (col.foreignResource.onDelete !== 'cascade' && col.foreignResource.onDelete !== 'setNull'){
errors.push (`Wrong delete strategy you can use 'onDelete' or 'cascade'`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mistake in message you have setNull and cascade strategies but your error message tells that you have onDelete or cascade

}
}
}
if (col.foreignResource) {
if (!col.foreignResource.resourceId) {
// resourceId is absent or empty
Expand Down
20 changes: 19 additions & 1 deletion adminforth/modules/restApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1464,8 +1464,26 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
if (!record){
return { error: `Record with ${body['primaryKey']} not found` };
}
if (resource.options.allowedActions.delete === false) {
if (!resource.options.allowedActions.delete) {
return { error: `Resource '${resource.resourceId}' does not allow delete action` };
}
const childResources = this.adminforth.config.resources.filter(r => r.columns.some(c => c.foreignResource?.resourceId === resource.resourceId));
if (childResources.length){
for (const childRes of childResources) {
const foreignResourceColumn = childRes.columns.find(c => c.foreignResource?.resourceId === resource.resourceId);
if (!foreignResourceColumn.foreignResource.onDelete) continue;
const onDeleteStrategy = foreignResourceColumn.foreignResource.onDelete;
const childRecords = await this.adminforth.resource(childRes.resourceId).list(Filters.EQ(foreignResourceColumn.name, body['primaryKey']))
if (onDeleteStrategy === 'cascade') {
for (const childRecord of childRecords) {
await this.adminforth.resource(childRes.resourceId).delete(childRecord.id);
}
} else if (onDeleteStrategy === 'setNull') {
for (const childRecord of childRecords) {
await this.adminforth.resource(childRes.resourceId).update(childRecord.id, {[foreignResourceColumn.name]: null});
}
}
}
}

const { allowedActions } = await interpretResource(
Expand Down
1 change: 1 addition & 0 deletions adminforth/types/Back.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,7 @@ export interface AdminForthForeignResource extends AdminForthForeignResourceComm
afterDatasourceResponse?: AfterDataSourceResponseFunction | Array<AfterDataSourceResponseFunction>,
},
},
onDelete: 'cascade' | 'setNull'
}

export type ShowInModernInput = {
Expand Down