diff --git a/packages/effect-mongodb/src/Collection.ts b/packages/effect-mongodb/src/Collection.ts index df348f9..ca19e9c 100644 --- a/packages/effect-mongodb/src/Collection.ts +++ b/packages/effect-mongodb/src/Collection.ts @@ -17,6 +17,7 @@ import type { InsertManyResult, InsertOneOptions, InsertOneResult, + ReplaceOptions, UpdateFilter, UpdateOptions, UpdateResult @@ -207,4 +208,41 @@ export const updateMany: { ) ) +export const replaceOne: { + ( + filter: Filter, + // TODO: should we put WithoutId here like the driver signature? + replacement: A, + options?: ReplaceOptions + ): ( + collection: Collection + ) => Effect.Effect + ( + collection: Collection, + filter: Filter, + replacement: A, + options?: ReplaceOptions + ): Effect.Effect +} = F.dual( + (args) => isCollection(args[0]), + ( + collection: Collection, + filter: Filter, + 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("replaceOne error")) + ) +) + const isCollection = (x: unknown) => x instanceof Collection diff --git a/packages/effect-mongodb/src/DocumentCollection.ts b/packages/effect-mongodb/src/DocumentCollection.ts index 391954c..551005c 100644 --- a/packages/effect-mongodb/src/DocumentCollection.ts +++ b/packages/effect-mongodb/src/DocumentCollection.ts @@ -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" @@ -195,6 +197,34 @@ export const updateMany: { ) ) +export const replaceOne: { + ( + filter: Filter, + replacement: WithoutId, + options?: ReplaceOptions + ): ( + collection: DocumentCollection + ) => Effect.Effect + ( + collection: DocumentCollection, + filter: Filter, + replacement: WithoutId, + options?: ReplaceOptions + ): Effect.Effect +} = F.dual( + (args) => isDocumentCollection(args[0]), + ( + collection: DocumentCollection, + filter: Filter, + replacement: WithoutId, + options?: ReplaceOptions + ): Effect.Effect => + F.pipe( + Effect.promise(() => collection.collection.replaceOne(filter, replacement, options)), + Effect.catchAllDefect(MongoError.mongoErrorDie("replaceOne error")) + ) +) + export const typed: { ( schema: Schema.Schema