From bdd144b2abbc002eb3178c87134fac9c973f20eb Mon Sep 17 00:00:00 2001 From: Cosimo Matteini Date: Mon, 11 Nov 2024 15:20:41 +0100 Subject: [PATCH] Collection+DocumentCollection: add updateMany --- packages/effect-mongodb/src/Collection.ts | 34 ++++++++++++++++++- .../effect-mongodb/src/DocumentCollection.ts | 33 +++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) 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