Skip to content

Commit 4287f85

Browse files
committed
packages: move effect-mongodb services-related code to new services package
1 parent d73f898 commit 4287f85

File tree

6 files changed

+72
-46
lines changed

6 files changed

+72
-46
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect-mongodb": patch
3+
---
4+
5+
Remove services related types from Db and MongoClient that are moved to the new package `@effect-mongodb/services`

packages/effect-mongodb/src/Db.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
/**
22
* @since 0.0.1
33
*/
4-
import type * as Brand from "effect/Brand"
5-
import * as Context from "effect/Context"
64
import * as Effect from "effect/Effect"
75
import * as F from "effect/Function"
8-
import * as Layer from "effect/Layer"
96
import type * as Schema from "effect/Schema"
107
import type { Document, DropCollectionOptions, ListCollectionsOptions } from "mongodb"
118
import { Db } from "mongodb"
129
import type * as Collection from "./Collection.js"
1310
import * as DocumentCollection from "./DocumentCollection.js"
1411
import * as ListCollectionsCursor from "./ListCollectionsCursor.js"
15-
import * as MongoClient from "./MongoClient.js"
1612
import * as MongoError from "./MongoError.js"
1713

1814
export const documentCollection: {
@@ -89,35 +85,3 @@ export const dropCollection: {
8985
)
9086

9187
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))

packages/effect-mongodb/src/MongoClient.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
/**
22
* @since 0.0.1
33
*/
4-
import type * as Brand from "effect/Brand"
54
import * as Effect from "effect/Effect"
65
import * as F from "effect/Function"
76
import type { Db, DbOptions, MongoClientOptions } from "mongodb"
87
import { MongoClient as MongoClient_ } from "mongodb"
98
import * as MongoError from "./MongoError.js"
109

11-
import * as Context from "effect/Context"
12-
1310
export type MongoClient = MongoClient_
1411

1512
export const connect = (
@@ -29,10 +26,3 @@ export const db: {
2926
)
3027

3128
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>>

packages/services/src/Db.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @since 0.0.1
3+
*/
4+
import * as MongoClient from "effect-mongodb/MongoClient"
5+
import type * as MongoError from "effect-mongodb/MongoError"
6+
import type * as Brand from "effect/Brand"
7+
import * as Context from "effect/Context"
8+
import * as Effect from "effect/Effect"
9+
import * as Layer from "effect/Layer"
10+
import type { Db } from "mongodb"
11+
import type * as MongoClientService from "./MongoClient.js"
12+
13+
export type DbService<K extends string> = {
14+
db: Effect.Effect<Db, MongoError.MongoError>
15+
} & Brand.Brand<K>
16+
17+
export const Tag = <K extends string>(key: K) => Context.GenericTag<DbService<K>>(key)
18+
export type TagType<K extends string> = ReturnType<typeof Tag<K>>
19+
20+
export const fromEffect = <DbK extends string, MongoClientK extends string, E = never, R = never>(
21+
dbTag: TagType<DbK>,
22+
clientTag: MongoClientService.TagType<MongoClientK>,
23+
dbName: Effect.Effect<string, E, R>
24+
) =>
25+
Layer.effect(
26+
dbTag,
27+
Effect.gen(function*(_) {
28+
const { client } = yield* _(clientTag)
29+
const dbName_ = yield* _(dbName)
30+
const db = yield* _(
31+
client,
32+
Effect.map((client) => MongoClient.db(client, dbName_)),
33+
Effect.cached
34+
)
35+
return dbTag.of({ db } as DbService<DbK>) // TODO fix cast using branded ctor
36+
})
37+
)
38+
39+
export const fromConst = <DbK extends string, MongoClientK extends string>(
40+
dbTag: TagType<DbK>,
41+
clientTag: MongoClientService.TagType<MongoClientK>,
42+
dbName: string
43+
) => fromEffect(dbTag, clientTag, Effect.succeed(dbName))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @since 0.0.1
3+
*/
4+
import type * as MongoClient from "effect-mongodb/MongoClient"
5+
import type * as MongoError from "effect-mongodb/MongoError"
6+
import type * as Brand from "effect/Brand"
7+
import * as Context from "effect/Context"
8+
import type * as Effect from "effect/Effect"
9+
10+
export type MongoClientService<K extends string> = {
11+
client: Effect.Effect<MongoClient.MongoClient, MongoError.MongoError>
12+
} & Brand.Brand<K>
13+
14+
export const Tag = <K extends string>(key: K) => Context.GenericTag<MongoClientService<K>>(key)
15+
export type TagType<K extends string> = ReturnType<typeof Tag<K>>

packages/services/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @since 0.0.1
3+
*/
4+
export * as Db from "./Db.js"
5+
6+
/**
7+
* @since 0.0.1
8+
*/
9+
export * as MongoClient from "./MongoClient.js"

0 commit comments

Comments
 (0)