Skip to content

Commit

Permalink
experimenting with find API v2
Browse files Browse the repository at this point in the history
  • Loading branch information
VenomAV committed Jul 12, 2024
1 parent b3ada1c commit 59e5411
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Effect from "effect/Effect"
import * as F from "effect/Function"
import * as Stream from "effect/Stream"
import type { Collection, Document, Filter, FindOptions } from "mongodb"
import * as FindCursor from "./FindCursor.js"
import * as MongoError from "./MongoError.js"

export const find: {
Expand All @@ -30,6 +31,15 @@ export const find: {
Stream.catchAll(MongoError.mongoErrorDie<T>("find error"))
))

export const findV2 = <T extends Document = Document>(
collection: Collection<T>
): FindCursor.FindCursor =>
new FindCursor.FindCursor(
{
cursor: collection.find()
}
)

export const toArray = F.flow(
Stream.runCollect,
Effect.map(Chunk.toReadonlyArray)
Expand Down
18 changes: 18 additions & 0 deletions src/FindCursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type * as Schema from "@effect/schema/Schema"
import * as Data from "effect/Data"
import * as Effect from "effect/Effect"
import type { FindCursor as FindCursor_ } from "mongodb"
import * as TypedFindCursor from "./TypedFindCursor.js"

export class FindCursor extends Data.TaggedClass("FindCursor")<{
cursor: FindCursor_<unknown>
}> {}

export const toArray = (cursor: FindCursor): Effect.Effect<ReadonlyArray<unknown>> =>
Effect.promise(() => cursor.cursor.toArray())

export const typed = <A, I = A, R = never>(
cursor: FindCursor,
schema: Schema.Schema<A, I, R>
): TypedFindCursor.TypedFindCursor<A, I, R> =>
new TypedFindCursor.TypedFindCursor<A, I, R>({ cursor: cursor.cursor, schema })
14 changes: 14 additions & 0 deletions src/TypedFindCursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as Schema from "@effect/schema/Schema"
import * as Data from "effect/Data"
import * as Effect from "effect/Effect"
import type { FindCursor as FindCursor_ } from "mongodb"

export class TypedFindCursor<A, I = A, R = never> extends Data.TaggedClass("TypedFindCursor")<{
cursor: FindCursor_<unknown>
schema: Schema.Schema<A, I, R>
}> {}

export const toArray = <A, I = A, R = never>(cursor: TypedFindCursor<A, I, R>) => {
const decode = Schema.decodeUnknown(cursor.schema)
return Effect.promise(() => cursor.cursor.toArray()).pipe(Effect.flatMap(Effect.forEach((x) => decode(x))))
}
16 changes: 15 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
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 MongoClient from "@doubleloop-io/effect-mongodb/MongoClient"
import * as TypedFindCursor from "@doubleloop-io/effect-mongodb/TypedFindCursor"
import * as Schema from "@effect/schema/Schema"
import * as Effect from "effect/Effect"
import * as F from "effect/Function"
import { expect, test } from "vitest"

test("should work", () => {
test("find", () => {
const foo = MongoClient.connect("mongodb://localhost:27017").pipe(
Effect.flatMap(MongoClient.db("foo")),
Effect.flatMap(Db.collection("bar")),
Effect.flatMap(F.flow(Collection.find(), Collection.toArray))
)
expect(foo).toBeDefined()
})

test("find with projection", () => {
const foo = MongoClient.connect("mongodb://localhost:27017").pipe(
Effect.flatMap(MongoClient.db("foo")),
Effect.flatMap(Db.collection("bar")),
Effect.map(Collection.findV2),
Effect.map((x) => FindCursor.typed(x, Schema.Struct({ foo: Schema.String }))),
Effect.flatMap(TypedFindCursor.toArray)
)
expect(foo).toBeDefined()
})

0 comments on commit 59e5411

Please sign in to comment.