effect-mongodb 0.0.3 → 0.0.5

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 (59) hide show
  1. package/README.md +6 -1
  2. package/dist/cjs/Collection.js +22 -3
  3. package/dist/cjs/Collection.js.map +1 -1
  4. package/dist/cjs/Db.js +5 -19
  5. package/dist/cjs/Db.js.map +1 -1
  6. package/dist/cjs/DocumentCollection.js +18 -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 +1 -5
  10. package/dist/cjs/FindCursor.js.map +1 -1
  11. package/dist/cjs/MongoClient.js +5 -4
  12. package/dist/cjs/MongoClient.js.map +1 -1
  13. package/dist/cjs/internal/modify-result.js +6 -0
  14. package/dist/cjs/internal/modify-result.js.map +1 -0
  15. package/dist/cjs/internal/schema.js +19 -0
  16. package/dist/cjs/internal/schema.js.map +1 -0
  17. package/dist/cjs/internal/version.js +1 -1
  18. package/dist/dts/Collection.d.ts +42 -3
  19. package/dist/dts/Collection.d.ts.map +1 -1
  20. package/dist/dts/Db.d.ts +0 -11
  21. package/dist/dts/Db.d.ts.map +1 -1
  22. package/dist/dts/DocumentCollection.d.ts +42 -3
  23. package/dist/dts/DocumentCollection.d.ts.map +1 -1
  24. package/dist/dts/DocumentFindCursor.d.ts +3 -3
  25. package/dist/dts/DocumentFindCursor.d.ts.map +1 -1
  26. package/dist/dts/FindCursor.d.ts +5 -4
  27. package/dist/dts/FindCursor.d.ts.map +1 -1
  28. package/dist/dts/MongoClient.d.ts +1 -8
  29. package/dist/dts/MongoClient.d.ts.map +1 -1
  30. package/dist/dts/internal/modify-result.d.ts +6 -0
  31. package/dist/dts/internal/modify-result.d.ts.map +1 -0
  32. package/dist/dts/internal/schema.d.ts +6 -0
  33. package/dist/dts/internal/schema.d.ts.map +1 -0
  34. package/dist/esm/Collection.js +21 -2
  35. package/dist/esm/Collection.js.map +1 -1
  36. package/dist/esm/Db.js +3 -15
  37. package/dist/esm/Db.js.map +1 -1
  38. package/dist/esm/DocumentCollection.js +17 -2
  39. package/dist/esm/DocumentCollection.js.map +1 -1
  40. package/dist/esm/DocumentFindCursor.js.map +1 -1
  41. package/dist/esm/FindCursor.js +1 -5
  42. package/dist/esm/FindCursor.js.map +1 -1
  43. package/dist/esm/MongoClient.js +3 -2
  44. package/dist/esm/MongoClient.js.map +1 -1
  45. package/dist/esm/internal/modify-result.js +2 -0
  46. package/dist/esm/internal/modify-result.js.map +1 -0
  47. package/dist/esm/internal/schema.js +10 -0
  48. package/dist/esm/internal/schema.js.map +1 -0
  49. package/dist/esm/internal/version.js +1 -1
  50. package/package.json +1 -1
  51. package/src/Collection.ts +203 -31
  52. package/src/Db.ts +0 -36
  53. package/src/DocumentCollection.ts +195 -30
  54. package/src/DocumentFindCursor.ts +9 -23
  55. package/src/FindCursor.ts +15 -22
  56. package/src/MongoClient.ts +5 -12
  57. package/src/internal/modify-result.ts +6 -0
  58. package/src/internal/schema.ts +12 -0
  59. package/src/internal/version.ts +1 -1
@@ -10,16 +10,23 @@ import type {
10
10
  AggregateOptions,
11
11
  BulkWriteOptions,
12
12
  Collection as MongoCollection,
13
+ CountDocumentsOptions,
13
14
  CreateIndexesOptions,
14
15
  DeleteOptions,
15
16
  DeleteResult,
16
17
  Document,
18
+ DropCollectionOptions,
19
+ DropIndexesOptions,
20
+ EstimatedDocumentCountOptions,
17
21
  Filter,
22
+ FindOneAndReplaceOptions,
18
23
  FindOptions,
19
24
  IndexDescription,
25
+ IndexSpecification,
20
26
  InsertManyResult,
21
27
  InsertOneOptions,
22
28
  InsertOneResult,
29
+ ModifyResult as MongoModifyResult,
23
30
  OptionalUnlessRequiredId,
24
31
  RenameOptions,
25
32
  ReplaceOptions,
@@ -31,6 +38,7 @@ import type {
31
38
  import * as Collection from "./Collection.js"
32
39
  import * as DocumentAggregationCursor from "./DocumentAggregationCursor.js"
33
40
  import * as DocumentFindCursor from "./DocumentFindCursor.js"
41
+ import type { ModifyResult } from "./internal/modify-result.js"
34
42
  import * as MongoError from "./MongoError.js"
35
43
 
36
44
  export class DocumentCollection extends Data.TaggedClass("DocumentCollection")<{
@@ -39,26 +47,27 @@ export class DocumentCollection extends Data.TaggedClass("DocumentCollection")<{
39
47
  }
40
48
 
41
49
  export const find: {
42
- (options?: FindOptions): (collection: DocumentCollection) => DocumentFindCursor.DocumentFindCursor
50
+ (
51
+ filter?: Filter<Document>,
52
+ options?: FindOptions
53
+ ): (collection: DocumentCollection) => DocumentFindCursor.DocumentFindCursor
43
54
  (
44
55
  collection: DocumentCollection,
56
+ filter?: Filter<Document>,
45
57
  options?: FindOptions
46
58
  ): DocumentFindCursor.DocumentFindCursor
47
59
  } = F.dual(
48
60
  (args) => isDocumentCollection(args[0]),
49
- (collection: DocumentCollection, options?: FindOptions) =>
61
+ (collection: DocumentCollection, filter?: Filter<Document>, options?: FindOptions) =>
50
62
  new DocumentFindCursor.DocumentFindCursor(
51
63
  {
52
- cursor: collection.collection.find({}, options)
64
+ cursor: collection.collection.find(filter ?? {}, options)
53
65
  }
54
66
  )
55
67
  )
56
68
 
57
69
  export const findOne: {
58
- (
59
- filter: Filter<Document>,
60
- options?: FindOptions
61
- ): (
70
+ (filter: Filter<Document>, options?: FindOptions): (
62
71
  collection: DocumentCollection
63
72
  ) => Effect.Effect<O.Option<Document>, MongoError.MongoError>
64
73
  (
@@ -81,10 +90,7 @@ export const findOne: {
81
90
  )
82
91
 
83
92
  export const insertOne: {
84
- (
85
- doc: OptionalUnlessRequiredId<Document>,
86
- options?: InsertOneOptions
87
- ): (
93
+ (doc: OptionalUnlessRequiredId<Document>, options?: InsertOneOptions): (
88
94
  collection: DocumentCollection
89
95
  ) => Effect.Effect<InsertOneResult, MongoError.MongoError>
90
96
  (
@@ -125,10 +131,7 @@ export const insertMany: {
125
131
  ))
126
132
 
127
133
  export const deleteOne: {
128
- (
129
- filter: Filter<Document>,
130
- options?: DeleteOptions
131
- ): (
134
+ (filter: Filter<Document>, options?: DeleteOptions): (
132
135
  collection: DocumentCollection
133
136
  ) => Effect.Effect<DeleteResult, MongoError.MongoError>
134
137
  (
@@ -150,10 +153,7 @@ export const deleteOne: {
150
153
  )
151
154
 
152
155
  export const deleteMany: {
153
- (
154
- filter: Filter<Document>,
155
- options?: DeleteOptions
156
- ): (
156
+ (filter: Filter<Document>, options?: DeleteOptions): (
157
157
  collection: DocumentCollection
158
158
  ) => Effect.Effect<DeleteResult, MongoError.MongoError>
159
159
  (
@@ -230,12 +230,80 @@ export const replaceOne: {
230
230
  )
231
231
  )
232
232
 
233
- export const rename: {
233
+ export const findOneAndReplace: {
234
234
  (
235
- newName: string,
236
- options?: RenameOptions
235
+ filter: Filter<Document>,
236
+ replacement: WithoutId<Document>,
237
+ options: FindOneAndReplaceOptions & { includeResultMetadata: true }
238
+ ): (
239
+ collection: DocumentCollection
240
+ ) => Effect.Effect<ModifyResult<Document>, MongoError.MongoError>
241
+ (
242
+ filter: Filter<Document>,
243
+ replacement: WithoutId<Document>,
244
+ options: FindOneAndReplaceOptions & { includeResultMetadata: false }
237
245
  ): (
238
246
  collection: DocumentCollection
247
+ ) => Effect.Effect<O.Option<Document>, MongoError.MongoError>
248
+ (
249
+ filter: Filter<Document>,
250
+ replacement: WithoutId<Document>,
251
+ options: FindOneAndReplaceOptions
252
+ ): (
253
+ collection: DocumentCollection
254
+ ) => Effect.Effect<O.Option<Document>, MongoError.MongoError>
255
+ (filter: Filter<Document>, replacement: WithoutId<Document>): (
256
+ collection: DocumentCollection
257
+ ) => Effect.Effect<O.Option<Document>, MongoError.MongoError>
258
+ (
259
+ collection: DocumentCollection,
260
+ filter: Filter<Document>,
261
+ replacement: WithoutId<Document>,
262
+ options: FindOneAndReplaceOptions & { includeResultMetadata: true }
263
+ ): Effect.Effect<ModifyResult<Document>, MongoError.MongoError>
264
+ (
265
+ collection: DocumentCollection,
266
+ filter: Filter<Document>,
267
+ replacement: WithoutId<Document>,
268
+ options: FindOneAndReplaceOptions & { includeResultMetadata: false }
269
+ ): Effect.Effect<O.Option<Document>, MongoError.MongoError>
270
+ (
271
+ collection: DocumentCollection,
272
+ filter: Filter<Document>,
273
+ replacement: WithoutId<Document>,
274
+ options: FindOneAndReplaceOptions
275
+ ): Effect.Effect<O.Option<Document>, MongoError.MongoError>
276
+ (
277
+ collection: DocumentCollection,
278
+ filter: Filter<Document>,
279
+ replacement: WithoutId<Document>
280
+ ): Effect.Effect<O.Option<Document>, MongoError.MongoError>
281
+ } = F.dual(
282
+ (args) => isDocumentCollection(args[0]),
283
+ (
284
+ collection: DocumentCollection,
285
+ filter: Filter<Document>,
286
+ replacement: WithoutId<Document>,
287
+ options?: FindOneAndReplaceOptions
288
+ ): Effect.Effect<O.Option<Document> | ModifyResult<Document>, MongoError.MongoError> =>
289
+ F.pipe(
290
+ Effect.promise(() => collection.collection.findOneAndReplace(filter, replacement, options ?? {})),
291
+ Effect.map((value) => {
292
+ if (options?.includeResultMetadata && !!value) {
293
+ const result = value as unknown as MongoModifyResult<Document>
294
+ return { ...result, value: O.fromNullable(result.value) }
295
+ }
296
+ return O.fromNullable(value)
297
+ }),
298
+ Effect.catchAllDefect(
299
+ MongoError.mongoErrorDie<O.Option<Document> | ModifyResult<Document>>("findOneAndReplace error")
300
+ )
301
+ )
302
+ )
303
+
304
+ export const rename: {
305
+ (newName: string, options?: RenameOptions): (
306
+ collection: DocumentCollection
239
307
  ) => Effect.Effect<MongoCollection, MongoError.MongoError>
240
308
  (
241
309
  collection: DocumentCollection,
@@ -255,11 +323,23 @@ export const rename: {
255
323
  )
256
324
  )
257
325
 
258
- export const createIndexes: {
326
+ export const drop: {
327
+ (options?: DropCollectionOptions): (collection: DocumentCollection) => Effect.Effect<boolean, MongoError.MongoError>
328
+ (collection: DocumentCollection, options?: DropCollectionOptions): Effect.Effect<boolean, MongoError.MongoError>
329
+ } = F.dual(
330
+ (args) => isDocumentCollection(args[0]),
259
331
  (
260
- indexSpecs: Array<IndexDescription>,
261
- options?: CreateIndexesOptions
262
- ): (
332
+ collection: DocumentCollection,
333
+ options?: DropCollectionOptions
334
+ ): Effect.Effect<boolean, MongoError.MongoError> =>
335
+ F.pipe(
336
+ Effect.promise(() => collection.collection.drop(options)),
337
+ Effect.catchAllDefect(MongoError.mongoErrorDie<boolean>("drop error"))
338
+ )
339
+ )
340
+
341
+ export const createIndexes: {
342
+ (indexSpecs: Array<IndexDescription>, options?: CreateIndexesOptions): (
263
343
  collection: DocumentCollection
264
344
  ) => Effect.Effect<Array<string>, MongoError.MongoError>
265
345
  (
@@ -280,11 +360,54 @@ export const createIndexes: {
280
360
  )
281
361
  )
282
362
 
283
- export const aggregate: {
363
+ export const createIndex: {
284
364
  (
285
- pipeline?: Array<Document>,
286
- options?: AggregateOptions
287
- ): (
365
+ indexSpec: IndexSpecification,
366
+ options?: CreateIndexesOptions
367
+ ): (collection: DocumentCollection) => Effect.Effect<string, MongoError.MongoError>
368
+ (
369
+ collection: DocumentCollection,
370
+ indexSpec: IndexSpecification,
371
+ options?: CreateIndexesOptions
372
+ ): Effect.Effect<string, MongoError.MongoError>
373
+ } = F.dual(
374
+ (args) => isDocumentCollection(args[0]),
375
+ (
376
+ collection: DocumentCollection,
377
+ indexSpec: IndexSpecification,
378
+ options?: CreateIndexesOptions
379
+ ): Effect.Effect<string, MongoError.MongoError> =>
380
+ F.pipe(
381
+ Effect.promise(() => collection.collection.createIndex(indexSpec, options)),
382
+ Effect.catchAllDefect(MongoError.mongoErrorDie<string>("createIndex error"))
383
+ )
384
+ )
385
+
386
+ export const dropIndex: {
387
+ (
388
+ indexName: string,
389
+ options?: DropIndexesOptions
390
+ ): (collection: DocumentCollection) => Effect.Effect<Document, MongoError.MongoError>
391
+ (
392
+ collection: DocumentCollection,
393
+ indexName: string,
394
+ options?: DropIndexesOptions
395
+ ): Effect.Effect<Document, MongoError.MongoError>
396
+ } = F.dual(
397
+ (args) => isDocumentCollection(args[0]),
398
+ (
399
+ collection: DocumentCollection,
400
+ indexName: string,
401
+ options?: DropIndexesOptions
402
+ ): Effect.Effect<Document, MongoError.MongoError> =>
403
+ F.pipe(
404
+ Effect.promise(() => collection.collection.dropIndex(indexName, options)),
405
+ Effect.catchAllDefect(MongoError.mongoErrorDie<Document>("dropIndex error"))
406
+ )
407
+ )
408
+
409
+ export const aggregate: {
410
+ (pipeline?: Array<Document>, options?: AggregateOptions): (
288
411
  collection: DocumentCollection
289
412
  ) => DocumentAggregationCursor.DocumentAggregationCursor
290
413
  (
@@ -304,6 +427,48 @@ export const aggregate: {
304
427
  })
305
428
  )
306
429
 
430
+ export const estimatedDocumentCount: {
431
+ (options?: EstimatedDocumentCountOptions): (
432
+ collection: DocumentCollection
433
+ ) => Effect.Effect<number, MongoError.MongoError>
434
+ (
435
+ collection: DocumentCollection,
436
+ options?: EstimatedDocumentCountOptions
437
+ ): Effect.Effect<number, MongoError.MongoError>
438
+ } = F.dual(
439
+ (args) => isDocumentCollection(args[0]),
440
+ (
441
+ collection: DocumentCollection,
442
+ options?: EstimatedDocumentCountOptions
443
+ ): Effect.Effect<number, MongoError.MongoError> =>
444
+ F.pipe(
445
+ Effect.promise(() => collection.collection.estimatedDocumentCount(options)),
446
+ Effect.catchAllDefect(MongoError.mongoErrorDie<number>("estimatedDocumentCount error"))
447
+ )
448
+ )
449
+
450
+ export const countDocuments: {
451
+ (filter?: Filter<Document>, options?: CountDocumentsOptions): (
452
+ collection: DocumentCollection
453
+ ) => Effect.Effect<number, MongoError.MongoError>
454
+ (
455
+ collection: DocumentCollection,
456
+ filter?: Filter<Document>,
457
+ options?: CountDocumentsOptions
458
+ ): Effect.Effect<number, MongoError.MongoError>
459
+ } = F.dual(
460
+ (args) => isDocumentCollection(args[0]),
461
+ (
462
+ collection: DocumentCollection,
463
+ filter?: Filter<Document>,
464
+ options?: CountDocumentsOptions
465
+ ): Effect.Effect<number, MongoError.MongoError> =>
466
+ F.pipe(
467
+ Effect.promise(() => collection.collection.countDocuments(filter, options)),
468
+ Effect.catchAllDefect(MongoError.mongoErrorDie<number>("countDocuments error"))
469
+ )
470
+ )
471
+
307
472
  export const typed: {
308
473
  <A extends Document, I extends Document = A, R = never>(
309
474
  schema: Schema.Schema<A, I, R>
@@ -6,7 +6,7 @@ import * as Effect from "effect/Effect"
6
6
  import * as F from "effect/Function"
7
7
  import type * as Schema from "effect/Schema"
8
8
  import * as Stream from "effect/Stream"
9
- import type { Document, FindCursor as MongoFindCursor, Sort, SortDirection } from "mongodb"
9
+ import type { Document, Filter, FindCursor as MongoFindCursor, Sort, SortDirection } from "mongodb"
10
10
  import * as FindCursor from "./FindCursor.js"
11
11
  import * as MongoError from "./MongoError.js"
12
12
 
@@ -16,20 +16,16 @@ export class DocumentFindCursor extends Data.TaggedClass("DocumentFindCursor")<{
16
16
  }
17
17
 
18
18
  export const filter: {
19
- (
20
- filter: Document
21
- ): (cursor: DocumentFindCursor) => DocumentFindCursor
22
- (cursor: DocumentFindCursor, filter: Document): DocumentFindCursor
19
+ (filter: Filter<Document>): (cursor: DocumentFindCursor) => DocumentFindCursor
20
+ (cursor: DocumentFindCursor, filter: Filter<Document>): DocumentFindCursor
23
21
  } = F.dual(
24
22
  (args) => isDocumentFindCursor(args[0]),
25
- (cursor: DocumentFindCursor, filter: Document): DocumentFindCursor =>
23
+ (cursor: DocumentFindCursor, filter: Filter<Document>): DocumentFindCursor =>
26
24
  new DocumentFindCursor({ cursor: cursor.cursor.filter(filter) })
27
25
  )
28
26
 
29
27
  export const project: {
30
- (
31
- value: Document
32
- ): (cursor: DocumentFindCursor) => DocumentFindCursor
28
+ (value: Document): (cursor: DocumentFindCursor) => DocumentFindCursor
33
29
  (cursor: DocumentFindCursor, value: Document): DocumentFindCursor
34
30
  } = F.dual(
35
31
  (args) => isDocumentFindCursor(args[0]),
@@ -38,10 +34,7 @@ export const project: {
38
34
  )
39
35
 
40
36
  export const sort: {
41
- (
42
- sort: Sort | string,
43
- direction?: SortDirection
44
- ): (cursor: DocumentFindCursor) => DocumentFindCursor
37
+ (sort: Sort | string, direction?: SortDirection): (cursor: DocumentFindCursor) => DocumentFindCursor
45
38
  (cursor: DocumentFindCursor, sort: Sort | string, direction?: SortDirection): DocumentFindCursor
46
39
  } = F.dual(
47
40
  (args) => isDocumentFindCursor(args[0]),
@@ -50,9 +43,7 @@ export const sort: {
50
43
  )
51
44
 
52
45
  export const limit: {
53
- (
54
- value: number
55
- ): (cursor: DocumentFindCursor) => DocumentFindCursor
46
+ (value: number): (cursor: DocumentFindCursor) => DocumentFindCursor
56
47
  (cursor: DocumentFindCursor, value: number): DocumentFindCursor
57
48
  } = F.dual(
58
49
  (args) => isDocumentFindCursor(args[0]),
@@ -75,13 +66,8 @@ export const toStream = (
75
66
  )
76
67
 
77
68
  export const typed: {
78
- <A, I = A, R = never>(
79
- schema: Schema.Schema<A, I, R>
80
- ): (cursor: DocumentFindCursor) => FindCursor.FindCursor<A, I, R>
81
- <A, I = A, R = never>(
82
- cursor: DocumentFindCursor,
83
- schema: Schema.Schema<A, I, R>
84
- ): FindCursor.FindCursor<A, I, R>
69
+ <A, I = A, R = never>(schema: Schema.Schema<A, I, R>): (cursor: DocumentFindCursor) => FindCursor.FindCursor<A, I, R>
70
+ <A, I = A, R = never>(cursor: DocumentFindCursor, schema: Schema.Schema<A, I, R>): FindCursor.FindCursor<A, I, R>
85
71
  } = F.dual((args) => isDocumentFindCursor(args[0]), <A, I = A, R = never>(
86
72
  cursor: DocumentFindCursor,
87
73
  schema: Schema.Schema<A, I, R>
package/src/FindCursor.ts CHANGED
@@ -10,26 +10,23 @@ import * as Schema from "effect/Schema"
10
10
  import * as Stream from "effect/Stream"
11
11
  import * as Tuple from "effect/Tuple"
12
12
  import type { Document, FindCursor as FindCursor_, Sort, SortDirection } from "mongodb"
13
+ import type { Filter } from "./internal/filter.js"
13
14
  import * as MongoError from "./MongoError.js"
14
15
 
15
16
  export class FindCursor<A, I = A, R = never> extends Data.TaggedClass("FindCursor")<{
16
17
  cursor: FindCursor_<unknown>
17
18
  schema: Schema.Schema<A, I, R>
18
- }> {}
19
+ }> {
20
+ }
19
21
 
20
22
  export const filter: {
21
- <T extends Document = Document>(
22
- filter: T
23
- ): <A, I, R>(cursor: FindCursor<A, I, R>) => FindCursor<A, I, R>
24
- <A, I, R, T extends Document = Document>(
25
- cursor: FindCursor<A, I, R>,
26
- filter: T
27
- ): FindCursor<A, I, R>
23
+ <I extends Document>(filter: Filter<I>): <A, R>(cursor: FindCursor<A, I, R>) => FindCursor<A, I, R>
24
+ <A, I extends Document, R>(cursor: FindCursor<A, I, R>, filter: Filter<I>): FindCursor<A, I, R>
28
25
  } = F.dual(
29
26
  (args) => isFindCursor(args[0]),
30
- <A, I, R, T extends Document = Document>(
27
+ <A, I extends Document, R>(
31
28
  cursor: FindCursor<A, I, R>,
32
- filter: T
29
+ filter: Filter<I>
33
30
  ): FindCursor<A, I, R> => new FindCursor({ cursor: cursor.cursor.filter(filter), schema: cursor.schema })
34
31
  )
35
32
 
@@ -53,10 +50,7 @@ export const project: {
53
50
  )
54
51
 
55
52
  export const sort: {
56
- (
57
- sort: Sort | string,
58
- direction?: SortDirection
59
- ): <A, I, R>(cursor: FindCursor<A, I, R>) => FindCursor<A, I, R>
53
+ (sort: Sort | string, direction?: SortDirection): <A, I, R>(cursor: FindCursor<A, I, R>) => FindCursor<A, I, R>
60
54
  <A, I, R>(
61
55
  cursor: FindCursor<A, I, R>,
62
56
  sort: Sort | string,
@@ -72,9 +66,7 @@ export const sort: {
72
66
  )
73
67
 
74
68
  export const limit: {
75
- (
76
- value: number
77
- ): <A, I, R>(cursor: FindCursor<A, I, R>) => FindCursor<A, I, R>
69
+ (value: number): <A, I, R>(cursor: FindCursor<A, I, R>) => FindCursor<A, I, R>
78
70
  <A, I, R>(cursor: FindCursor<A, I, R>, value: number): FindCursor<A, I, R>
79
71
  } = F.dual(
80
72
  (args) => isFindCursor(args[0]),
@@ -82,8 +74,9 @@ export const limit: {
82
74
  new FindCursor({ cursor: cursor.cursor.limit(value), schema: cursor.schema })
83
75
  )
84
76
 
85
- // TODO: add explicit return type for public API
86
- export const toArray = <A, I, R>(cursor: FindCursor<A, I, R>) => {
77
+ export const toArray = <A, I, R>(
78
+ cursor: FindCursor<A, I, R>
79
+ ): Effect.Effect<Array<A>, MongoError.MongoError | ParseResult.ParseError, R> => {
87
80
  const decode = Schema.decodeUnknown(cursor.schema)
88
81
  return Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }).pipe(
89
82
  Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<A>>("Unable to get array from mongodb cursor")),
@@ -91,14 +84,14 @@ export const toArray = <A, I, R>(cursor: FindCursor<A, I, R>) => {
91
84
  )
92
85
  }
93
86
 
94
- // TODO: add explicit return type for public API
95
- export const toArrayEither = <A, I, R>(cursor: FindCursor<A, I, R>) => {
87
+ export const toArrayEither = <A, I, R>(
88
+ cursor: FindCursor<A, I, R>
89
+ ): Effect.Effect<Array<E.Either<A, [document: unknown, error: ParseResult.ParseError]>>, MongoError.MongoError, R> => {
96
90
  const decode = Schema.decodeUnknown(cursor.schema)
97
91
  return Effect.tryPromise({ try: () => cursor.cursor.toArray(), catch: F.identity }).pipe(
98
92
  Effect.catchAll(MongoError.mongoErrorDie<ReadonlyArray<A>>("Unable to get array from mongodb cursor")),
99
93
  Effect.flatMap(Effect.forEach((x) =>
100
94
  F.pipe(
101
- // keep new line
102
95
  decode(x),
103
96
  Effect.mapError((error) => Tuple.make(x, error)),
104
97
  Effect.either
@@ -1,18 +1,18 @@
1
1
  /**
2
2
  * @since 0.0.1
3
3
  */
4
- import type * as Brand from "effect/Brand"
5
4
  import * as Effect from "effect/Effect"
6
5
  import * as F from "effect/Function"
7
6
  import type { Db, DbOptions, MongoClientOptions } from "mongodb"
8
7
  import { MongoClient as MongoClient_ } from "mongodb"
9
8
  import * as MongoError from "./MongoError.js"
10
9
 
11
- import * as Context from "effect/Context"
12
-
13
10
  export type MongoClient = MongoClient_
14
11
 
15
- export const connect = (url: string, options?: MongoClientOptions) =>
12
+ export const connect = (
13
+ url: string,
14
+ options?: MongoClientOptions
15
+ ): Effect.Effect<MongoClient, MongoError.MongoError> =>
16
16
  Effect.promise(() => MongoClient_.connect(url, options)).pipe(
17
17
  Effect.catchAll(MongoError.mongoErrorDie<MongoClient_>("connect error"))
18
18
  )
@@ -22,14 +22,7 @@ export const db: {
22
22
  (client: MongoClient, dbName?: string, options?: DbOptions): Db
23
23
  } = F.dual(
24
24
  (args) => isMongoClient(args[0]),
25
- (client: MongoClient, dbName?: string, options?: DbOptions) => client.db(dbName, options)
25
+ (client: MongoClient, dbName?: string, options?: DbOptions): Db => client.db(dbName, options)
26
26
  )
27
27
 
28
28
  const isMongoClient = (x: unknown) => x instanceof MongoClient_
29
-
30
- export type MongoClientService<K extends string> = {
31
- client: Effect.Effect<MongoClient, MongoError.MongoError>
32
- } & Brand.Brand<K>
33
-
34
- export const Tag = <K extends string>(key: K) => Context.GenericTag<MongoClientService<K>>(key)
35
- export type TagType<K extends string> = ReturnType<typeof Tag<K>>
@@ -0,0 +1,6 @@
1
+ import type * as O from "effect/Option"
2
+ import type { ModifyResult as MongoModifyResult } from "mongodb"
3
+
4
+ export type ModifyResult<TSchema> = Omit<MongoModifyResult<TSchema>, "value"> & {
5
+ value: O.Option<TSchema>
6
+ }
@@ -0,0 +1,12 @@
1
+ import * as Effect from "effect/Effect"
2
+ import * as O from "effect/Option"
3
+ import * as Schema from "effect/Schema"
4
+ import type { Document } from "mongodb"
5
+
6
+ // TODO: there is probably a Schema to do this decode or using typeclass package for traverse
7
+ export const decodeNullableDocument = <A, I, R>(schema: Schema.Schema<A, I, R>, value: Document | null) =>
8
+ Effect.gen(function*(_) {
9
+ if (value === null) return O.none()
10
+ const decoded = yield* _(Schema.decodeUnknown(schema)(value))
11
+ return O.some(decoded)
12
+ })
@@ -1,4 +1,4 @@
1
- let moduleVersion = "0.0.3"
1
+ let moduleVersion = "0.0.5"
2
2
 
3
3
  export const getCurrentVersion = () => moduleVersion
4
4