diff --git a/packages/effect-mongodb/src/Collection.ts b/packages/effect-mongodb/src/Collection.ts index c0a5a49..df348f9 100644 --- a/packages/effect-mongodb/src/Collection.ts +++ b/packages/effect-mongodb/src/Collection.ts @@ -16,7 +16,10 @@ import type { FindOptions as MongoFindOptions, InsertManyResult, InsertOneOptions, - InsertOneResult + InsertOneResult, + UpdateFilter, + UpdateOptions, + UpdateResult } from "mongodb" import * as FindCursor from "./FindCursor.js" import type { Filter } from "./internal/filter.js" @@ -175,4 +178,33 @@ export const deleteMany: { ) ) +export const updateMany: { + ( + filter: Filter, + update: UpdateFilter | Array, + options?: UpdateOptions + ): ( + collection: Collection + ) => Effect.Effect + ( + collection: Collection, + filter: Filter, + update: UpdateFilter | Array, + options?: UpdateOptions + ): Effect.Effect +} = F.dual( + (args) => isCollection(args[0]), + ( + collection: Collection, + filter: Filter, + update: UpdateFilter | Array, + options?: UpdateOptions + ): Effect.Effect => + Effect.promise(() => + collection.collection.updateMany(filter, update as UpdateFilter | Array, options) + ).pipe( + Effect.catchAllDefect(MongoError.mongoErrorDie("updateMany 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 3a45428..391954c 100644 --- a/packages/effect-mongodb/src/DocumentCollection.ts +++ b/packages/effect-mongodb/src/DocumentCollection.ts @@ -17,7 +17,10 @@ import type { InsertManyResult, InsertOneOptions, InsertOneResult, - OptionalUnlessRequiredId + OptionalUnlessRequiredId, + UpdateFilter, + UpdateOptions, + UpdateResult } from "mongodb" import * as Collection from "./Collection.js" import * as DocumentFindCursor from "./DocumentFindCursor.js" @@ -164,6 +167,34 @@ export const deleteMany: { ) ) +export const updateMany: { + ( + filter: Filter, + update: UpdateFilter | Array, + options?: UpdateOptions + ): ( + collection: DocumentCollection + ) => Effect.Effect + ( + collection: DocumentCollection, + filter: Filter, + update: UpdateFilter | Array, + options?: UpdateOptions + ): Effect.Effect +} = F.dual( + (args) => isDocumentCollection(args[0]), + ( + collection: DocumentCollection, + filter: Filter, + update: UpdateFilter | Array, + options?: UpdateOptions + ): Effect.Effect => + F.pipe( + Effect.promise(() => collection.collection.updateMany(filter, update, options)), + Effect.catchAllDefect(MongoError.mongoErrorDie("updateMany error")) + ) +) + export const typed: { ( schema: Schema.Schema