Skip to content

Commit 899a45d

Browse files
committed
fix: enhance uniqueness check in checkUnique method to exclude current record
1 parent 946c790 commit 899a45d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

adminforth/dataConnectors/baseConnector.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,12 +327,12 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
319327
}
320328
recordWithOriginalValues[col.name] = this.setFieldValue(col, newValues[col.name]);
321329
}
322-
330+
const record = await this.getRecordByPrimaryKey(resource, recordId);
323331
let error: string | null = null;
324332
await Promise.all(
325333
resource.dataSourceColumns.map(async (col) => {
326334
if (col.isUnique && !col.virtual && !error) {
327-
const exists = await this.checkUnique(resource, col, recordWithOriginalValues[col.name]);
335+
const exists = await this.checkUnique(resource, col, recordWithOriginalValues[col.name], record);
328336
if (exists) {
329337
error = `Record with ${col.name} ${recordWithOriginalValues[col.name]} already exists`;
330338
}

0 commit comments

Comments
 (0)