Skip to content

Commit

Permalink
Collection+DocumentCollection: add updateMany
Browse files Browse the repository at this point in the history
  • Loading branch information
devmatteini committed Nov 11, 2024
1 parent ba9c522 commit bdd144b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
34 changes: 33 additions & 1 deletion packages/effect-mongodb/src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -175,4 +178,33 @@ export const deleteMany: {
)
)

export const updateMany: {
<I extends Document>(
filter: Filter<I>,
update: UpdateFilter<I> | Array<Document>,
options?: UpdateOptions
): <A extends Document, R>(
collection: Collection<A, I, R>
) => Effect.Effect<UpdateResult, MongoError.MongoError, R>
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
filter: Filter<I>,
update: UpdateFilter<I> | Array<Document>,
options?: UpdateOptions
): Effect.Effect<UpdateResult, MongoError.MongoError, R>
} = F.dual(
(args) => isCollection(args[0]),
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
filter: Filter<I>,
update: UpdateFilter<I> | Array<Document>,
options?: UpdateOptions
): Effect.Effect<UpdateResult, MongoError.MongoError, R> =>
Effect.promise(() =>
collection.collection.updateMany(filter, update as UpdateFilter<Document> | Array<Document>, options)
).pipe(
Effect.catchAllDefect(MongoError.mongoErrorDie<UpdateResult>("updateMany error"))
)
)

const isCollection = (x: unknown) => x instanceof Collection
33 changes: 32 additions & 1 deletion packages/effect-mongodb/src/DocumentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -164,6 +167,34 @@ export const deleteMany: {
)
)

export const updateMany: {
(
filter: Filter<Document>,
update: UpdateFilter<Document> | Array<Document>,
options?: UpdateOptions
): (
collection: DocumentCollection
) => Effect.Effect<UpdateResult, MongoError.MongoError>
(
collection: DocumentCollection,
filter: Filter<Document>,
update: UpdateFilter<Document> | Array<Document>,
options?: UpdateOptions
): Effect.Effect<UpdateResult, MongoError.MongoError>
} = F.dual(
(args) => isDocumentCollection(args[0]),
(
collection: DocumentCollection,
filter: Filter<Document>,
update: UpdateFilter<Document> | Array<Document>,
options?: UpdateOptions
): Effect.Effect<UpdateResult, MongoError.MongoError> =>
F.pipe(
Effect.promise(() => collection.collection.updateMany(filter, update, options)),
Effect.catchAllDefect(MongoError.mongoErrorDie<UpdateResult>("updateMany 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 bdd144b

Please sign in to comment.