Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions adminforth/dataConnectors/mongo.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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) {
Expand All @@ -89,6 +81,9 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
return !!value;
} else if (field.type == AdminForthDataTypes.DECIMAL) {
return value?.toString();
} else if (field.name === '_id' && !field.fillOnCreate) {
Copy link
Contributor

@ivictbor ivictbor Apr 11, 2025

Choose a reason for hiding this comment

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

@Astaldos will it brake something if we will remove && !field.fillOnCreate? I mean for the case if user returns object id in fillOnCreate.

// if "_id" was created by mongo it will be ObjectId
return value?.toString();
Copy link
Contributor

@ivictbor ivictbor Apr 9, 2025

Choose a reason for hiding this comment

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

@Astaldos I am a little bit afraid of case if user returns bson object from fillOnCreate.

Maybe it is better to check it like if type of value is object then do .toString(), is it safe or you see any potential issues with it?

}

return value;
Expand All @@ -111,6 +106,9 @@ 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) {
// if "_id" was created by mongo it supposed to be saved as ObjectId
return ObjectId.createFromHexString(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, we can check if value is type of string or number or null or undefined, then wrap it in bson Object id, otherwise pass "as is"

}
return value;
}
Expand Down