Skip to content

Commit ec45cb5

Browse files
committed
effect-mongodb - MongoClient: wrap mongodb driver MongoClient type into a TaggedClass
1 parent 2a9a030 commit ec45cb5

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

.changeset/late-penguins-invent.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect-mongodb": minor
3+
---
4+
5+
Wrap mongodb driver MongoClient type into a TaggedClass

packages/effect-mongodb/src/MongoClient.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @since 0.0.1
33
*/
4+
import * as Data from "effect/Data"
45
import * as Effect from "effect/Effect"
56
import * as F from "effect/Function"
67
import type * as Scope from "effect/Scope"
@@ -10,13 +11,14 @@ import * as Db from "./Db.js"
1011
import { mongoErrorOrDie } from "./internal/mongo-error.js"
1112
import * as MongoError from "./MongoError.js"
1213

13-
export type MongoClient = MongoClient_
14+
export class MongoClient extends Data.TaggedClass("MongoClient")<{ client: MongoClient_ }> {}
1415

1516
export const connect = (
1617
url: string,
1718
options?: MongoClientOptions
1819
): Effect.Effect<MongoClient, MongoError.MongoError> =>
1920
Effect.promise(() => MongoClient_.connect(url, options)).pipe(
21+
Effect.map((client) => new MongoClient({ client })),
2022
Effect.catchAllDefect(mongoErrorOrDie(errorSource([new URL(url).host], "connect")))
2123
)
2224

@@ -25,7 +27,7 @@ export const close: {
2527
(client: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError>
2628
} = F.dual(
2729
(args) => isMongoClient(args[0]),
28-
(client: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError> =>
30+
({ client }: MongoClient, force?: boolean): Effect.Effect<void, MongoError.MongoError> =>
2931
Effect.promise(() => client.close(force)).pipe(
3032
Effect.catchAllDefect(mongoErrorOrDie(errorSource(client.options.hosts.map((x) => x.host ?? "NO_HOST"), "close")))
3133
)
@@ -47,7 +49,8 @@ export const db: {
4749
(client: MongoClient, dbName?: string, options?: DbOptions): Db.Db
4850
} = F.dual(
4951
(args) => isMongoClient(args[0]),
50-
(client: MongoClient, dbName?: string, options?: DbOptions): Db.Db => new Db.Db({ db: client.db(dbName, options) })
52+
({ client }: MongoClient, dbName?: string, options?: DbOptions): Db.Db =>
53+
new Db.Db({ db: client.db(dbName, options) })
5154
)
5255

5356
const isMongoClient = (x: unknown) => x instanceof MongoClient_

packages/services/src/DbInstance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as MongoClient from "effect-mongodb/MongoClient"
55
import * as Effect from "effect/Effect"
66
import * as F from "effect/Function"
77
import * as Layer from "effect/Layer"
8-
import type { DbOptions } from "mongodb"
8+
import type { DbOptions, MongoClient as MongoClient_ } from "mongodb"
99
import * as DbService from "./DbService.js"
1010
import * as MongoClientService from "./MongoClientService.js"
1111

@@ -36,7 +36,7 @@ export const layer = <DbK extends string>(
3636
return dbLayer.pipe(Layer.provide(defaultClientLayer))
3737
}
3838

39-
type DbInstanceOptionsWithClient = Pick<DbInstanceOptions, "database"> & { client: MongoClient.MongoClient }
39+
type DbInstanceOptionsWithClient = Pick<DbInstanceOptions, "database"> & { client: MongoClient_ }
4040

4141
export const fromMongoClient = <DbK extends string, E = never, R = never>(
4242
dbTag: DbService.TagType<DbK>,
@@ -45,7 +45,8 @@ export const fromMongoClient = <DbK extends string, E = never, R = never>(
4545
Layer.effect(
4646
dbTag,
4747
Effect.gen(function*() {
48-
const { client, database: { name, ...dbOptions } } = yield* options
48+
const { client: client_, database: { name, ...dbOptions } } = yield* options
49+
const client = new MongoClient.MongoClient({ client: client_ })
4950
const db = MongoClient.db(client, name, dbOptions)
5051
return dbTag.of(db as DbService.DbService<DbK>)
5152
})

0 commit comments

Comments
 (0)