Skip to content

Commit

Permalink
DocumentCollection+Collection: add optional filter parameter to find …
Browse files Browse the repository at this point in the history
…function (close #4)
  • Loading branch information
devmatteini committed Nov 12, 2024
1 parent 74e18f7 commit 6f0a8d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/shiny-tables-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"effect-mongodb": patch
---

Use Filter type in FindCursor and DocumentFindCursor filter function.
Add optional filter parameter to find functions in Collection and DocumentCollection
9 changes: 6 additions & 3 deletions packages/effect-mongodb/src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ export class Collection<A extends Document, I extends Document = A, R = never> e
export type FindOptions = Omit<MongoFindOptions, "projection">

export const find: {
(
<I extends Document>(
filter?: Filter<I>,
options?: FindOptions
): <A extends Document, I extends Document, R>(collection: Collection<A, I, R>) => FindCursor.FindCursor<A, I, R>
): <A extends Document, R>(collection: Collection<A, I, R>) => FindCursor.FindCursor<A, I, R>
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
filter?: Filter<I>,
options?: FindOptions
): FindCursor.FindCursor<A, I, R>
} = F.dual(
(args) => isCollection(args[0]),
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
filter?: Filter<I>,
options?: FindOptions
): FindCursor.FindCursor<A, I, R> =>
new FindCursor.FindCursor<A, I, R>({
cursor: collection.collection.find({}, options),
cursor: collection.collection.find(filter ?? {}, options),
schema: collection.schema
})
)
Expand Down
10 changes: 7 additions & 3 deletions packages/effect-mongodb/src/DocumentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,21 @@ export class DocumentCollection extends Data.TaggedClass("DocumentCollection")<{
}

export const find: {
(options?: FindOptions): (collection: DocumentCollection) => DocumentFindCursor.DocumentFindCursor
(
filter?: Filter<Document>,
options?: FindOptions
): (collection: DocumentCollection) => DocumentFindCursor.DocumentFindCursor
(
collection: DocumentCollection,
filter?: Filter<Document>,
options?: FindOptions
): DocumentFindCursor.DocumentFindCursor
} = F.dual(
(args) => isDocumentCollection(args[0]),
(collection: DocumentCollection, options?: FindOptions) =>
(collection: DocumentCollection, filter?: Filter<Document>, options?: FindOptions) =>
new DocumentFindCursor.DocumentFindCursor(
{
cursor: collection.collection.find({}, options)
cursor: collection.collection.find(filter ?? {}, options)
}
)
)
Expand Down

0 comments on commit 6f0a8d4

Please sign in to comment.