effect-mongodb 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +80 -13
  2. package/dist/cjs/AggregationCursor.js.map +1 -1
  3. package/dist/cjs/Collection.js +11 -13
  4. package/dist/cjs/Collection.js.map +1 -1
  5. package/dist/cjs/DocumentAggregationCursor.js.map +1 -1
  6. package/dist/cjs/DocumentCollection.js +4 -3
  7. package/dist/cjs/DocumentCollection.js.map +1 -1
  8. package/dist/cjs/DocumentFindCursor.js.map +1 -1
  9. package/dist/cjs/FindCursor.js.map +1 -1
  10. package/dist/cjs/ListCollectionsCursor.js.map +1 -1
  11. package/dist/cjs/MongoClient.js +4 -1
  12. package/dist/cjs/MongoClient.js.map +1 -1
  13. package/dist/cjs/internal/version.js +1 -1
  14. package/dist/dts/AggregationCursor.d.ts +1 -1
  15. package/dist/dts/AggregationCursor.d.ts.map +1 -1
  16. package/dist/dts/Collection.d.ts +9 -8
  17. package/dist/dts/Collection.d.ts.map +1 -1
  18. package/dist/dts/DocumentAggregationCursor.d.ts +2 -2
  19. package/dist/dts/DocumentAggregationCursor.d.ts.map +1 -1
  20. package/dist/dts/DocumentCollection.d.ts +8 -8
  21. package/dist/dts/DocumentCollection.d.ts.map +1 -1
  22. package/dist/dts/DocumentFindCursor.d.ts +1 -1
  23. package/dist/dts/DocumentFindCursor.d.ts.map +1 -1
  24. package/dist/dts/FindCursor.d.ts.map +1 -1
  25. package/dist/dts/ListCollectionsCursor.d.ts +1 -1
  26. package/dist/dts/ListCollectionsCursor.d.ts.map +1 -1
  27. package/dist/dts/MongoClient.d.ts +8 -0
  28. package/dist/dts/MongoClient.d.ts.map +1 -1
  29. package/dist/esm/AggregationCursor.js.map +1 -1
  30. package/dist/esm/Collection.js +11 -13
  31. package/dist/esm/Collection.js.map +1 -1
  32. package/dist/esm/DocumentAggregationCursor.js.map +1 -1
  33. package/dist/esm/DocumentCollection.js +4 -3
  34. package/dist/esm/DocumentCollection.js.map +1 -1
  35. package/dist/esm/DocumentFindCursor.js.map +1 -1
  36. package/dist/esm/FindCursor.js.map +1 -1
  37. package/dist/esm/ListCollectionsCursor.js.map +1 -1
  38. package/dist/esm/MongoClient.js +2 -0
  39. package/dist/esm/MongoClient.js.map +1 -1
  40. package/dist/esm/internal/version.js +1 -1
  41. package/package.json +1 -1
  42. package/src/AggregationCursor.ts +2 -2
  43. package/src/Collection.ts +29 -25
  44. package/src/DocumentAggregationCursor.ts +3 -3
  45. package/src/DocumentCollection.ts +18 -15
  46. package/src/DocumentFindCursor.ts +2 -2
  47. package/src/FindCursor.ts +7 -3
  48. package/src/ListCollectionsCursor.ts +2 -2
  49. package/src/MongoClient.ts +21 -0
  50. package/src/internal/version.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "effect-mongodb",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A MongoDB toolkit for Effect",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -17,10 +17,10 @@ export class AggregationCursor<A, I = A, R = never> extends Data.TaggedClass("Ag
17
17
 
18
18
  export const toArray = <A, I, R>(
19
19
  cursor: AggregationCursor<A, I, R>
20
- ): Effect.Effect<ReadonlyArray<A>, MongoError.MongoError | ParseResult.ParseError, R> => {
20
+ ): Effect.Effect<Array<A>, MongoError.MongoError | ParseResult.ParseError, R> => {
21
21
  const decode = Schema.decodeUnknown(cursor.schema)
22
22
  return Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }).pipe(
23
- Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<A>>("Unable to get array from mongodb aggregate cursor")),
23
+ Effect.catchAll(MongoError.mongoErrorDie<Array<A>>("Unable to get array from mongodb aggregate cursor")),
24
24
  Effect.flatMap(Effect.forEach((x) => decode(x)))
25
25
  )
26
26
  }
package/src/Collection.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * @since 0.0.1
3
3
  */
4
+ import * as Array from "effect/Array"
4
5
  import * as Data from "effect/Data"
5
6
  import * as Effect from "effect/Effect"
6
7
  import * as F from "effect/Function"
@@ -44,6 +45,7 @@ export class Collection<A extends Document, I extends Document = A, R = never> e
44
45
  collection: MongoCollection
45
46
  schema: Schema.Schema<A, I, R>
46
47
  }> {
48
+ readonly encode = Schema.encode(this.schema)
47
49
  }
48
50
 
49
51
  export type FindOptions = Omit<MongoFindOptions, "projection">
@@ -110,29 +112,28 @@ export const insertOne: {
110
112
  options?: InsertOneOptions
111
113
  ): Effect.Effect<InsertOneResult, MongoError.MongoError | ParseResult.ParseError, R> =>
112
114
  F.pipe(
113
- Schema.encode(collection.schema)(doc),
115
+ collection.encode(doc),
114
116
  Effect.flatMap((doc) => Effect.promise(() => collection.collection.insertOne(doc, options))),
115
117
  Effect.catchAllDefect(MongoError.mongoErrorDie<InsertOneResult>("insertOne error"))
116
118
  ))
117
119
 
118
120
  export const insertMany: {
119
- <A extends Document>(docs: Array<A>, options?: BulkWriteOptions): <I extends Document, R>(
121
+ <A extends Document>(docs: ReadonlyArray<A>, options?: BulkWriteOptions): <I extends Document, R>(
120
122
  collection: Collection<A, I, R>
121
123
  ) => Effect.Effect<InsertManyResult, MongoError.MongoError | ParseResult.ParseError, R>
122
124
  <A extends Document, I extends Document, R>(
123
125
  collection: Collection<A, I, R>,
124
- docs: Array<A>,
126
+ docs: ReadonlyArray<A>,
125
127
  options?: BulkWriteOptions
126
128
  ): Effect.Effect<InsertManyResult, MongoError.MongoError | ParseResult.ParseError, R>
127
129
  } = F.dual((args) => isCollection(args[0]), <A extends Document, I extends Document, R>(
128
130
  collection: Collection<A, I, R>,
129
- docs: Array<A>,
131
+ docs: ReadonlyArray<A>,
130
132
  options?: BulkWriteOptions
131
133
  ): Effect.Effect<InsertManyResult, MongoError.MongoError | ParseResult.ParseError, R> => {
132
- const encode = Schema.encode(collection.schema)
133
134
  return F.pipe(
134
135
  docs,
135
- Effect.forEach((doc) => encode(doc)),
136
+ Effect.forEach((doc) => collection.encode(doc)),
136
137
  Effect.flatMap((docs) => Effect.promise(() => collection.collection.insertMany(docs, options))),
137
138
  Effect.catchAllDefect(MongoError.mongoErrorDie<InsertManyResult>("insertMany error"))
138
139
  )
@@ -183,7 +184,7 @@ export const deleteMany: {
183
184
  export const updateMany: {
184
185
  <I extends Document>(
185
186
  filter: Filter<I>,
186
- update: UpdateFilter<I> | Array<Document>,
187
+ update: UpdateFilter<I> | ReadonlyArray<Document>,
187
188
  options?: UpdateOptions
188
189
  ): <A extends Document, R>(
189
190
  collection: Collection<A, I, R>
@@ -191,7 +192,7 @@ export const updateMany: {
191
192
  <A extends Document, I extends Document, R>(
192
193
  collection: Collection<A, I, R>,
193
194
  filter: Filter<I>,
194
- update: UpdateFilter<I> | Array<Document>,
195
+ update: UpdateFilter<I> | ReadonlyArray<Document>,
195
196
  options?: UpdateOptions
196
197
  ): Effect.Effect<UpdateResult, MongoError.MongoError, R>
197
198
  } = F.dual(
@@ -199,14 +200,16 @@ export const updateMany: {
199
200
  <A extends Document, I extends Document, R>(
200
201
  collection: Collection<A, I, R>,
201
202
  filter: Filter<I>,
202
- update: UpdateFilter<I> | Array<Document>,
203
+ update: UpdateFilter<I> | ReadonlyArray<Document>,
203
204
  options?: UpdateOptions
204
205
  ): Effect.Effect<UpdateResult, MongoError.MongoError, R> =>
205
206
  Effect.promise(() =>
206
- collection.collection.updateMany(filter, update as UpdateFilter<Document> | Array<Document>, options)
207
- ).pipe(
208
- Effect.catchAllDefect(MongoError.mongoErrorDie<UpdateResult>("updateMany error"))
209
- )
207
+ collection.collection.updateMany(
208
+ filter,
209
+ Array.isArray(update) ? [...update] : update as UpdateFilter<Document>,
210
+ options
211
+ )
212
+ ).pipe(Effect.catchAllDefect(MongoError.mongoErrorDie<UpdateResult>("updateMany error")))
210
213
  )
211
214
 
212
215
  export const replaceOne: {
@@ -237,8 +240,7 @@ export const replaceOne: {
237
240
  R
238
241
  > =>
239
242
  F.pipe(
240
- // TODO: extract function in Collection
241
- Schema.encode(collection.schema)(replacement),
243
+ collection.encode(replacement),
242
244
  Effect.flatMap((replacement) =>
243
245
  Effect.promise(() => collection.collection.replaceOne(filter, replacement, options))
244
246
  ),
@@ -299,8 +301,7 @@ export const findOneAndReplace: {
299
301
  options?: FindOneAndReplaceOptions
300
302
  ): Effect.Effect<O.Option<A> | ModifyResult<A>, MongoError.MongoError | ParseResult.ParseError, R> =>
301
303
  F.pipe(
302
- // TODO: extract function in Collection
303
- Schema.encode(collection.schema)(replacement),
304
+ collection.encode(replacement),
304
305
  Effect.flatMap((replacement) =>
305
306
  Effect.promise(() => collection.collection.findOneAndReplace(filter, replacement, options ?? {}))
306
307
  ),
@@ -364,23 +365,26 @@ export const drop: {
364
365
  )
365
366
 
366
367
  export const createIndexes: {
367
- (indexSpecs: Array<IndexDescription>, options?: CreateIndexesOptions): <A extends Document, I extends Document, R>(
368
+ (
369
+ indexSpecs: ReadonlyArray<IndexDescription>,
370
+ options?: CreateIndexesOptions
371
+ ): <A extends Document, I extends Document, R>(
368
372
  collection: Collection<A, I, R>
369
373
  ) => Effect.Effect<Array<string>, MongoError.MongoError, R>
370
374
  <A extends Document, I extends Document, R>(
371
375
  collection: Collection<A, I, R>,
372
- indexSpecs: Array<IndexDescription>,
376
+ indexSpecs: ReadonlyArray<IndexDescription>,
373
377
  options?: CreateIndexesOptions
374
378
  ): Effect.Effect<Array<string>, MongoError.MongoError, R>
375
379
  } = F.dual(
376
380
  (args) => isCollection(args[0]),
377
381
  <A extends Document, I extends Document, R>(
378
382
  collection: Collection<A, I, R>,
379
- indexSpecs: Array<IndexDescription>,
383
+ indexSpecs: ReadonlyArray<IndexDescription>,
380
384
  options?: CreateIndexesOptions
381
385
  ): Effect.Effect<Array<string>, MongoError.MongoError, R> =>
382
386
  F.pipe(
383
- Effect.promise(() => collection.collection.createIndexes(indexSpecs, options)),
387
+ Effect.promise(() => collection.collection.createIndexes([...indexSpecs], options)),
384
388
  Effect.catchAllDefect(MongoError.mongoErrorDie<Array<string>>("createIndexes error"))
385
389
  )
386
390
  )
@@ -433,7 +437,7 @@ export const dropIndex: {
433
437
 
434
438
  export const aggregate: {
435
439
  <B extends Document, BI extends Document, BR>(
436
- pipeline: Array<Document>,
440
+ pipeline: ReadonlyArray<Document>,
437
441
  schema: Schema.Schema<B, BI, BR>,
438
442
  options?: AggregateOptions
439
443
  ): <A extends Document, I extends Document, R>(
@@ -441,7 +445,7 @@ export const aggregate: {
441
445
  ) => AggregationCursor.AggregationCursor<B, BI, BR>
442
446
  <A extends Document, I extends Document, R, B extends Document, BI extends Document, BR>(
443
447
  collection: Collection<A, I, R>,
444
- pipeline: Array<Document>,
448
+ pipeline: ReadonlyArray<Document>,
445
449
  schema: Schema.Schema<B, BI, BR>,
446
450
  options?: AggregateOptions
447
451
  ): AggregationCursor.AggregationCursor<B, BI, BR>
@@ -449,12 +453,12 @@ export const aggregate: {
449
453
  (args) => isCollection(args[0]),
450
454
  <A extends Document, I extends Document, R, B extends Document, BI extends Document, BR>(
451
455
  collection: Collection<A, I, R>,
452
- pipeline: Array<Document>,
456
+ pipeline: ReadonlyArray<Document>,
453
457
  schema: Schema.Schema<B, BI, BR>,
454
458
  options?: AggregateOptions
455
459
  ): AggregationCursor.AggregationCursor<B, BI, BR> =>
456
460
  new AggregationCursor.AggregationCursor<B, BI, BR>({
457
- cursor: collection.collection.aggregate(pipeline, options),
461
+ cursor: collection.collection.aggregate([...pipeline], options),
458
462
  schema
459
463
  })
460
464
  )
@@ -5,7 +5,7 @@ import * as Data from "effect/Data"
5
5
  import * as Effect from "effect/Effect"
6
6
  import * as F from "effect/Function"
7
7
  import * as Stream from "effect/Stream"
8
- import type { AggregationCursor as MongoAggregationCursor } from "mongodb"
8
+ import type { AggregationCursor as MongoAggregationCursor, Document } from "mongodb"
9
9
  import * as MongoError from "./MongoError.js"
10
10
 
11
11
  export class DocumentAggregationCursor
@@ -15,10 +15,10 @@ export class DocumentAggregationCursor
15
15
 
16
16
  export const toArray = (
17
17
  cursor: DocumentAggregationCursor
18
- ): Effect.Effect<ReadonlyArray<Document>, MongoError.MongoError> =>
18
+ ): Effect.Effect<Array<Document>, MongoError.MongoError> =>
19
19
  F.pipe(
20
20
  Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }),
21
- Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<Document>>(`${cursor._tag}.toArray error`))
21
+ Effect.catchAll(MongoError.mongoErrorDie<Array<Document>>(`${cursor._tag}.toArray error`))
22
22
  )
23
23
 
24
24
  export const toStream = (
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * @since 0.0.1
3
3
  */
4
+ import * as Array from "effect/Array"
4
5
  import * as Data from "effect/Data"
5
6
  import * as Effect from "effect/Effect"
6
7
  import * as F from "effect/Function"
@@ -110,19 +111,19 @@ export const insertOne: {
110
111
 
111
112
  export const insertMany: {
112
113
  (
113
- docs: Array<OptionalUnlessRequiredId<Document>>,
114
+ docs: ReadonlyArray<OptionalUnlessRequiredId<Document>>,
114
115
  options?: BulkWriteOptions
115
116
  ): (
116
117
  collection: DocumentCollection
117
118
  ) => Effect.Effect<InsertOneResult, MongoError.MongoError>
118
119
  (
119
120
  collection: DocumentCollection,
120
- docs: Array<OptionalUnlessRequiredId<Document>>,
121
+ docs: ReadonlyArray<OptionalUnlessRequiredId<Document>>,
121
122
  options?: BulkWriteOptions
122
123
  ): Effect.Effect<InsertOneResult, MongoError.MongoError>
123
124
  } = F.dual((args) => isDocumentCollection(args[0]), (
124
125
  collection: DocumentCollection,
125
- docs: Array<OptionalUnlessRequiredId<Document>>,
126
+ docs: ReadonlyArray<OptionalUnlessRequiredId<Document>>,
126
127
  options?: BulkWriteOptions
127
128
  ): Effect.Effect<InsertManyResult, MongoError.MongoError> =>
128
129
  F.pipe(
@@ -177,7 +178,7 @@ export const deleteMany: {
177
178
  export const updateMany: {
178
179
  (
179
180
  filter: Filter<Document>,
180
- update: UpdateFilter<Document> | Array<Document>,
181
+ update: UpdateFilter<Document> | ReadonlyArray<Document>,
181
182
  options?: UpdateOptions
182
183
  ): (
183
184
  collection: DocumentCollection
@@ -185,7 +186,7 @@ export const updateMany: {
185
186
  (
186
187
  collection: DocumentCollection,
187
188
  filter: Filter<Document>,
188
- update: UpdateFilter<Document> | Array<Document>,
189
+ update: UpdateFilter<Document> | ReadonlyArray<Document>,
189
190
  options?: UpdateOptions
190
191
  ): Effect.Effect<UpdateResult, MongoError.MongoError>
191
192
  } = F.dual(
@@ -193,11 +194,13 @@ export const updateMany: {
193
194
  (
194
195
  collection: DocumentCollection,
195
196
  filter: Filter<Document>,
196
- update: UpdateFilter<Document> | Array<Document>,
197
+ update: UpdateFilter<Document> | ReadonlyArray<Document>,
197
198
  options?: UpdateOptions
198
199
  ): Effect.Effect<UpdateResult, MongoError.MongoError> =>
199
200
  F.pipe(
200
- Effect.promise(() => collection.collection.updateMany(filter, update, options)),
201
+ Effect.promise(() =>
202
+ collection.collection.updateMany(filter, Array.isArray(update) ? [...update] : update, options)
203
+ ),
201
204
  Effect.catchAllDefect(MongoError.mongoErrorDie<UpdateResult>("updateMany error"))
202
205
  )
203
206
  )
@@ -339,23 +342,23 @@ export const drop: {
339
342
  )
340
343
 
341
344
  export const createIndexes: {
342
- (indexSpecs: Array<IndexDescription>, options?: CreateIndexesOptions): (
345
+ (indexSpecs: ReadonlyArray<IndexDescription>, options?: CreateIndexesOptions): (
343
346
  collection: DocumentCollection
344
347
  ) => Effect.Effect<Array<string>, MongoError.MongoError>
345
348
  (
346
349
  collection: DocumentCollection,
347
- indexSpecs: Array<IndexDescription>,
350
+ indexSpecs: ReadonlyArray<IndexDescription>,
348
351
  options?: CreateIndexesOptions
349
352
  ): Effect.Effect<Array<string>, MongoError.MongoError>
350
353
  } = F.dual(
351
354
  (args) => isDocumentCollection(args[0]),
352
355
  (
353
356
  collection: DocumentCollection,
354
- indexSpecs: Array<IndexDescription>,
357
+ indexSpecs: ReadonlyArray<IndexDescription>,
355
358
  options?: CreateIndexesOptions
356
359
  ): Effect.Effect<Array<string>, MongoError.MongoError> =>
357
360
  F.pipe(
358
- Effect.promise(() => collection.collection.createIndexes(indexSpecs, options)),
361
+ Effect.promise(() => collection.collection.createIndexes([...indexSpecs], options)),
359
362
  Effect.catchAllDefect(MongoError.mongoErrorDie<Array<string>>("createIndexes error"))
360
363
  )
361
364
  )
@@ -407,23 +410,23 @@ export const dropIndex: {
407
410
  )
408
411
 
409
412
  export const aggregate: {
410
- (pipeline?: Array<Document>, options?: AggregateOptions): (
413
+ (pipeline?: ReadonlyArray<Document>, options?: AggregateOptions): (
411
414
  collection: DocumentCollection
412
415
  ) => DocumentAggregationCursor.DocumentAggregationCursor
413
416
  (
414
417
  collection: DocumentCollection,
415
- pipeline?: Array<Document>,
418
+ pipeline?: ReadonlyArray<Document>,
416
419
  options?: AggregateOptions
417
420
  ): DocumentAggregationCursor.DocumentAggregationCursor
418
421
  } = F.dual(
419
422
  (args) => isDocumentCollection(args[0]),
420
423
  (
421
424
  collection: DocumentCollection,
422
- pipeline?: Array<Document>,
425
+ pipeline?: ReadonlyArray<Document>,
423
426
  options?: AggregateOptions
424
427
  ): DocumentAggregationCursor.DocumentAggregationCursor =>
425
428
  new DocumentAggregationCursor.DocumentAggregationCursor({
426
- cursor: collection.collection.aggregate(pipeline, options)
429
+ cursor: collection.collection.aggregate(pipeline ? [...pipeline] : undefined, options)
427
430
  })
428
431
  )
429
432
 
@@ -51,10 +51,10 @@ export const limit: {
51
51
  new DocumentFindCursor({ cursor: cursor.cursor.limit(value) })
52
52
  )
53
53
 
54
- export const toArray = (cursor: DocumentFindCursor): Effect.Effect<ReadonlyArray<Document>, MongoError.MongoError> =>
54
+ export const toArray = (cursor: DocumentFindCursor): Effect.Effect<Array<Document>, MongoError.MongoError> =>
55
55
  F.pipe(
56
56
  Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }),
57
- Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<Document>>("toArray error"))
57
+ Effect.catchAll(MongoError.mongoErrorDie<Array<Document>>("toArray error"))
58
58
  )
59
59
 
60
60
  export const toStream = (
package/src/FindCursor.ts CHANGED
@@ -79,17 +79,21 @@ export const toArray = <A, I, R>(
79
79
  ): Effect.Effect<Array<A>, MongoError.MongoError | ParseResult.ParseError, R> => {
80
80
  const decode = Schema.decodeUnknown(cursor.schema)
81
81
  return Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }).pipe(
82
- Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<A>>("Unable to get array from mongodb cursor")),
82
+ Effect.catchAll(MongoError.mongoErrorDie<Array<A>>("Unable to get array from mongodb cursor")),
83
83
  Effect.flatMap(Effect.forEach((x) => decode(x)))
84
84
  )
85
85
  }
86
86
 
87
87
  export const toArrayEither = <A, I, R>(
88
88
  cursor: FindCursor<A, I, R>
89
- ): Effect.Effect<Array<E.Either<A, [document: unknown, error: ParseResult.ParseError]>>, MongoError.MongoError, R> => {
89
+ ): Effect.Effect<
90
+ Array<E.Either<A, [document: unknown, error: ParseResult.ParseError]>>,
91
+ MongoError.MongoError,
92
+ R
93
+ > => {
90
94
  const decode = Schema.decodeUnknown(cursor.schema)
91
95
  return Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }).pipe(
92
- Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<A>>("Unable to get array from mongodb cursor")),
96
+ Effect.catchAll(MongoError.mongoErrorDie<Array<unknown>>("Unable to get array from mongodb cursor")),
93
97
  Effect.flatMap(Effect.forEach((x) =>
94
98
  F.pipe(
95
99
  decode(x),
@@ -30,10 +30,10 @@ export type FullListCollectionsCursor = ListCollectionsCursor<MongoCollectionInf
30
30
 
31
31
  export const toArray = <T extends DefaultCollectionInfo>(
32
32
  cursor: ListCollectionsCursor<T>
33
- ): Effect.Effect<ReadonlyArray<T>, MongoError.MongoError> =>
33
+ ): Effect.Effect<Array<T>, MongoError.MongoError> =>
34
34
  F.pipe(
35
35
  Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }),
36
- Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<T>>(`${cursor._tag}.toArray error`))
36
+ Effect.catchAll(MongoError.mongoErrorDie<Array<T>>(`${cursor._tag}.toArray error`))
37
37
  )
38
38
 
39
39
  export const toStream = <T extends DefaultCollectionInfo>(
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import * as Effect from "effect/Effect"
5
5
  import * as F from "effect/Function"
6
+ import type * as Scope from "effect/Scope"
6
7
  import type { Db, DbOptions, MongoClientOptions } from "mongodb"
7
8
  import { MongoClient as MongoClient_ } from "mongodb"
8
9
  import * as MongoError from "./MongoError.js"
@@ -17,6 +18,26 @@ export const connect = (
17
18
  Effect.catchAll(MongoError.mongoErrorDie<MongoClient_>("connect error"))
18
19
  )
19
20
 
21
+ export const close: {
22
+ (force?: boolean): (client: MongoClient) => Effect.Effect<void, MongoError.MongoError>
23
+ (client: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError>
24
+ } = F.dual(
25
+ (args) => isMongoClient(args[0]),
26
+ (client: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError> =>
27
+ Effect.promise(() => client.close(force)).pipe(
28
+ Effect.catchAll(MongoError.mongoErrorDie<void>("close error"))
29
+ )
30
+ )
31
+
32
+ export const connectScoped = (
33
+ url: string,
34
+ options?: MongoClientOptions & { forceClose?: boolean }
35
+ ): Effect.Effect<MongoClient, MongoError.MongoError, Scope.Scope> =>
36
+ Effect.acquireRelease(
37
+ connect(url, options),
38
+ (client) => close(client, options?.forceClose).pipe(Effect.orDie)
39
+ )
40
+
20
41
  export const db: {
21
42
  (dbName?: string, options?: DbOptions): (client: MongoClient) => Db
22
43
  (client: MongoClient, dbName?: string, options?: DbOptions): Db
@@ -1,4 +1,4 @@
1
- let moduleVersion = "0.1.0"
1
+ let moduleVersion = "0.1.1"
2
2
 
3
3
  export const getCurrentVersion = () => moduleVersion
4
4