Skip to content

Commit 89b1063

Browse files
committed
effect-mongodb - Db: wrap mongodb driver Db type into a TaggedClass (close #41)
1 parent ee4dd24 commit 89b1063

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

.changeset/lovely-ears-jog.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 Db type into a TaggedClass

packages/effect-mongodb/src/Db.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
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 Schema from "effect/Schema"
78
import type { Document, DropCollectionOptions, ListCollectionsOptions } from "mongodb"
8-
import { Db } from "mongodb"
9+
import { Db as Db_ } from "mongodb"
910
import type * as Collection from "./Collection.js"
1011
import * as DocumentCollection from "./DocumentCollection.js"
1112
import { mongoErrorOrDie } from "./internal/mongo-error.js"
1213
import * as ListCollectionsCursor from "./ListCollectionsCursor.js"
1314
import * as MongoError from "./MongoError.js"
1415

16+
export class Db extends Data.TaggedClass("Db")<{ db: Db_ }> {}
17+
1518
export const documentCollection: {
1619
(name: string): (db: Db) => DocumentCollection.DocumentCollection
1720
(db: Db, name: string): DocumentCollection.DocumentCollection
1821
} = F.dual(
1922
(args) => isDb(args[0]),
2023
(db: Db, name: string): DocumentCollection.DocumentCollection =>
2124
new DocumentCollection.DocumentCollectionImpl({
22-
collection: db.collection(name)
25+
collection: db.db.collection(name)
2326
})
2427
)
2528

@@ -67,7 +70,7 @@ export const listCollections: {
6770
} = F.dual(
6871
(args) => isDb(args[0]),
6972
(db: Db, filter?: Document, options?: ListCollectionsOptions): ListCollectionsCursor.ListCollectionsCursor =>
70-
new ListCollectionsCursor.ListCollectionsCursorImpl({ cursor: db.listCollections(filter, options) })
73+
new ListCollectionsCursor.ListCollectionsCursorImpl({ cursor: db.db.listCollections(filter, options) })
7174
)
7275

7376
export const dropCollection: {
@@ -79,12 +82,12 @@ export const dropCollection: {
7982
(args) => isDb(args[0]),
8083
(db: Db, name: string, options?: DropCollectionOptions): Effect.Effect<boolean, MongoError.MongoError> =>
8184
F.pipe(
82-
Effect.promise(() => db.dropCollection(name, options)),
85+
Effect.promise(() => db.db.dropCollection(name, options)),
8386
Effect.catchAllDefect(mongoErrorOrDie(errorSource(db, "dropCollection")))
8487
)
8588
)
8689

87-
const isDb = (x: unknown) => x instanceof Db
90+
const isDb = (x: unknown) => x instanceof Db_
8891

8992
const errorSource = (db: Db, functionName: string) =>
90-
new MongoError.DbErrorSource({ module: "Db", functionName, db: db.databaseName })
93+
new MongoError.DbErrorSource({ module: "Db", functionName, db: db.db.databaseName })

packages/effect-mongodb/src/MongoClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import * as Effect from "effect/Effect"
55
import * as F from "effect/Function"
66
import type * as Scope from "effect/Scope"
7-
import type { Db, DbOptions, MongoClientOptions } from "mongodb"
7+
import type { DbOptions, MongoClientOptions } from "mongodb"
88
import { MongoClient as MongoClient_ } from "mongodb"
9+
import * as Db from "./Db.js"
910
import { mongoErrorOrDie } from "./internal/mongo-error.js"
1011
import * as MongoError from "./MongoError.js"
1112

@@ -42,11 +43,11 @@ export const connectScoped = (
4243
)
4344

4445
export const db: {
45-
(dbName?: string, options?: DbOptions): (client: MongoClient) => Db
46-
(client: MongoClient, dbName?: string, options?: DbOptions): Db
46+
(dbName?: string, options?: DbOptions): (client: MongoClient) => Db.Db
47+
(client: MongoClient, dbName?: string, options?: DbOptions): Db.Db
4748
} = F.dual(
4849
(args) => isMongoClient(args[0]),
49-
(client: MongoClient, dbName?: string, options?: DbOptions): Db => client.db(dbName, options)
50+
(client: MongoClient, dbName?: string, options?: DbOptions): Db.Db => new Db.Db({ db: client.db(dbName, options) })
5051
)
5152

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

packages/effect-mongodb/test/support/describe-mongo.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import * as Db from "effect-mongodb/Db"
12
import * as Effect from "effect/Effect"
2-
import type { Db } from "mongodb"
3+
import type { Db as Db_ } from "mongodb"
34
import { MongoClient } from "mongodb"
45
import { afterAll, beforeAll, describe, inject } from "vitest"
56

67
type MongoContext = {
78
client: Effect.Effect<MongoClient>
8-
database: Effect.Effect<Db>
9+
database: Effect.Effect<Db.Db>
910
_client: () => MongoClient
10-
_database: () => Db
11+
_database: () => Db_
1112
}
1213

1314
export const describeMongo = (
@@ -16,7 +17,7 @@ export const describeMongo = (
1617
) => {
1718
describe(suiteName, () => {
1819
let client: MongoClient
19-
let database: Db
20+
let database: Db_
2021
let databaseName: string
2122

2223
beforeAll(async () => {
@@ -41,7 +42,7 @@ export const describeMongo = (
4142
_client: () => client,
4243
_database: () => database,
4344
client: Effect.sync(() => client),
44-
database: Effect.sync(() => database)
45+
database: Effect.sync(() => new Db.Db({ db: database }))
4546
})
4647
})
4748
}

packages/services/src/DbService.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
/**
22
* @since 0.0.1
33
*/
4+
import type * as Db from "effect-mongodb/Db"
45
import * as MongoClient from "effect-mongodb/MongoClient"
56
import type * as Brand from "effect/Brand"
67
import * as Context from "effect/Context"
78
import * as Effect from "effect/Effect"
89
import * as Layer from "effect/Layer"
9-
import type { Db, DbOptions } from "mongodb"
10+
import type { DbOptions } from "mongodb"
1011
import type * as MongoClientService from "./MongoClientService.js"
1112

12-
export type DbService<K extends string> = Db & Brand.Brand<K>
13+
export type DbService<K extends string> = Db.Db & Brand.Brand<K>
1314

1415
export const Tag = <K extends string>(key: K) => Context.GenericTag<DbService<K>>(key)
1516
export type TagType<K extends string> = ReturnType<typeof Tag<K>>

packages/services/test/support/describe-mongo.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import * as Db from "effect-mongodb/Db"
12
import * as Effect from "effect/Effect"
2-
import type { Db } from "mongodb"
3+
import type { Db as Db_ } from "mongodb"
34
import { MongoClient } from "mongodb"
45
import { afterAll, beforeAll, describe, inject } from "vitest"
56

67
type MongoContext = {
78
client: Effect.Effect<MongoClient>
8-
database: Effect.Effect<Db>
9+
database: Effect.Effect<Db.Db>
910
_client: () => MongoClient
10-
_database: () => Db
11+
_database: () => Db_
1112
}
1213

1314
export const describeMongo = (
@@ -16,7 +17,7 @@ export const describeMongo = (
1617
) => {
1718
describe(suiteName, () => {
1819
let client: MongoClient
19-
let database: Db
20+
let database: Db_
2021
let databaseName: string
2122

2223
beforeAll(async () => {
@@ -41,7 +42,7 @@ export const describeMongo = (
4142
_client: () => client,
4243
_database: () => database,
4344
client: Effect.sync(() => client),
44-
database: Effect.sync(() => database)
45+
database: Effect.sync(() => new Db.Db({ db: database }))
4546
})
4647
})
4748
}

0 commit comments

Comments
 (0)