fix(mongodb): add missing findBy method to MongoEntityManager (#11814)

This commit is contained in:
CHOIJEWON 2025-12-03 00:35:42 +09:00 committed by GitHub
parent ec3ea10b44
commit 38715bbd41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 75 additions and 32 deletions

View File

@ -15,46 +15,46 @@ import { DeleteResult } from "../query-builder/result/DeleteResult"
import { EntityMetadata } from "../metadata/EntityMetadata"
import {
BulkWriteResult,
AggregationCursor,
Collection,
FindCursor,
Document,
AggregateOptions,
AggregationCursor,
AnyBulkWriteOperation,
BulkWriteOptions,
Filter,
CountOptions,
IndexSpecification,
CreateIndexesOptions,
IndexDescription,
DeleteResult as DeleteResultMongoDb,
DeleteOptions,
CommandOperationOptions,
FindOneAndDeleteOptions,
FindOneAndReplaceOptions,
UpdateFilter,
FindOneAndUpdateOptions,
RenameOptions,
ReplaceOptions,
UpdateResult as UpdateResultMongoDb,
BulkWriteResult,
ChangeStream,
ChangeStreamOptions,
Collection,
CollStats,
CollStatsOptions,
ChangeStreamOptions,
ChangeStream,
UpdateOptions,
ListIndexesOptions,
ListIndexesCursor,
OptionalId,
CommandOperationOptions,
CountDocumentsOptions,
CountOptions,
CreateIndexesOptions,
DeleteOptions,
DeleteResult as DeleteResultMongoDb,
Document,
Filter,
FilterOperators,
FindCursor,
FindOneAndDeleteOptions,
FindOneAndReplaceOptions,
FindOneAndUpdateOptions,
IndexDescription,
IndexInformationOptions,
IndexSpecification,
InsertManyResult,
InsertOneOptions,
InsertOneResult,
InsertManyResult,
UnorderedBulkOperation,
OrderedBulkOperation,
IndexInformationOptions,
ListIndexesCursor,
ListIndexesOptions,
ObjectId,
FilterOperators,
CountDocumentsOptions,
OptionalId,
OrderedBulkOperation,
RenameOptions,
ReplaceOptions,
UnorderedBulkOperation,
UpdateFilter,
UpdateOptions,
UpdateResult as UpdateResultMongoDb,
} from "../driver/mongodb/typings"
import { DataSource } from "../data-source/DataSource"
import { MongoFindManyOptions } from "../find-options/mongodb/MongoFindManyOptions"
@ -161,6 +161,16 @@ export class MongoEntityManager extends EntityManager {
return this.executeFindAndCount(entityClassOrName, where)
}
/**
* Finds entities that match given WHERE conditions.
*/
async findBy<Entity>(
entityClassOrName: EntityTarget<Entity>,
where: any,
): Promise<Entity[]> {
return this.executeFind(entityClassOrName, where)
}
/**
* Finds entities by ids.
* Optionally find options can be applied.

View File

@ -265,6 +265,39 @@ describe("mongodb > MongoRepository", () => {
))
})
})
it("should be able to use findBy method", () =>
Promise.all(
connections.map(async (connection) => {
const postRepository = connection.getMongoRepository(Post)
// save few posts
const firstPost = new Post()
firstPost.title = "Post #1"
firstPost.text = "Everything about post #1"
await postRepository.save(firstPost)
const secondPost = new Post()
secondPost.title = "Post #1"
secondPost.text = "Everything about post #2"
await postRepository.save(secondPost)
const thirdPost = new Post()
thirdPost.title = "Post #2"
thirdPost.text = "Everything about post #3"
await postRepository.save(thirdPost)
const loadedPosts = await postRepository.findBy({
title: "Post #1",
})
expect(loadedPosts).to.have.length(2)
expect(loadedPosts[0]).to.be.instanceOf(Post)
expect(loadedPosts[1]).to.be.instanceOf(Post)
expect(loadedPosts[0].title).to.eql("Post #1")
expect(loadedPosts[1].title).to.eql("Post #1")
}),
))
})
async function seedPosts(postRepository: MongoRepository<PostWithDeleted>) {