@@ -227,11 +227,19 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
227227 throw new Error ( 'Method not implemented.' ) ;
228228 }
229229
230- async checkUnique ( resource : AdminForthResource , column : AdminForthResourceColumn , value : any ) {
230+ async checkUnique ( resource : AdminForthResource , column : AdminForthResourceColumn , value : any , record ?: any ) : Promise < boolean > {
231231 process . env . HEAVY_DEBUG && console . log ( '☝️🪲🪲🪲🪲 checkUnique|||' , column , value ) ;
232+
233+ const primaryKeyField = this . getPrimaryKey ( resource ) ;
232234 const existingRecord = await this . getData ( {
233235 resource,
234- filters : { operator : AdminForthFilterOperators . AND , subFilters : [ { field : column . name , operator : AdminForthFilterOperators . EQ , value } ] } ,
236+ filters : {
237+ operator : AdminForthFilterOperators . AND ,
238+ subFilters : [
239+ { field : column . name , operator : AdminForthFilterOperators . EQ , value } ,
240+ ...( record ? [ { field : primaryKeyField , operator : AdminForthFilterOperators . NE as AdminForthFilterOperators . NE , value : record [ primaryKeyField ] } ] : [ ] )
241+ ]
242+ } ,
235243 limit : 1 ,
236244 sort : [ ] ,
237245 offset : 0 ,
@@ -306,7 +314,7 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
306314 async updateRecord ( { resource, recordId, newValues } : { resource : AdminForthResource ; recordId : string ; newValues : any ; } ) : Promise < { error ?: string ; ok : boolean ; } > {
307315 // transform value using setFieldValue and call updateRecordOriginalValues
308316 const recordWithOriginalValues = { ...newValues } ;
309-
317+
310318 for ( const field of Object . keys ( newValues ) ) {
311319 const col = resource . dataSourceColumns . find ( ( col ) => col . name == field ) ;
312320 // todo instead of throwing error, we can just not use setFieldValue here, and pass original value to updateRecordOriginalValues
@@ -319,6 +327,23 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
319327 }
320328 recordWithOriginalValues [ col . name ] = this . setFieldValue ( col , newValues [ col . name ] ) ;
321329 }
330+ const record = await this . getRecordByPrimaryKey ( resource , recordId ) ;
331+ let error : string | null = null ;
332+ await Promise . all (
333+ resource . dataSourceColumns . map ( async ( col ) => {
334+ if ( col . isUnique && ! col . virtual && ! error ) {
335+ const exists = await this . checkUnique ( resource , col , recordWithOriginalValues [ col . name ] , record ) ;
336+ if ( exists ) {
337+ error = `Record with ${ col . name } ${ recordWithOriginalValues [ col . name ] } already exists` ;
338+ }
339+ }
340+ } )
341+ ) ;
342+ if ( error ) {
343+ process . env . HEAVY_DEBUG && console . log ( '🪲🆕 check unique error' , error ) ;
344+ return { error, ok : false } ;
345+ }
346+
322347
323348 process . env . HEAVY_DEBUG && console . log ( `🪲✏️ updating record id:${ recordId } , values: ${ JSON . stringify ( recordWithOriginalValues ) } ` ) ;
324349
0 commit comments