Skip to content

Commit

Permalink
Collection+DocumentCollection: add replaceOne
Browse files Browse the repository at this point in the history
  • Loading branch information
devmatteini committed Nov 11, 2024
1 parent 225601b commit 7ec8746
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
38 changes: 38 additions & 0 deletions packages/effect-mongodb/src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import type {
InsertManyResult,
InsertOneOptions,
InsertOneResult,
ReplaceOptions,
UpdateFilter,
UpdateOptions,
UpdateResult
Expand Down Expand Up @@ -207,4 +208,41 @@ export const updateMany: {
)
)

export const replaceOne: {
<A extends Document, I extends Document>(
filter: Filter<I>,
// TODO: should we put WithoutId<A> here like the driver signature?
replacement: A,
options?: ReplaceOptions
): <R>(
collection: Collection<A, I, R>
) => Effect.Effect<UpdateResult | Document, MongoError.MongoError | ParseResult.ParseError, R>
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
filter: Filter<I>,
replacement: A,
options?: ReplaceOptions
): Effect.Effect<UpdateResult | Document, MongoError.MongoError | ParseResult.ParseError, R>
} = F.dual(
(args) => isCollection(args[0]),
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
filter: Filter<I>,
replacement: A,
options?: ReplaceOptions
): Effect.Effect<
UpdateResult | Document,
MongoError.MongoError | ParseResult.ParseError,
R
> =>
F.pipe(
// TODO: extract function in Collection
Schema.encode(collection.schema)(replacement),
Effect.flatMap((replacement) =>
Effect.promise(() => collection.collection.replaceOne(filter, replacement, options))
),
Effect.catchAllDefect(MongoError.mongoErrorDie<UpdateResult | Document>("replaceOne error"))
)
)

const isCollection = (x: unknown) => x instanceof Collection
32 changes: 31 additions & 1 deletion packages/effect-mongodb/src/DocumentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import type {
InsertOneOptions,
InsertOneResult,
OptionalUnlessRequiredId,
ReplaceOptions,
UpdateFilter,
UpdateOptions,
UpdateResult
UpdateResult,
WithoutId
} from "mongodb"
import * as Collection from "./Collection.js"
import * as DocumentFindCursor from "./DocumentFindCursor.js"
Expand Down Expand Up @@ -195,6 +197,34 @@ export const updateMany: {
)
)

export const replaceOne: {
(
filter: Filter<Document>,
replacement: WithoutId<Document>,
options?: ReplaceOptions
): (
collection: DocumentCollection
) => Effect.Effect<UpdateResult | Document, MongoError.MongoError>
(
collection: DocumentCollection,
filter: Filter<Document>,
replacement: WithoutId<Document>,
options?: ReplaceOptions
): Effect.Effect<UpdateResult | Document, MongoError.MongoError>
} = F.dual(
(args) => isDocumentCollection(args[0]),
(
collection: DocumentCollection,
filter: Filter<Document>,
replacement: WithoutId<Document>,
options?: ReplaceOptions
): Effect.Effect<UpdateResult | Document, MongoError.MongoError> =>
F.pipe(
Effect.promise(() => collection.collection.replaceOne(filter, replacement, options)),
Effect.catchAllDefect(MongoError.mongoErrorDie<UpdateResult | Document>("replaceOne error"))
)
)

export const typed: {
<A extends Document, I extends Document = A, R = never>(
schema: Schema.Schema<A, I, R>
Expand Down

0 comments on commit 7ec8746

Please sign in to comment.