diff --git a/adminforth/dataConnectors/baseConnector.ts b/adminforth/dataConnectors/baseConnector.ts index 3d032415..0198a944 100644 --- a/adminforth/dataConnectors/baseConnector.ts +++ b/adminforth/dataConnectors/baseConnector.ts @@ -37,7 +37,14 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon limit: 1, offset: 0, sort: [], - filters: { operator: AdminForthFilterOperators.AND, subFilters: [{ field: this.getPrimaryKey(resource), operator: AdminForthFilterOperators.EQ, value: id }]}, + filters: { + operator: AdminForthFilterOperators.AND, + subFilters: [{ + field: this.getPrimaryKey(resource), + operator: AdminForthFilterOperators.EQ, + value: this.setFieldValue(resource.dataSourceColumns.find((col) => col.name === this.getPrimaryKey(resource)), id), + }], + }, }); return data.length > 0 ? data[0] : null; } @@ -191,7 +198,14 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon process.env.HEAVY_DEBUG && console.log('☝️🪲🪲🪲🪲 checkUnique|||', column, value); const existingRecord = await this.getData({ resource, - filters: { operator: AdminForthFilterOperators.AND, subFilters: [{ field: column.name, operator: AdminForthFilterOperators.EQ, value }]}, + filters: { + operator: AdminForthFilterOperators.AND, + subFilters: [{ + field: column.name, + operator: AdminForthFilterOperators.EQ, + value: this.setFieldValue(column, value), + }], + }, limit: 1, sort: [], offset: 0, diff --git a/adminforth/dataConnectors/mongo.ts b/adminforth/dataConnectors/mongo.ts index b07ff5c7..dac3bc6c 100644 --- a/adminforth/dataConnectors/mongo.ts +++ b/adminforth/dataConnectors/mongo.ts @@ -1,6 +1,6 @@ import dayjs from 'dayjs'; import { MongoClient } from 'mongodb'; -import { Decimal128 } from 'bson'; +import { Decimal128, ObjectId } from 'bson'; import { IAdminForthDataSourceConnector, IAdminForthSingleFilter, IAdminForthAndOrFilter, AdminForthResource } from '../types/Back.js'; import AdminForthBaseConnector from './baseConnector.js'; @@ -64,14 +64,6 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS }, {}); } - getPrimaryKey(resource) { - for (const col of resource.dataSourceColumns) { - if (col.primaryKey) { - return col.name; - } - } - } - getFieldValue(field, value) { if (field.type == AdminForthDataTypes.DATETIME) { if (!value) { @@ -89,6 +81,11 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS return !!value; } else if (field.type == AdminForthDataTypes.DECIMAL) { return value?.toString(); + } else if (field.name === '_id' && !field.fillOnCreate) { + // value is supposed to be an ObjectId or string representing it + if (typeof value === 'object') { + return value?.toString(); + } } return value; @@ -111,6 +108,21 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS return value ? true : false; } else if (field.type == AdminForthDataTypes.DECIMAL) { return Decimal128.fromString(value?.toString()); + } else if (field.name === '_id' && !field.fillOnCreate) { + // value is supposed to be an ObjectId + if (!ObjectId.isValid(value)) { + return null; + } + if (typeof value === 'string' || typeof value === 'number') { + // if string or number - turn it into ObjectId + return new ObjectId(value); + } else if (typeof value === 'object') { + // assume it is a correct ObjectId + return value; + } + + // unsupported type for ObjectId + return null; } return value; } @@ -205,13 +217,14 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS async updateRecordOriginalValues({ resource, recordId, newValues }) { const collection = this.client.db().collection(resource.table); - await collection.updateOne({ [this.getPrimaryKey(resource)]: recordId }, { $set: newValues }); + const primaryKeyColumn = resource.dataSourceColumns.find((col) => col.name === this.getPrimaryKey(resource)); + await collection.updateOne({ [primaryKeyColumn.name]: this.setFieldValue(primaryKeyColumn, recordId) }, { $set: newValues }); } async deleteRecord({ resource, recordId }): Promise { - const primaryKey = this.getPrimaryKey(resource); const collection = this.client.db().collection(resource.table); - const res = await collection.deleteOne({ [primaryKey]: recordId }); + const primaryKeyColumn = resource.dataSourceColumns.find((col) => col.name === this.getPrimaryKey(resource)); + const res = await collection.deleteOne({ [primaryKeyColumn.name]: this.setFieldValue(primaryKeyColumn, recordId) }); return res.deletedCount > 0; }