mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fix(mongodb): add missing findBy method to MongoEntityManager (#11814)
This commit is contained in:
parent
ec3ea10b44
commit
38715bbd41
@ -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.
|
||||
|
||||
@ -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>) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user