effect-mongodb 0.1.3 → 0.2.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 (73) hide show
  1. package/README.md +25 -19
  2. package/dist/cjs/AggregationCursor.js +10 -4
  3. package/dist/cjs/AggregationCursor.js.map +1 -1
  4. package/dist/cjs/Collection.js +19 -14
  5. package/dist/cjs/Collection.js.map +1 -1
  6. package/dist/cjs/Db.js +11 -9
  7. package/dist/cjs/Db.js.map +1 -1
  8. package/dist/cjs/DocumentAggregationCursor.js +10 -4
  9. package/dist/cjs/DocumentAggregationCursor.js.map +1 -1
  10. package/dist/cjs/DocumentCollection.js +15 -9
  11. package/dist/cjs/DocumentCollection.js.map +1 -1
  12. package/dist/cjs/DocumentFindCursor.js +16 -10
  13. package/dist/cjs/DocumentFindCursor.js.map +1 -1
  14. package/dist/cjs/FindCursor.js +15 -9
  15. package/dist/cjs/FindCursor.js.map +1 -1
  16. package/dist/cjs/ListCollectionsCursor.js +10 -4
  17. package/dist/cjs/ListCollectionsCursor.js.map +1 -1
  18. package/dist/cjs/MongoClient.js +17 -5
  19. package/dist/cjs/MongoClient.js.map +1 -1
  20. package/dist/cjs/internal/schema.js +2 -2
  21. package/dist/cjs/internal/schema.js.map +1 -1
  22. package/dist/cjs/internal/version.js +1 -1
  23. package/dist/dts/AggregationCursor.d.ts +5 -5
  24. package/dist/dts/AggregationCursor.d.ts.map +1 -1
  25. package/dist/dts/Collection.d.ts +5 -6
  26. package/dist/dts/Collection.d.ts.map +1 -1
  27. package/dist/dts/Db.d.ts +9 -5
  28. package/dist/dts/Db.d.ts.map +1 -1
  29. package/dist/dts/DocumentAggregationCursor.d.ts +5 -5
  30. package/dist/dts/DocumentAggregationCursor.d.ts.map +1 -1
  31. package/dist/dts/DocumentCollection.d.ts +5 -5
  32. package/dist/dts/DocumentCollection.d.ts.map +1 -1
  33. package/dist/dts/DocumentFindCursor.d.ts +5 -5
  34. package/dist/dts/DocumentFindCursor.d.ts.map +1 -1
  35. package/dist/dts/FindCursor.d.ts +5 -5
  36. package/dist/dts/FindCursor.d.ts.map +1 -1
  37. package/dist/dts/ListCollectionsCursor.d.ts +5 -5
  38. package/dist/dts/ListCollectionsCursor.d.ts.map +1 -1
  39. package/dist/dts/MongoClient.d.ts +15 -9
  40. package/dist/dts/MongoClient.d.ts.map +1 -1
  41. package/dist/esm/AggregationCursor.js +8 -2
  42. package/dist/esm/AggregationCursor.js.map +1 -1
  43. package/dist/esm/Collection.js +17 -12
  44. package/dist/esm/Collection.js.map +1 -1
  45. package/dist/esm/Db.js +8 -7
  46. package/dist/esm/Db.js.map +1 -1
  47. package/dist/esm/DocumentAggregationCursor.js +8 -2
  48. package/dist/esm/DocumentAggregationCursor.js.map +1 -1
  49. package/dist/esm/DocumentCollection.js +13 -7
  50. package/dist/esm/DocumentCollection.js.map +1 -1
  51. package/dist/esm/DocumentFindCursor.js +14 -8
  52. package/dist/esm/DocumentFindCursor.js.map +1 -1
  53. package/dist/esm/FindCursor.js +13 -7
  54. package/dist/esm/FindCursor.js.map +1 -1
  55. package/dist/esm/ListCollectionsCursor.js +8 -2
  56. package/dist/esm/ListCollectionsCursor.js.map +1 -1
  57. package/dist/esm/MongoClient.js +15 -4
  58. package/dist/esm/MongoClient.js.map +1 -1
  59. package/dist/esm/internal/schema.js +2 -2
  60. package/dist/esm/internal/schema.js.map +1 -1
  61. package/dist/esm/internal/version.js +1 -1
  62. package/package.json +1 -1
  63. package/src/AggregationCursor.ts +20 -4
  64. package/src/Collection.ts +34 -13
  65. package/src/Db.ts +9 -7
  66. package/src/DocumentAggregationCursor.ts +16 -3
  67. package/src/DocumentCollection.ts +23 -8
  68. package/src/DocumentFindCursor.ts +23 -9
  69. package/src/FindCursor.ts +22 -8
  70. package/src/ListCollectionsCursor.ts +20 -5
  71. package/src/MongoClient.ts +14 -8
  72. package/src/internal/schema.ts +2 -2
  73. package/src/internal/version.ts +1 -1
@@ -4,6 +4,8 @@
4
4
  import * as Data from "effect/Data"
5
5
  import * as Effect from "effect/Effect"
6
6
  import * as F from "effect/Function"
7
+ import type { Pipeable } from "effect/Pipeable"
8
+ import { pipeArguments } from "effect/Pipeable"
7
9
  import type * as Schema from "effect/Schema"
8
10
  import * as Stream from "effect/Stream"
9
11
  import type { Document, Filter, FindCursor as MongoFindCursor, Sort, SortDirection } from "mongodb"
@@ -11,9 +13,21 @@ import * as FindCursor from "./FindCursor.js"
11
13
  import { mongoErrorOrDie } from "./internal/mongo-error.js"
12
14
  import * as MongoError from "./MongoError.js"
13
15
 
14
- export class DocumentFindCursor extends Data.TaggedClass("DocumentFindCursor")<{
16
+ type DocumentFindCursorFields = {
15
17
  cursor: MongoFindCursor<Document>
16
- }> {
18
+ }
19
+
20
+ export interface DocumentFindCursor extends DocumentFindCursorFields, Pipeable {
21
+ _tag: "DocumentFindCursor"
22
+ }
23
+
24
+ /** @internal */
25
+ export class DocumentFindCursorImpl extends Data.TaggedClass("DocumentFindCursor")<DocumentFindCursorFields>
26
+ implements DocumentFindCursor
27
+ {
28
+ pipe() {
29
+ return pipeArguments(this, arguments)
30
+ }
17
31
  }
18
32
 
19
33
  export const filter: {
@@ -22,7 +36,7 @@ export const filter: {
22
36
  } = F.dual(
23
37
  (args) => isDocumentFindCursor(args[0]),
24
38
  (cursor: DocumentFindCursor, filter: Filter<Document>): DocumentFindCursor =>
25
- new DocumentFindCursor({ cursor: cursor.cursor.filter(filter) })
39
+ new DocumentFindCursorImpl({ cursor: cursor.cursor.filter(filter) })
26
40
  )
27
41
 
28
42
  export const project: {
@@ -31,7 +45,7 @@ export const project: {
31
45
  } = F.dual(
32
46
  (args) => isDocumentFindCursor(args[0]),
33
47
  (cursor: DocumentFindCursor, value: Document): DocumentFindCursor =>
34
- new DocumentFindCursor({ cursor: cursor.cursor.project(value) })
48
+ new DocumentFindCursorImpl({ cursor: cursor.cursor.project(value) })
35
49
  )
36
50
 
37
51
  export const sort: {
@@ -40,7 +54,7 @@ export const sort: {
40
54
  } = F.dual(
41
55
  (args) => isDocumentFindCursor(args[0]),
42
56
  (cursor: DocumentFindCursor, sort: Sort | string, direction?: SortDirection): DocumentFindCursor =>
43
- new DocumentFindCursor({ cursor: cursor.cursor.sort(sort, direction) })
57
+ new DocumentFindCursorImpl({ cursor: cursor.cursor.sort(sort, direction) })
44
58
  )
45
59
 
46
60
  export const limit: {
@@ -49,7 +63,7 @@ export const limit: {
49
63
  } = F.dual(
50
64
  (args) => isDocumentFindCursor(args[0]),
51
65
  (cursor: DocumentFindCursor, value: number): DocumentFindCursor =>
52
- new DocumentFindCursor({ cursor: cursor.cursor.limit(value) })
66
+ new DocumentFindCursorImpl({ cursor: cursor.cursor.limit(value) })
53
67
  )
54
68
 
55
69
  export const toArray = (cursor: DocumentFindCursor): Effect.Effect<Array<Document>, MongoError.MongoError> =>
@@ -72,13 +86,13 @@ export const typed: {
72
86
  } = F.dual((args) => isDocumentFindCursor(args[0]), <A, I = A, R = never>(
73
87
  cursor: DocumentFindCursor,
74
88
  schema: Schema.Schema<A, I, R>
75
- ): FindCursor.FindCursor<A, I, R> => new FindCursor.FindCursor<A, I, R>({ cursor: cursor.cursor, schema }))
89
+ ): FindCursor.FindCursor<A, I, R> => new FindCursor.FindCursorImpl<A, I, R>({ cursor: cursor.cursor, schema }))
76
90
 
77
- const isDocumentFindCursor = (x: unknown) => x instanceof DocumentFindCursor
91
+ const isDocumentFindCursor = (x: unknown) => x instanceof DocumentFindCursorImpl
78
92
 
79
93
  const errorSource = (cursor: DocumentFindCursor, functionName: string) =>
80
94
  new MongoError.CollectionErrorSource({
81
- module: DocumentFindCursor.name,
95
+ module: DocumentFindCursorImpl.name,
82
96
  functionName,
83
97
  db: cursor.cursor.namespace.db,
84
98
  collection: cursor.cursor.namespace.collection ?? "NO_COLLECTION_NAME"
package/src/FindCursor.ts CHANGED
@@ -6,6 +6,8 @@ import * as Effect from "effect/Effect"
6
6
  import type * as E from "effect/Either"
7
7
  import * as F from "effect/Function"
8
8
  import type * as ParseResult from "effect/ParseResult"
9
+ import type { Pipeable } from "effect/Pipeable"
10
+ import { pipeArguments } from "effect/Pipeable"
9
11
  import * as Schema from "effect/Schema"
10
12
  import * as Stream from "effect/Stream"
11
13
  import * as Tuple from "effect/Tuple"
@@ -14,10 +16,22 @@ import type { Filter } from "./internal/filter.js"
14
16
  import { mongoErrorOrDie } from "./internal/mongo-error.js"
15
17
  import * as MongoError from "./MongoError.js"
16
18
 
17
- export class FindCursor<A, I = A, R = never> extends Data.TaggedClass("FindCursor")<{
19
+ type FindCursorFields<A, I = A, R = never> = {
18
20
  cursor: FindCursor_<unknown>
19
21
  schema: Schema.Schema<A, I, R>
20
- }> {
22
+ }
23
+
24
+ export interface FindCursor<A, I = A, R = never> extends FindCursorFields<A, I, R>, Pipeable {
25
+ _tag: "FindCursor"
26
+ }
27
+
28
+ /** @internal */
29
+ export class FindCursorImpl<A, I = A, R = never> extends Data.TaggedClass("FindCursor")<FindCursorFields<A, I, R>>
30
+ implements FindCursor<A, I, R>
31
+ {
32
+ pipe() {
33
+ return pipeArguments(this, arguments)
34
+ }
21
35
  }
22
36
 
23
37
  export const filter: {
@@ -28,7 +42,7 @@ export const filter: {
28
42
  <A, I extends Document, R>(
29
43
  cursor: FindCursor<A, I, R>,
30
44
  filter: Filter<I>
31
- ): FindCursor<A, I, R> => new FindCursor({ cursor: cursor.cursor.filter(filter), schema: cursor.schema })
45
+ ): FindCursor<A, I, R> => new FindCursorImpl({ cursor: cursor.cursor.filter(filter), schema: cursor.schema })
32
46
  )
33
47
 
34
48
  export const project: {
@@ -47,7 +61,7 @@ export const project: {
47
61
  cursor: FindCursor<A, I, R>,
48
62
  newSchema: Schema.Schema<B, BI, BR>,
49
63
  value: T
50
- ): FindCursor<B, BI, BR> => new FindCursor({ cursor: cursor.cursor.project(value), schema: newSchema })
64
+ ): FindCursor<B, BI, BR> => new FindCursorImpl({ cursor: cursor.cursor.project(value), schema: newSchema })
51
65
  )
52
66
 
53
67
  export const sort: {
@@ -63,7 +77,7 @@ export const sort: {
63
77
  cursor: FindCursor<A, I, R>,
64
78
  sort: Sort | string,
65
79
  direction?: SortDirection
66
- ): FindCursor<A, I, R> => new FindCursor({ cursor: cursor.cursor.sort(sort, direction), schema: cursor.schema })
80
+ ): FindCursor<A, I, R> => new FindCursorImpl({ cursor: cursor.cursor.sort(sort, direction), schema: cursor.schema })
67
81
  )
68
82
 
69
83
  export const limit: {
@@ -72,7 +86,7 @@ export const limit: {
72
86
  } = F.dual(
73
87
  (args) => isFindCursor(args[0]),
74
88
  <A, I, R>(cursor: FindCursor<A, I, R>, value: number): FindCursor<A, I, R> =>
75
- new FindCursor({ cursor: cursor.cursor.limit(value), schema: cursor.schema })
89
+ new FindCursorImpl({ cursor: cursor.cursor.limit(value), schema: cursor.schema })
76
90
  )
77
91
 
78
92
  export const toArray = <A, I, R>(
@@ -134,11 +148,11 @@ export const toStreamEither = <A, I, R>(
134
148
  )
135
149
  }
136
150
 
137
- const isFindCursor = (x: unknown): x is FindCursor<unknown> => x instanceof FindCursor
151
+ const isFindCursor = (x: unknown): x is FindCursor<unknown> => x instanceof FindCursorImpl
138
152
 
139
153
  const errorSource = <A, I, R>(cursor: FindCursor<A, I, R>, functionName: string) =>
140
154
  new MongoError.CollectionErrorSource({
141
- module: FindCursor.name,
155
+ module: FindCursorImpl.name,
142
156
  functionName,
143
157
  db: cursor.cursor.namespace.db,
144
158
  collection: cursor.cursor.namespace.collection ?? "NO_COLLECTION_NAME"
@@ -4,6 +4,8 @@
4
4
  import * as Data from "effect/Data"
5
5
  import * as Effect from "effect/Effect"
6
6
  import * as F from "effect/Function"
7
+ import type { Pipeable } from "effect/Pipeable"
8
+ import { pipeArguments } from "effect/Pipeable"
7
9
  import * as Stream from "effect/Stream"
8
10
  import type {
9
11
  CollectionInfo as MongoCollectionInfo,
@@ -17,11 +19,24 @@ export type FullCollectionInfo = MongoCollectionInfo
17
19
 
18
20
  type DefaultCollectionInfo = NameOnlyCollectionInfo | FullCollectionInfo
19
21
 
20
- export class ListCollectionsCursor<
21
- T extends DefaultCollectionInfo = DefaultCollectionInfo
22
- > extends Data.TaggedClass("ListCollectionsCursor")<{
22
+ type ListCollectionsCursorFields<T extends DefaultCollectionInfo = DefaultCollectionInfo> = {
23
23
  cursor: MongoListCollectionsCursor<T>
24
- }> {}
24
+ }
25
+
26
+ export interface ListCollectionsCursor<T extends DefaultCollectionInfo = DefaultCollectionInfo>
27
+ extends ListCollectionsCursorFields<T>, Pipeable
28
+ {
29
+ _tag: "ListCollectionsCursor"
30
+ }
31
+
32
+ /** @internal */
33
+ export class ListCollectionsCursorImpl<
34
+ T extends DefaultCollectionInfo = DefaultCollectionInfo
35
+ > extends Data.TaggedClass("ListCollectionsCursor")<ListCollectionsCursorFields<T>> implements Pipeable {
36
+ pipe() {
37
+ return pipeArguments(this, arguments)
38
+ }
39
+ }
25
40
 
26
41
  export type NameOnlyListCollectionsCursor = ListCollectionsCursor<
27
42
  Pick<MongoCollectionInfo, "name" | "type">
@@ -47,7 +62,7 @@ export const toStream = <T extends DefaultCollectionInfo>(
47
62
 
48
63
  const errorSource = (cursor: ListCollectionsCursor, functionName: string) =>
49
64
  new MongoError.CollectionErrorSource({
50
- module: ListCollectionsCursor.name,
65
+ module: ListCollectionsCursorImpl.name,
51
66
  functionName,
52
67
  db: cursor.cursor.namespace.db,
53
68
  collection: cursor.cursor.namespace.collection ?? "NO_COLLECTION_NAME"
@@ -1,21 +1,24 @@
1
1
  /**
2
2
  * @since 0.0.1
3
3
  */
4
+ import * as Data from "effect/Data"
4
5
  import * as Effect from "effect/Effect"
5
6
  import * as F from "effect/Function"
6
7
  import type * as Scope from "effect/Scope"
7
- import type { Db, DbOptions, MongoClientOptions } from "mongodb"
8
+ import type { DbOptions, MongoClientOptions } from "mongodb"
8
9
  import { MongoClient as MongoClient_ } from "mongodb"
10
+ import * as Db from "./Db.js"
9
11
  import { mongoErrorOrDie } from "./internal/mongo-error.js"
10
12
  import * as MongoError from "./MongoError.js"
11
13
 
12
- export type MongoClient = MongoClient_
14
+ export class MongoClient extends Data.TaggedClass("MongoClient")<{ client: MongoClient_ }> {}
13
15
 
14
16
  export const connect = (
15
17
  url: string,
16
18
  options?: MongoClientOptions
17
19
  ): Effect.Effect<MongoClient, MongoError.MongoError> =>
18
20
  Effect.promise(() => MongoClient_.connect(url, options)).pipe(
21
+ Effect.map((client) => new MongoClient({ client })),
19
22
  Effect.catchAllDefect(mongoErrorOrDie(errorSource([new URL(url).host], "connect")))
20
23
  )
21
24
 
@@ -24,15 +27,17 @@ export const close: {
24
27
  (client: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError>
25
28
  } = F.dual(
26
29
  (args) => isMongoClient(args[0]),
27
- (client: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError> =>
30
+ ({ client }: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError> =>
28
31
  Effect.promise(() => client.close(force)).pipe(
29
32
  Effect.catchAllDefect(mongoErrorOrDie(errorSource(client.options.hosts.map((x) => x.host ?? "NO_HOST"), "close")))
30
33
  )
31
34
  )
32
35
 
36
+ export type MongoClientScopedOptions = MongoClientOptions & { forceClose?: boolean }
37
+
33
38
  export const connectScoped = (
34
39
  url: string,
35
- options?: MongoClientOptions & { forceClose?: boolean }
40
+ options?: MongoClientScopedOptions
36
41
  ): Effect.Effect<MongoClient, MongoError.MongoError, Scope.Scope> =>
37
42
  Effect.acquireRelease(
38
43
  connect(url, options),
@@ -40,14 +45,15 @@ export const connectScoped = (
40
45
  )
41
46
 
42
47
  export const db: {
43
- (dbName?: string, options?: DbOptions): (client: MongoClient) => Db
44
- (client: MongoClient, dbName?: string, options?: DbOptions): Db
48
+ (dbName?: string, options?: DbOptions): (client: MongoClient) => Db.Db
49
+ (client: MongoClient, dbName?: string, options?: DbOptions): Db.Db
45
50
  } = F.dual(
46
51
  (args) => isMongoClient(args[0]),
47
- (client: MongoClient, dbName?: string, options?: DbOptions): Db => client.db(dbName, options)
52
+ ({ client }: MongoClient, dbName?: string, options?: DbOptions): Db.Db =>
53
+ new Db.Db({ db: client.db(dbName, options) })
48
54
  )
49
55
 
50
- const isMongoClient = (x: unknown) => x instanceof MongoClient_
56
+ const isMongoClient = (x: unknown) => x instanceof MongoClient
51
57
 
52
58
  const errorSource = (hosts: Array<string>, functionName: string) =>
53
59
  new MongoError.ClientErrorSource({ module: "MongoClient", functionName, hosts })
@@ -5,8 +5,8 @@ import type { Document } from "mongodb"
5
5
 
6
6
  // TODO: there is probably a Schema to do this decode or using typeclass package for traverse
7
7
  export const decodeNullableDocument = <A, I, R>(schema: Schema.Schema<A, I, R>, value: Document | null) =>
8
- Effect.gen(function*(_) {
8
+ Effect.gen(function*() {
9
9
  if (value === null) return O.none()
10
- const decoded = yield* _(Schema.decodeUnknown(schema)(value))
10
+ const decoded = yield* Schema.decodeUnknown(schema)(value)
11
11
  return O.some(decoded)
12
12
  })
@@ -1,4 +1,4 @@
1
- let moduleVersion = "0.1.3"
1
+ let moduleVersion = "0.2.0"
2
2
 
3
3
  export const getCurrentVersion = () => moduleVersion
4
4