@@ -15,6 +15,8 @@ import {
1515 Filters ,
1616} from "../types/Back.js" ;
1717
18+ import { cascadeChildrenDelete } from './utils.js'
19+
1820import { afLogger } from "./logger.js" ;
1921
2022import { ADMINFORTH_VERSION , listify , md5hash , getLoginPromptHTML } from './utils.js' ;
@@ -152,47 +154,6 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
152154 }
153155 }
154156 }
155- async deleteWithCascade ( resource : AdminForthResource , primaryKey : string , context : { adminUser : any , response : any } ) : Promise < { error : string | null } > {
156- const { adminUser, response } = context ;
157-
158- const record = await this . adminforth . connectors [ resource . dataSource ] . getRecordByPrimaryKey ( resource , primaryKey ) ;
159-
160- if ( ! record ) {
161- return { error : `Record with id ${ primaryKey } not found` } ;
162- }
163-
164- const childResources = this . adminforth . config . resources . filter ( r => r . columns . some ( c => c . foreignResource ?. resourceId === resource . resourceId ) ) ;
165-
166- for ( const childRes of childResources ) {
167- const foreignColumn = childRes . columns . find ( c => c . foreignResource ?. resourceId === resource . resourceId ) ;
168-
169- if ( ! foreignColumn ?. foreignResource ?. onDelete ) continue ;
170-
171- const strategy = foreignColumn . foreignResource . onDelete ;
172-
173- const childRecords = await this . adminforth . resource ( childRes . resourceId ) . list ( Filters . EQ ( foreignColumn . name , primaryKey ) ) ;
174-
175- const childPk = childRes . columns . find ( c => c . primaryKey ) ?. name ;
176- if ( ! childPk ) continue ;
177-
178- if ( strategy === 'cascade' ) {
179- for ( const childRecord of childRecords ) {
180- const childResult = await this . deleteWithCascade ( childRes , childRecord [ childPk ] , context ) ;
181- if ( childResult ?. error ) {
182- return childResult ;
183- }
184- }
185- }
186-
187- if ( strategy === 'setNull' ) {
188- for ( const childRecord of childRecords ) {
189- await this . adminforth . resource ( childRes . resourceId ) . update ( childRecord [ childPk ] , { [ foreignColumn . name ] : null } ) ;
190- }
191- }
192- }
193- const deleteResult = await this . adminforth . deleteResourceRecord ( { resource, record, adminUser, recordId : primaryKey , response} ) ;
194- return { error : deleteResult . error } ;
195- }
196157
197158 registerEndpoints ( server : IHttpServer ) {
198159 server . endpoint ( {
@@ -1522,7 +1483,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
15221483 return { error } ;
15231484 }
15241485
1525- const { error : cascadeError } = await this . deleteWithCascade ( resource , body . primaryKey , { adminUser, response} ) ;
1486+ const { error : cascadeError } = await cascadeChildrenDelete ( resource , body . primaryKey , { adminUser, response} , this . adminforth ) ;
15261487 if ( cascadeError ) {
15271488 return { error : cascadeError } ;
15281489 }
0 commit comments