import { ObjectId } from 'bson' ; import type { IPrimaryKey , Primary , NamingStrategy , Constructor , EntityRepository , EntityProperty , PopulateOptions , EntityMetadata } from '@mikro-orm/core' ; import { Platform , MongoNamingStrategy , Utils , ReferenceType , MetadataError } from '@mikro-orm/core' ; import { MongoExceptionConverter } from './MongoExceptionConverter' ; import { MongoEntityRepository } from './MongoEntityRepository' ; export class MongoPlatform extends Platform { protected readonly exceptionConverter = new MongoExceptionConverter ( ) ; getNamingStrategy ( ) { return MongoNamingStrategy ; } getRepositoryClass < T > ( ) { return MongoEntityRepository ; } normalizePrimaryKey < T extends number | string = number | string > ( data ) { if ( data instanceof ObjectId ) { return data . toHexString ( ) as T ; } return data as T ; } denormalizePrimaryKey ( data ) { return new ObjectId ( data ) ; } getSerializedPrimaryKeyField ( field ) { return 'id' ; } usesDifferentSerializedPrimaryKey ( ) { return true ; } usesImplicitTransactions ( ) { return false ; } convertsJsonAutomatically ( marshall = false ) { return true ; } marshallArray ( values ) { return values as unknown as string ; } cloneEmbeddable < T > ( data ) { const ret = super . cloneEmbeddable ( data ) ; Utils . dropUndefinedProperties ( ret ) ; return ret ; } shouldHaveColumn < T > ( prop , populate ) { if ( super . shouldHaveColumn ( prop , populate ) ) { return true ; } return prop . reference === ReferenceType . MANY_TO_MANY && prop . owner ; } validateMetadata ( meta ) { const pk = meta . getPrimaryProps ( ) [ 0 ] ; if ( pk && pk . fieldNames ?. [ 0 ] !== '_id' ) { throw MetadataError . invalidPrimaryKey ( meta , pk , '_id' ) ; } } }