(
entityName: EntityName<Entity>,
where: FilterQuery<NoInfer<Entity>>,
options: FindOneOptions<Entity, Hint, Fields, Excludes> = {},
)
| 1035 | ): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>; |
| 1036 | |
| 1037 | async findOne< |
| 1038 | Entity extends object, |
| 1039 | Hint extends string = never, |
| 1040 | Fields extends string = never, |
| 1041 | Excludes extends string = never, |
| 1042 | >( |
| 1043 | entityName: EntityName<Entity>, |
| 1044 | where: FilterQuery<NoInfer<Entity>>, |
| 1045 | options: FindOneOptions<Entity, Hint, Fields, Excludes> = {}, |
| 1046 | ): Promise<Loaded<Entity, Hint, Fields, Excludes> | null> { |
| 1047 | if (options.disableIdentityMap ?? this.config.get('disableIdentityMap')) { |
| 1048 | const em = this.getContext(false); |
| 1049 | const fork = em.fork({ keepTransactionContext: true }); |
| 1050 | const ret = await fork.findOne(entityName, where as any, { ...options, disableIdentityMap: false }); |
| 1051 | fork.clear(); |
| 1052 | |
| 1053 | return ret; |
| 1054 | } |
| 1055 | |
| 1056 | const em = this.getContext(); |
| 1057 | em.prepareOptions(options); |
| 1058 | let entity = em.#unitOfWork.tryGetById(entityName, where, options.schema); |
| 1059 | |
| 1060 | // query for a not managed entity which is already in the identity map as it |
| 1061 | // was provided with a PK this entity does not exist in the db, there can't |
| 1062 | // be any relations to it, so no need to deal with the populate hint |
| 1063 | if (entity && !helper(entity).__managed) { |
| 1064 | return entity as Loaded<Entity, Hint, Fields, Excludes>; |
| 1065 | } |
| 1066 | |
| 1067 | await em.tryFlush(entityName, options); |
| 1068 | const meta = em.metadata.get<Entity>(entityName); |
| 1069 | em.validateIndexUsage(meta, where, options); |
| 1070 | where = await em.processWhere(entityName, where, options, 'read'); |
| 1071 | validateEmptyWhere(where); |
| 1072 | em.checkLockRequirements(options.lockMode, meta); |
| 1073 | const isOptimisticLocking = |
| 1074 | options.lockMode == null || options.lockMode === LockMode.NONE || options.lockMode === LockMode.OPTIMISTIC; |
| 1075 | |
| 1076 | if (entity && !em.shouldRefresh(meta, entity, options) && isOptimisticLocking) { |
| 1077 | return em.lockAndPopulate(meta, entity, where, options); |
| 1078 | } |
| 1079 | |
| 1080 | validateParams(where); |
| 1081 | options.populate = (await em.preparePopulate(entityName, options)) as any; |
| 1082 | const cacheKey = em.cacheKey(entityName, options, 'em.findOne', where); |
| 1083 | const cached = await em.tryCache<Entity, Loaded<Entity, Hint, Fields, Excludes>>( |
| 1084 | entityName, |
| 1085 | options.cache, |
| 1086 | cacheKey, |
| 1087 | options.refresh, |
| 1088 | true, |
| 1089 | ); |
| 1090 | |
| 1091 | if (cached?.data !== undefined) { |
| 1092 | if (cached.data) { |
| 1093 | await em.#entityLoader.populate<Entity, Fields>( |
| 1094 | entityName, |
no test coverage detected