Skip to content

Commit 6cc9c61

Browse files
committed
DocumentCollection+Collection: add createIndex
1 parent d50d85d commit 6cc9c61

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

.changeset/dull-icons-nail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect-mongodb": patch
3+
---
4+
5+
Add createIndex function in Collection and DocumentCollection

packages/effect-mongodb/src/Collection.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
DropCollectionOptions,
1919
FindOptions as MongoFindOptions,
2020
IndexDescription,
21+
IndexSpecification,
2122
InsertManyResult,
2223
InsertOneOptions,
2324
InsertOneResult,
@@ -327,6 +328,31 @@ export const createIndexes: {
327328
)
328329
)
329330

331+
export const createIndex: {
332+
(
333+
indexSpec: IndexSpecification,
334+
options?: CreateIndexesOptions
335+
): <A extends Document, I extends Document, R>(
336+
collection: Collection<A, I, R>
337+
) => Effect.Effect<string, MongoError.MongoError, R>
338+
<A extends Document, I extends Document, R>(
339+
collection: Collection<A, I, R>,
340+
indexSpec: IndexSpecification,
341+
options?: CreateIndexesOptions
342+
): Effect.Effect<string, MongoError.MongoError, R>
343+
} = F.dual(
344+
(args) => isCollection(args[0]),
345+
<A extends Document, I extends Document, R>(
346+
collection: Collection<A, I, R>,
347+
indexSpec: IndexSpecification,
348+
options?: CreateIndexesOptions
349+
): Effect.Effect<string, MongoError.MongoError, R> =>
350+
F.pipe(
351+
Effect.promise(() => collection.collection.createIndex(indexSpec, options)),
352+
Effect.catchAllDefect(MongoError.mongoErrorDie<string>("createIndex error"))
353+
)
354+
)
355+
330356
export const aggregate: {
331357
<B extends Document, BI extends Document, BR>(
332358
pipeline: Array<Document>,

packages/effect-mongodb/src/DocumentCollection.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
Filter,
1919
FindOptions,
2020
IndexDescription,
21+
IndexSpecification,
2122
InsertManyResult,
2223
InsertOneOptions,
2324
InsertOneResult,
@@ -300,6 +301,29 @@ export const createIndexes: {
300301
)
301302
)
302303

304+
export const createIndex: {
305+
(
306+
indexSpec: IndexSpecification,
307+
options?: CreateIndexesOptions
308+
): (collection: DocumentCollection) => Effect.Effect<string, MongoError.MongoError>
309+
(
310+
collection: DocumentCollection,
311+
indexSpec: IndexSpecification,
312+
options?: CreateIndexesOptions
313+
): Effect.Effect<string, MongoError.MongoError>
314+
} = F.dual(
315+
(args) => isDocumentCollection(args[0]),
316+
(
317+
collection: DocumentCollection,
318+
indexSpec: IndexSpecification,
319+
options?: CreateIndexesOptions
320+
): Effect.Effect<string, MongoError.MongoError> =>
321+
F.pipe(
322+
Effect.promise(() => collection.collection.createIndex(indexSpec, options)),
323+
Effect.catchAllDefect(MongoError.mongoErrorDie<string>("createIndex error"))
324+
)
325+
)
326+
303327
export const aggregate: {
304328
(
305329
pipeline?: Array<Document>,

0 commit comments

Comments
 (0)