Skip to content

Commit

Permalink
Collection+DocumentCollection: add rename
Browse files Browse the repository at this point in the history
  • Loading branch information
devmatteini committed Nov 11, 2024
1 parent 7ec8746 commit a1c9463
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
27 changes: 27 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,
RenameOptions,
ReplaceOptions,
UpdateFilter,
UpdateOptions,
Expand Down Expand Up @@ -245,4 +246,30 @@ export const replaceOne: {
)
)

export const rename: {
(
newName: string,
options?: RenameOptions
): <A extends Document, I extends Document, R>(
collection: Collection<A, I, R>
) => Effect.Effect<Collection<A, I, R>, MongoError.MongoError, R>
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
newName: string,
options?: RenameOptions
): Effect.Effect<Collection<A, I, R>, MongoError.MongoError, R>
} = F.dual(
(args) => isCollection(args[0]),
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
newName: string,
options?: RenameOptions
): Effect.Effect<Collection<A, I, R>, MongoError.MongoError, R> =>
F.pipe(
Effect.promise(() => collection.collection.rename(newName, options)),
Effect.map((newCollection) => new Collection<A, I, R>({ collection: newCollection, schema: collection.schema })),
Effect.catchAllDefect(MongoError.mongoErrorDie<Collection<A, I, R>>("rename error"))
)
)

const isCollection = (x: unknown) => x instanceof Collection
26 changes: 26 additions & 0 deletions packages/effect-mongodb/src/DocumentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
InsertOneOptions,
InsertOneResult,
OptionalUnlessRequiredId,
RenameOptions,
ReplaceOptions,
UpdateFilter,
UpdateOptions,
Expand Down Expand Up @@ -225,6 +226,31 @@ export const replaceOne: {
)
)

export const rename: {
(
newName: string,
options?: RenameOptions
): (
collection: DocumentCollection
) => Effect.Effect<MongoCollection, MongoError.MongoError>
(
collection: DocumentCollection,
newName: string,
options?: RenameOptions
): Effect.Effect<MongoCollection, MongoError.MongoError>
} = F.dual(
(args) => isDocumentCollection(args[0]),
(
collection: DocumentCollection,
newName: string,
options?: RenameOptions
): Effect.Effect<MongoCollection, MongoError.MongoError> =>
F.pipe(
Effect.promise(() => collection.collection.rename(newName, options)),
Effect.catchAllDefect(MongoError.mongoErrorDie<MongoCollection>("rename 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 a1c9463

Please sign in to comment.