Skip to content

Commit 4118cce

Browse files
committed
fix: fix filter by autogenerated _id for mongodb
1 parent f7b0a94 commit 4118cce

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

adminforth/dataConnectors/mongo.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dayjs from 'dayjs';
22
import { MongoClient } from 'mongodb';
3-
import { Decimal128 } from 'bson';
3+
import { Decimal128, ObjectId } from 'bson';
44
import { IAdminForthDataSourceConnector, IAdminForthSingleFilter, IAdminForthAndOrFilter, AdminForthResource } from '../types/Back.js';
55
import AdminForthBaseConnector from './baseConnector.js';
66

@@ -64,14 +64,6 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
6464
}, {});
6565
}
6666

67-
getPrimaryKey(resource) {
68-
for (const col of resource.dataSourceColumns) {
69-
if (col.primaryKey) {
70-
return col.name;
71-
}
72-
}
73-
}
74-
7567
getFieldValue(field, value) {
7668
if (field.type == AdminForthDataTypes.DATETIME) {
7769
if (!value) {
@@ -89,6 +81,9 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
8981
return !!value;
9082
} else if (field.type == AdminForthDataTypes.DECIMAL) {
9183
return value?.toString();
84+
} else if (field.name === '_id' && !field.fillOnCreate) {
85+
// if "_id" was created by mongo it will be ObjectId
86+
return value?.toString();
9287
}
9388

9489
return value;
@@ -111,6 +106,9 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
111106
return value ? true : false;
112107
} else if (field.type == AdminForthDataTypes.DECIMAL) {
113108
return Decimal128.fromString(value?.toString());
109+
} else if (field.name === '_id' && !field.fillOnCreate) {
110+
// if "_id" was created by mongo it supposed to be saved as ObjectId
111+
return ObjectId.createFromHexString(value);
114112
}
115113
return value;
116114
}

0 commit comments

Comments
 (0)