Skip to content

Commit

Permalink
add Collection.findOne
Browse files Browse the repository at this point in the history
  • Loading branch information
devmatteini committed Jul 26, 2024
1 parent 6d4db49 commit 82871f6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as Chunk from "effect/Chunk"
import * as Effect from "effect/Effect"
import * as F from "effect/Function"
import * as O from "effect/Option"
import * as Stream from "effect/Stream"
import type {
BulkWriteOptions,
Expand Down Expand Up @@ -50,6 +51,32 @@ export const findV2 = <T extends Document = Document>(
}
)

export const findOne: {
<T extends Document = Document>(
filter: Filter<T>,
options?: FindOptions
): (
collection: Collection<T>
) => Effect.Effect<O.Option<T>, MongoError.MongoError>
<T extends Document = Document>(
collection: Collection<T>,
filter: Filter<T>,
options?: FindOptions
): Effect.Effect<O.Option<T>, MongoError.MongoError>
} = F.dual(
(args) => isCollection(args[0]),
<T extends Document = Document>(
collection: Collection<T>,
filter: Filter<T>,
options?: FindOptions
): Effect.Effect<O.Option<T>, MongoError.MongoError> =>
F.pipe(
Effect.promise(() => collection.findOne(filter, options)),
Effect.map((value) => O.fromNullable(value)),
Effect.catchAllDefect(MongoError.mongoErrorDie<O.Option<T>>("findOne error"))
)
)

export const insertOne: {
<T extends Document = Document>(
doc: OptionalUnlessRequiredId<T>,
Expand Down
35 changes: 35 additions & 0 deletions test/Collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Collection from "@doubleloop-io/effect-mongodb/Collection"
import * as Db from "@doubleloop-io/effect-mongodb/Db"
import * as FindCursor from "@doubleloop-io/effect-mongodb/FindCursor"
import * as Effect from "effect/Effect"
import * as O from "effect/Option"
import { ObjectId } from "mongodb"
import { expect, test } from "vitest"
import { describeMongo } from "./support/describe-mongo.js"
Expand Down Expand Up @@ -42,4 +43,38 @@ describeMongo("Collection", (ctx) => {
{ _id: expect.any(ObjectId), name: "NAME_3" }
])
})

test("find one", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.collection(db, "find-one"))

yield* _(
Collection.insertMany(collection, [{ name: "ANY_NAME_1" }, { name: "john" }, { name: "ANY_NAME_2" }])
)

return yield* _(Collection.findOne(collection, { name: "john" }))
})

const result = await Effect.runPromise(program)

expect(result).toEqual(O.some({ _id: expect.any(ObjectId), name: "john" }))
})

test("find one - no result", async () => {
const program = Effect.gen(function*(_) {
const db = yield* _(ctx.database)
const collection = yield* _(Db.collection(db, "find-one-no-result"))

yield* _(
Collection.insertMany(collection, [{ name: "ANY_NAME_1" }, { name: "ANY_NAME_2" }, { name: "ANY_NAME_2" }])
)

return yield* _(Collection.findOne(collection, { name: "john" }))
})

const result = await Effect.runPromise(program)

expect(result).toEqual(O.none())
})
})

0 comments on commit 82871f6

Please sign in to comment.