Skip to content

Commit 941a52a

Browse files
committed
services - DbInstance: add explicit return type on public code + type tests (#44)
1 parent 6dbb3d7 commit 941a52a

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as DbInstance from "@effect-mongodb/services/DbInstance"
2+
import type * as DbService from "@effect-mongodb/services/DbService"
3+
import * as Config from "effect/Config"
4+
import type * as Context from "effect/Context"
5+
import * as Effect from "effect/Effect"
6+
import * as F from "effect/Function"
7+
import type { MongoClient as NativeMongoClient } from "mongodb"
8+
9+
declare const db: DbService.Tag<"MyDb">
10+
11+
type SomeService = { url: string; name: string }
12+
declare const SomeService: Context.Tag<SomeService, SomeService>
13+
14+
// -------------------------------------------------------------------------------------
15+
// layerEffect
16+
// -------------------------------------------------------------------------------------
17+
18+
// $ExpectType Layer<DbService<"MyDb">, MongoError, never>
19+
DbInstance.layerEffect(db, Effect.succeed({ database: { name: "mydb" }, client: { url: "mongodb://localhost:27017" } }))
20+
21+
// $ExpectType Layer<DbService<"MyDb">, MongoError | ConfigError, never>
22+
DbInstance.layerEffect(
23+
db,
24+
Effect.gen(function*() {
25+
const databaseName = yield* Config.string("MONGO_DATABASE_NAME")
26+
const clientUrl = yield* Config.string("MONGO_CONNECTION_STRING")
27+
return { database: { name: databaseName }, client: { url: clientUrl } }
28+
})
29+
)
30+
31+
const withRequirements = Effect.gen(function*() {
32+
const { url } = yield* SomeService
33+
const name = yield* Config.string("MONGO_DATABASE_NAME")
34+
return { database: { name }, client: { url } }
35+
})
36+
// $ExpectType Layer<DbService<"MyDb">, MongoError | ConfigError, SomeService>
37+
DbInstance.layerEffect(db, withRequirements)
38+
39+
// -------------------------------------------------------------------------------------
40+
// layer
41+
// -------------------------------------------------------------------------------------
42+
43+
// $ExpectType Layer<DbService<"MyDb">, MongoError, never>
44+
DbInstance.layer(db, { database: { name: "mydb" }, client: { url: "mongodb://localhost:27017" } })
45+
46+
// -------------------------------------------------------------------------------------
47+
// fromMongoClient
48+
// -------------------------------------------------------------------------------------
49+
50+
declare const legacyClient: NativeMongoClient
51+
52+
// $ExpectType Layer<DbService<"MyDb">, never, never>
53+
DbInstance.fromMongoClient(db, Effect.succeed({ database: { name: "mydb" }, client: legacyClient }))
54+
55+
// $ExpectType Layer<DbService<"MyDb">, ConfigError, never>
56+
DbInstance.fromMongoClient(
57+
db,
58+
F.pipe(
59+
Config.string("MONGO_DATABASE_NAME"),
60+
Effect.map((name) => ({ database: { name }, client: legacyClient }))
61+
)
62+
)
63+
64+
// $ExpectType Layer<DbService<"MyDb">, ConfigError, SomeService>
65+
DbInstance.fromMongoClient(
66+
db,
67+
Effect.gen(function*() {
68+
const { name: dbName } = yield* SomeService
69+
const name = yield* Config.string(dbName)
70+
return { database: { name }, client: legacyClient }
71+
})
72+
)

packages/services/dtslint/MongoClientService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ MongoClientService.Tag("MyMongoClient")
2323
// $ExpectType Layer<MongoClientService<"MyMongoClient">, MongoError, never>
2424
MongoClientService.layerEffect(mongoClient, Effect.succeed("mongodb://localhost:27017"))
2525

26-
// $ExpectType Layer<MongoClientService<"MyMongoClient">, ConfigError | MongoError, never>
26+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, MongoError | ConfigError, never>
2727
MongoClientService.layerEffect(mongoClient, Config.string("DATABASE_NAME"))
2828

2929
const withRequirements = SomeService.pipe(Effect.flatMap(({ url }) => Config.string(url)))
30-
// $ExpectType Layer<MongoClientService<"MyMongoClient">, ConfigError | MongoError, SomeService>
30+
// $ExpectType Layer<MongoClientService<"MyMongoClient">, MongoError | ConfigError, SomeService>
3131
MongoClientService.layerEffect(mongoClient, withRequirements)
3232

3333
// -------------------------------------------------------------------------------------

packages/services/src/DbInstance.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* @since 0.0.1
33
*/
44
import * as MongoClient from "effect-mongodb/MongoClient"
5+
import type * as MongoError from "effect-mongodb/MongoError"
56
import * as Effect from "effect/Effect"
67
import * as F from "effect/Function"
78
import * as Layer from "effect/Layer"
@@ -17,7 +18,7 @@ type DbInstanceOptions = {
1718
export const layerEffect = <DbK extends string, E = never, R = never>(
1819
dbTag: DbService.Tag<DbK>,
1920
options: Effect.Effect<DbInstanceOptions, E, R>
20-
) =>
21+
): Layer.Layer<DbService.DbService<DbK>, MongoError.MongoError | E, R> =>
2122
F.pipe(
2223
options,
2324
Effect.map((options) => layer(dbTag, options)),
@@ -27,7 +28,7 @@ export const layerEffect = <DbK extends string, E = never, R = never>(
2728
export const layer = <DbK extends string>(
2829
dbTag: DbService.Tag<DbK>,
2930
options: DbInstanceOptions
30-
) => {
31+
): Layer.Layer<DbService.DbService<DbK>, MongoError.MongoError> => {
3132
const { name: databaseName, ...databaseOptions } = options.database
3233
const { url: clientUrl, ...clientOptions } = options.client
3334

@@ -41,7 +42,7 @@ type DbInstanceOptionsWithClient = Pick<DbInstanceOptions, "database"> & { clien
4142
export const fromMongoClient = <DbK extends string, E = never, R = never>(
4243
dbTag: DbService.Tag<DbK>,
4344
options: Effect.Effect<DbInstanceOptionsWithClient, E, R>
44-
) =>
45+
): Layer.Layer<DbService.DbService<DbK>, E, R> =>
4546
Layer.effect(
4647
dbTag,
4748
Effect.gen(function*() {

0 commit comments

Comments
 (0)