effect-mongodb 0.0.4 → 0.1.0

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 (40) hide show
  1. package/dist/cjs/Collection.js +5 -4
  2. package/dist/cjs/Collection.js.map +1 -1
  3. package/dist/cjs/Db.js +5 -19
  4. package/dist/cjs/Db.js.map +1 -1
  5. package/dist/cjs/DocumentCollection.js +3 -1
  6. package/dist/cjs/DocumentCollection.js.map +1 -1
  7. package/dist/cjs/DocumentFindCursor.js.map +1 -1
  8. package/dist/cjs/FindCursor.js.map +1 -1
  9. package/dist/cjs/MongoClient.js +5 -4
  10. package/dist/cjs/MongoClient.js.map +1 -1
  11. package/dist/cjs/internal/version.js +1 -1
  12. package/dist/dts/Collection.d.ts +12 -4
  13. package/dist/dts/Collection.d.ts.map +1 -1
  14. package/dist/dts/Db.d.ts +2 -14
  15. package/dist/dts/Db.d.ts.map +1 -1
  16. package/dist/dts/DocumentCollection.d.ts +9 -1
  17. package/dist/dts/DocumentCollection.d.ts.map +1 -1
  18. package/dist/dts/DocumentFindCursor.d.ts.map +1 -1
  19. package/dist/dts/FindCursor.d.ts.map +1 -1
  20. package/dist/dts/MongoClient.d.ts +0 -7
  21. package/dist/dts/MongoClient.d.ts.map +1 -1
  22. package/dist/esm/Collection.js +4 -3
  23. package/dist/esm/Collection.js.map +1 -1
  24. package/dist/esm/Db.js +3 -15
  25. package/dist/esm/Db.js.map +1 -1
  26. package/dist/esm/DocumentCollection.js +2 -0
  27. package/dist/esm/DocumentCollection.js.map +1 -1
  28. package/dist/esm/DocumentFindCursor.js.map +1 -1
  29. package/dist/esm/FindCursor.js.map +1 -1
  30. package/dist/esm/MongoClient.js +3 -2
  31. package/dist/esm/MongoClient.js.map +1 -1
  32. package/dist/esm/internal/version.js +1 -1
  33. package/package.json +2 -2
  34. package/src/Collection.ts +61 -53
  35. package/src/Db.ts +2 -39
  36. package/src/DocumentCollection.ts +52 -32
  37. package/src/DocumentFindCursor.ts +6 -20
  38. package/src/FindCursor.ts +4 -14
  39. package/src/MongoClient.ts +0 -10
  40. package/src/internal/version.ts +1 -1
package/src/Db.ts CHANGED
@@ -1,18 +1,14 @@
1
1
  /**
2
2
  * @since 0.0.1
3
3
  */
4
- import type * as Brand from "effect/Brand"
5
- import * as Context from "effect/Context"
6
4
  import * as Effect from "effect/Effect"
7
5
  import * as F from "effect/Function"
8
- import * as Layer from "effect/Layer"
9
6
  import type * as Schema from "effect/Schema"
10
7
  import type { Document, DropCollectionOptions, ListCollectionsOptions } from "mongodb"
11
8
  import { Db } from "mongodb"
12
9
  import type * as Collection from "./Collection.js"
13
10
  import * as DocumentCollection from "./DocumentCollection.js"
14
11
  import * as ListCollectionsCursor from "./ListCollectionsCursor.js"
15
- import * as MongoClient from "./MongoClient.js"
16
12
  import * as MongoError from "./MongoError.js"
17
13
 
18
14
  export const documentCollection: {
@@ -46,7 +42,7 @@ export const collection: {
46
42
  )
47
43
 
48
44
  export const listCollections: {
49
- (): (db: Db) => ListCollectionsCursor.ListCollectionsCursor
45
+ (db: Db): ListCollectionsCursor.ListCollectionsCursor
50
46
  (filter: Document): (db: Db) => ListCollectionsCursor.ListCollectionsCursor
51
47
  (
52
48
  filter: Document,
@@ -56,7 +52,6 @@ export const listCollections: {
56
52
  filter: Document,
57
53
  options: Exclude<ListCollectionsOptions, "nameOnly"> & { nameOnly: false }
58
54
  ): (db: Db) => ListCollectionsCursor.FullListCollectionsCursor
59
- (db: Db): ListCollectionsCursor.ListCollectionsCursor
60
55
  (db: Db, filter: Document): ListCollectionsCursor.ListCollectionsCursor
61
56
  (
62
57
  db: Db,
@@ -75,7 +70,7 @@ export const listCollections: {
75
70
  )
76
71
 
77
72
  export const dropCollection: {
78
- (name: string): (db: Db) => Effect.Effect<boolean>
73
+ (name: string): (db: Db) => Effect.Effect<boolean, MongoError.MongoError>
79
74
  (name: string, options: DropCollectionOptions): (db: Db) => Effect.Effect<boolean, MongoError.MongoError>
80
75
  (db: Db, name: string): Effect.Effect<boolean, MongoError.MongoError>
81
76
  (db: Db, name: string, options: DropCollectionOptions): Effect.Effect<boolean, MongoError.MongoError>
@@ -89,35 +84,3 @@ export const dropCollection: {
89
84
  )
90
85
 
91
86
  const isDb = (x: unknown) => x instanceof Db
92
-
93
- export type DbService<K extends string> = {
94
- db: Effect.Effect<Db, MongoError.MongoError>
95
- } & Brand.Brand<K>
96
-
97
- export const Tag = <K extends string>(key: K) => Context.GenericTag<DbService<K>>(key)
98
- export type TagType<K extends string> = ReturnType<typeof Tag<K>>
99
-
100
- export const fromEffect = <DbK extends string, MongoClientK extends string, E = never, R = never>(
101
- dbTag: TagType<DbK>,
102
- clientTag: MongoClient.TagType<MongoClientK>,
103
- dbName: Effect.Effect<string, E, R>
104
- ) =>
105
- Layer.effect(
106
- dbTag,
107
- Effect.gen(function*(_) {
108
- const { client } = yield* _(clientTag)
109
- const dbName_ = yield* _(dbName)
110
- const db = yield* _(
111
- client,
112
- Effect.map((client) => MongoClient.db(client, dbName_)),
113
- Effect.cached
114
- )
115
- return dbTag.of({ db } as DbService<DbK>) // TODO fix cast using branded ctor
116
- })
117
- )
118
-
119
- export const fromConst = <DbK extends string, MongoClientK extends string>(
120
- dbTag: TagType<DbK>,
121
- clientTag: MongoClient.TagType<MongoClientK>,
122
- dbName: string
123
- ) => fromEffect(dbTag, clientTag, Effect.succeed(dbName))
@@ -10,12 +10,14 @@ 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,
17
18
  DropCollectionOptions,
18
19
  DropIndexesOptions,
20
+ EstimatedDocumentCountOptions,
19
21
  Filter,
20
22
  FindOneAndReplaceOptions,
21
23
  FindOptions,
@@ -65,10 +67,7 @@ export const find: {
65
67
  )
66
68
 
67
69
  export const findOne: {
68
- (
69
- filter: Filter<Document>,
70
- options?: FindOptions
71
- ): (
70
+ (filter: Filter<Document>, options?: FindOptions): (
72
71
  collection: DocumentCollection
73
72
  ) => Effect.Effect<O.Option<Document>, MongoError.MongoError>
74
73
  (
@@ -91,10 +90,7 @@ export const findOne: {
91
90
  )
92
91
 
93
92
  export const insertOne: {
94
- (
95
- doc: OptionalUnlessRequiredId<Document>,
96
- options?: InsertOneOptions
97
- ): (
93
+ (doc: OptionalUnlessRequiredId<Document>, options?: InsertOneOptions): (
98
94
  collection: DocumentCollection
99
95
  ) => Effect.Effect<InsertOneResult, MongoError.MongoError>
100
96
  (
@@ -135,10 +131,7 @@ export const insertMany: {
135
131
  ))
136
132
 
137
133
  export const deleteOne: {
138
- (
139
- filter: Filter<Document>,
140
- options?: DeleteOptions
141
- ): (
134
+ (filter: Filter<Document>, options?: DeleteOptions): (
142
135
  collection: DocumentCollection
143
136
  ) => Effect.Effect<DeleteResult, MongoError.MongoError>
144
137
  (
@@ -160,10 +153,7 @@ export const deleteOne: {
160
153
  )
161
154
 
162
155
  export const deleteMany: {
163
- (
164
- filter: Filter<Document>,
165
- options?: DeleteOptions
166
- ): (
156
+ (filter: Filter<Document>, options?: DeleteOptions): (
167
157
  collection: DocumentCollection
168
158
  ) => Effect.Effect<DeleteResult, MongoError.MongoError>
169
159
  (
@@ -262,10 +252,7 @@ export const findOneAndReplace: {
262
252
  ): (
263
253
  collection: DocumentCollection
264
254
  ) => Effect.Effect<O.Option<Document>, MongoError.MongoError>
265
- (
266
- filter: Filter<Document>,
267
- replacement: WithoutId<Document>
268
- ): (
255
+ (filter: Filter<Document>, replacement: WithoutId<Document>): (
269
256
  collection: DocumentCollection
270
257
  ) => Effect.Effect<O.Option<Document>, MongoError.MongoError>
271
258
  (
@@ -315,10 +302,7 @@ export const findOneAndReplace: {
315
302
  )
316
303
 
317
304
  export const rename: {
318
- (
319
- newName: string,
320
- options?: RenameOptions
321
- ): (
305
+ (newName: string, options?: RenameOptions): (
322
306
  collection: DocumentCollection
323
307
  ) => Effect.Effect<MongoCollection, MongoError.MongoError>
324
308
  (
@@ -355,10 +339,7 @@ export const drop: {
355
339
  )
356
340
 
357
341
  export const createIndexes: {
358
- (
359
- indexSpecs: Array<IndexDescription>,
360
- options?: CreateIndexesOptions
361
- ): (
342
+ (indexSpecs: Array<IndexDescription>, options?: CreateIndexesOptions): (
362
343
  collection: DocumentCollection
363
344
  ) => Effect.Effect<Array<string>, MongoError.MongoError>
364
345
  (
@@ -426,10 +407,7 @@ export const dropIndex: {
426
407
  )
427
408
 
428
409
  export const aggregate: {
429
- (
430
- pipeline?: Array<Document>,
431
- options?: AggregateOptions
432
- ): (
410
+ (pipeline?: Array<Document>, options?: AggregateOptions): (
433
411
  collection: DocumentCollection
434
412
  ) => DocumentAggregationCursor.DocumentAggregationCursor
435
413
  (
@@ -449,6 +427,48 @@ export const aggregate: {
449
427
  })
450
428
  )
451
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
+
452
472
  export const typed: {
453
473
  <A extends Document, I extends Document = A, R = never>(
454
474
  schema: Schema.Schema<A, I, R>
@@ -16,9 +16,7 @@ export class DocumentFindCursor extends Data.TaggedClass("DocumentFindCursor")<{
16
16
  }
17
17
 
18
18
  export const filter: {
19
- (
20
- filter: Filter<Document>
21
- ): (cursor: DocumentFindCursor) => DocumentFindCursor
19
+ (filter: Filter<Document>): (cursor: DocumentFindCursor) => DocumentFindCursor
22
20
  (cursor: DocumentFindCursor, filter: Filter<Document>): DocumentFindCursor
23
21
  } = F.dual(
24
22
  (args) => isDocumentFindCursor(args[0]),
@@ -27,9 +25,7 @@ export const 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
@@ -20,13 +20,8 @@ export class FindCursor<A, I = A, R = never> extends Data.TaggedClass("FindCurso
20
20
  }
21
21
 
22
22
  export const filter: {
23
- <I extends Document>(
24
- filter: Filter<I>
25
- ): <A, R>(cursor: FindCursor<A, I, R>) => FindCursor<A, I, R>
26
- <A, I extends Document, R>(
27
- cursor: FindCursor<A, I, R>,
28
- filter: Filter<I>
29
- ): 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>
30
25
  } = F.dual(
31
26
  (args) => isFindCursor(args[0]),
32
27
  <A, I extends Document, R>(
@@ -55,10 +50,7 @@ export const project: {
55
50
  )
56
51
 
57
52
  export const sort: {
58
- (
59
- sort: Sort | string,
60
- direction?: SortDirection
61
- ): <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>
62
54
  <A, I, R>(
63
55
  cursor: FindCursor<A, I, R>,
64
56
  sort: Sort | string,
@@ -74,9 +66,7 @@ export const sort: {
74
66
  )
75
67
 
76
68
  export const limit: {
77
- (
78
- value: number
79
- ): <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>
80
70
  <A, I, R>(cursor: FindCursor<A, I, R>, value: number): FindCursor<A, I, R>
81
71
  } = F.dual(
82
72
  (args) => isFindCursor(args[0]),
@@ -1,15 +1,12 @@
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
12
  export const connect = (
@@ -29,10 +26,3 @@ export const db: {
29
26
  )
30
27
 
31
28
  const isMongoClient = (x: unknown) => x instanceof MongoClient_
32
-
33
- export type MongoClientService<K extends string> = {
34
- client: Effect.Effect<MongoClient, MongoError.MongoError>
35
- } & Brand.Brand<K>
36
-
37
- export const Tag = <K extends string>(key: K) => Context.GenericTag<MongoClientService<K>>(key)
38
- export type TagType<K extends string> = ReturnType<typeof Tag<K>>
@@ -1,4 +1,4 @@
1
- let moduleVersion = "0.0.4"
1
+ let moduleVersion = "0.1.0"
2
2
 
3
3
  export const getCurrentVersion = () => moduleVersion
4
4