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.
- package/README.md +25 -19
- package/dist/cjs/AggregationCursor.js +10 -4
- package/dist/cjs/AggregationCursor.js.map +1 -1
- package/dist/cjs/Collection.js +19 -14
- package/dist/cjs/Collection.js.map +1 -1
- package/dist/cjs/Db.js +11 -9
- package/dist/cjs/Db.js.map +1 -1
- package/dist/cjs/DocumentAggregationCursor.js +10 -4
- package/dist/cjs/DocumentAggregationCursor.js.map +1 -1
- package/dist/cjs/DocumentCollection.js +15 -9
- package/dist/cjs/DocumentCollection.js.map +1 -1
- package/dist/cjs/DocumentFindCursor.js +16 -10
- package/dist/cjs/DocumentFindCursor.js.map +1 -1
- package/dist/cjs/FindCursor.js +15 -9
- package/dist/cjs/FindCursor.js.map +1 -1
- package/dist/cjs/ListCollectionsCursor.js +10 -4
- package/dist/cjs/ListCollectionsCursor.js.map +1 -1
- package/dist/cjs/MongoClient.js +17 -5
- package/dist/cjs/MongoClient.js.map +1 -1
- package/dist/cjs/internal/schema.js +2 -2
- package/dist/cjs/internal/schema.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/dts/AggregationCursor.d.ts +5 -5
- package/dist/dts/AggregationCursor.d.ts.map +1 -1
- package/dist/dts/Collection.d.ts +5 -6
- package/dist/dts/Collection.d.ts.map +1 -1
- package/dist/dts/Db.d.ts +9 -5
- package/dist/dts/Db.d.ts.map +1 -1
- package/dist/dts/DocumentAggregationCursor.d.ts +5 -5
- package/dist/dts/DocumentAggregationCursor.d.ts.map +1 -1
- package/dist/dts/DocumentCollection.d.ts +5 -5
- package/dist/dts/DocumentCollection.d.ts.map +1 -1
- package/dist/dts/DocumentFindCursor.d.ts +5 -5
- package/dist/dts/DocumentFindCursor.d.ts.map +1 -1
- package/dist/dts/FindCursor.d.ts +5 -5
- package/dist/dts/FindCursor.d.ts.map +1 -1
- package/dist/dts/ListCollectionsCursor.d.ts +5 -5
- package/dist/dts/ListCollectionsCursor.d.ts.map +1 -1
- package/dist/dts/MongoClient.d.ts +15 -9
- package/dist/dts/MongoClient.d.ts.map +1 -1
- package/dist/esm/AggregationCursor.js +8 -2
- package/dist/esm/AggregationCursor.js.map +1 -1
- package/dist/esm/Collection.js +17 -12
- package/dist/esm/Collection.js.map +1 -1
- package/dist/esm/Db.js +8 -7
- package/dist/esm/Db.js.map +1 -1
- package/dist/esm/DocumentAggregationCursor.js +8 -2
- package/dist/esm/DocumentAggregationCursor.js.map +1 -1
- package/dist/esm/DocumentCollection.js +13 -7
- package/dist/esm/DocumentCollection.js.map +1 -1
- package/dist/esm/DocumentFindCursor.js +14 -8
- package/dist/esm/DocumentFindCursor.js.map +1 -1
- package/dist/esm/FindCursor.js +13 -7
- package/dist/esm/FindCursor.js.map +1 -1
- package/dist/esm/ListCollectionsCursor.js +8 -2
- package/dist/esm/ListCollectionsCursor.js.map +1 -1
- package/dist/esm/MongoClient.js +15 -4
- package/dist/esm/MongoClient.js.map +1 -1
- package/dist/esm/internal/schema.js +2 -2
- package/dist/esm/internal/schema.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/package.json +1 -1
- package/src/AggregationCursor.ts +20 -4
- package/src/Collection.ts +34 -13
- package/src/Db.ts +9 -7
- package/src/DocumentAggregationCursor.ts +16 -3
- package/src/DocumentCollection.ts +23 -8
- package/src/DocumentFindCursor.ts +23 -9
- package/src/FindCursor.ts +22 -8
- package/src/ListCollectionsCursor.ts +20 -5
- package/src/MongoClient.ts +14 -8
- package/src/internal/schema.ts +2 -2
- 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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
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:
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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:
|
|
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
|
-
|
|
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:
|
|
65
|
+
module: ListCollectionsCursorImpl.name,
|
|
51
66
|
functionName,
|
|
52
67
|
db: cursor.cursor.namespace.db,
|
|
53
68
|
collection: cursor.cursor.namespace.collection ?? "NO_COLLECTION_NAME"
|
package/src/MongoClient.ts
CHANGED
|
@@ -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 {
|
|
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
|
|
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?:
|
|
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 =>
|
|
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
|
|
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 })
|
package/src/internal/schema.ts
CHANGED
|
@@ -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*
|
|
10
|
+
const decoded = yield* Schema.decodeUnknown(schema)(value)
|
|
11
11
|
return O.some(decoded)
|
|
12
12
|
})
|
package/src/internal/version.ts
CHANGED