Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions src/entity-manager/MongoEntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
BulkWriteResult,
ChangeStream,
ChangeStreamOptions,
Collection,
CollStats,
CollStatsOptions,
CommandOperationOptions,
CountDocumentsOptions,
CountOptions,
IndexSpecification,
CreateIndexesOptions,
IndexDescription,
DeleteResult as DeleteResultMongoDb,
DeleteOptions,
CommandOperationOptions,
DeleteResult as DeleteResultMongoDb,
Document,
Filter,
FilterOperators,
FindCursor,
FindOneAndDeleteOptions,
FindOneAndReplaceOptions,
UpdateFilter,
FindOneAndUpdateOptions,
RenameOptions,
ReplaceOptions,
UpdateResult as UpdateResultMongoDb,
CollStats,
CollStatsOptions,
ChangeStreamOptions,
ChangeStream,
UpdateOptions,
ListIndexesOptions,
ListIndexesCursor,
OptionalId,
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"
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Expand Down
Loading