-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |