11import dayjs from 'dayjs' ;
22import { MongoClient } from 'mongodb' ;
3- import { Decimal128 } from 'bson' ;
3+ import { Decimal128 , ObjectId } from 'bson' ;
44import { IAdminForthDataSourceConnector , IAdminForthSingleFilter , IAdminForthAndOrFilter , AdminForthResource } from '../types/Back.js' ;
55import 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