Skip to content

Commit

Permalink
Collection+DocumentCollection: add createIndexes
Browse files Browse the repository at this point in the history
  • Loading branch information
devmatteini committed Nov 11, 2024
1 parent a1c9463 commit efa6c05
Show file tree
Hide file tree
Showing 2 changed files with 54 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 @@ -10,10 +10,12 @@ import * as O from "effect/Option"
import type {
BulkWriteOptions,
Collection as MongoCollection,
CreateIndexesOptions,
DeleteOptions,
DeleteResult,
Document,
FindOptions as MongoFindOptions,
IndexDescription,
InsertManyResult,
InsertOneOptions,
InsertOneResult,
Expand Down Expand Up @@ -272,4 +274,29 @@ export const rename: {
)
)

export const createIndexes: {
(
indexSpecs: Array<IndexDescription>,
options?: CreateIndexesOptions
): <A extends Document, I extends Document, R>(
collection: Collection<A, I, R>
) => Effect.Effect<Array<string>, MongoError.MongoError, R>
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
indexSpecs: Array<IndexDescription>,
options?: CreateIndexesOptions
): Effect.Effect<Array<string>, MongoError.MongoError, R>
} = F.dual(
(args) => isCollection(args[0]),
<A extends Document, I extends Document, R>(
collection: Collection<A, I, R>,
indexSpecs: Array<IndexDescription>,
options?: CreateIndexesOptions
): Effect.Effect<Array<string>, MongoError.MongoError, R> =>
F.pipe(
Effect.promise(() => collection.collection.createIndexes(indexSpecs, options)),
Effect.catchAllDefect(MongoError.mongoErrorDie<Array<string>>("createIndexes error"))
)
)

const isCollection = (x: unknown) => x instanceof Collection
27 changes: 27 additions & 0 deletions packages/effect-mongodb/src/DocumentCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import * as O from "effect/Option"
import type {
BulkWriteOptions,
Collection as MongoCollection,
CreateIndexesOptions,
DeleteOptions,
DeleteResult,
Document,
Filter,
FindOptions,
IndexDescription,
InsertManyResult,
InsertOneOptions,
InsertOneResult,
Expand Down Expand Up @@ -251,6 +253,31 @@ export const rename: {
)
)

export const createIndexes: {
(
indexSpecs: Array<IndexDescription>,
options?: CreateIndexesOptions
): (
collection: DocumentCollection
) => Effect.Effect<Array<string>, MongoError.MongoError>
(
collection: DocumentCollection,
indexSpecs: Array<IndexDescription>,
options?: CreateIndexesOptions
): Effect.Effect<Array<string>, MongoError.MongoError>
} = F.dual(
(args) => isDocumentCollection(args[0]),
(
collection: DocumentCollection,
indexSpecs: Array<IndexDescription>,
options?: CreateIndexesOptions
): Effect.Effect<Array<string>, MongoError.MongoError> =>
F.pipe(
Effect.promise(() => collection.collection.createIndexes(indexSpecs, options)),
Effect.catchAllDefect(MongoError.mongoErrorDie<Array<string>>("createIndexes 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 efa6c05

Please sign in to comment.