-
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.
Collection+DocumentCollection: add aggregate
- Loading branch information
1 parent
efa6c05
commit b00bc03
Showing
7 changed files
with
197 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @since 0.0.1 | ||
*/ | ||
import type * as ParseResult from "@effect/schema/ParseResult" | ||
import * as Schema from "@effect/schema/Schema" | ||
import * as Data from "effect/Data" | ||
import * as Effect from "effect/Effect" | ||
import * as F from "effect/Function" | ||
import * as Stream from "effect/Stream" | ||
import type { AggregationCursor as MongoAggregationCursor } from "mongodb" | ||
import * as MongoError from "./MongoError.js" | ||
|
||
export class AggregationCursor<A, I = A, R = never> extends Data.TaggedClass("AggregationCursor")< | ||
{ cursor: MongoAggregationCursor<unknown>; schema: Schema.Schema<A, I, R> } | ||
> { | ||
} | ||
|
||
export const toArray = <A, I, R>( | ||
cursor: AggregationCursor<A, I, R> | ||
): Effect.Effect<ReadonlyArray<A>, MongoError.MongoError | ParseResult.ParseError, R> => { | ||
const decode = Schema.decodeUnknown(cursor.schema) | ||
return Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }).pipe( | ||
Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<A>>("Unable to get array from mongodb aggregate cursor")), | ||
Effect.flatMap(Effect.forEach((x) => decode(x))) | ||
) | ||
} | ||
|
||
export const toStream = <A, I, R>( | ||
cursor: AggregationCursor<A, I, R> | ||
): Stream.Stream<A, MongoError.MongoError | ParseResult.ParseError, R> => { | ||
const decode = Schema.decodeUnknown(cursor.schema) | ||
return F.pipe( | ||
Stream.fromAsyncIterable(cursor.cursor, F.identity), | ||
Stream.catchAll(MongoError.mongoErrorStream<A>("Unable to get stream from mongodb aggregate cursor")), | ||
Stream.mapEffect((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
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,30 @@ | ||
/** | ||
* @since 0.0.1 | ||
*/ | ||
import * as Data from "effect/Data" | ||
import * as Effect from "effect/Effect" | ||
import * as F from "effect/Function" | ||
import * as Stream from "effect/Stream" | ||
import type { AggregationCursor as MongoAggregationCursor } from "mongodb" | ||
import * as MongoError from "./MongoError.js" | ||
|
||
export class DocumentAggregationCursor | ||
extends Data.TaggedClass("DocumentAggregationCursor")<{ cursor: MongoAggregationCursor<Document> }> | ||
{ | ||
} | ||
|
||
export const toArray = ( | ||
cursor: DocumentAggregationCursor | ||
): Effect.Effect<ReadonlyArray<Document>, MongoError.MongoError> => | ||
F.pipe( | ||
Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }), | ||
Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<Document>>(`${cursor._tag}.toArray error`)) | ||
) | ||
|
||
export const toStream = ( | ||
cursor: DocumentAggregationCursor | ||
): Stream.Stream<Document, MongoError.MongoError> => | ||
F.pipe( | ||
Stream.fromAsyncIterable(cursor.cursor, F.identity), | ||
Stream.catchAll(MongoError.mongoErrorStream<Document>(`${cursor._tag}.toStream error`)) | ||
) |
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
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