Skip to content

Commit ee4dd24

Browse files
committed
services - examples: using DbInstance multiple times with different connection strings (#36)
1 parent d21b767 commit ee4dd24

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as DbInstance from "@effect-mongodb/services/DbInstance"
2+
import * as DbService from "@effect-mongodb/services/DbService"
3+
import * as Collection from "effect-mongodb/Collection"
4+
import * as Db from "effect-mongodb/Db"
5+
import * as FindCursor from "effect-mongodb/FindCursor"
6+
import * as Effect from "effect/Effect"
7+
import * as Schema from "effect/Schema"
8+
9+
const Todo = Schema.Struct({
10+
userId: Schema.Number,
11+
id: Schema.Number,
12+
title: Schema.String,
13+
completed: Schema.Boolean
14+
})
15+
16+
const MainDb = DbService.Tag("MainDb")
17+
const ReplicaDb = DbService.Tag("ReplicaDb")
18+
19+
const program = Effect.gen(function*() {
20+
const mainDb = yield* MainDb
21+
const newDb = yield* ReplicaDb
22+
23+
const collectionName = "source"
24+
const mainCollection = Db.collection(mainDb, collectionName, Todo)
25+
const replicaCollection = Db.collection(newDb, collectionName, Todo)
26+
27+
const items = yield* Collection.find(mainCollection).pipe(FindCursor.toArray)
28+
29+
yield* Collection.insertMany(replicaCollection, items)
30+
})
31+
32+
/*** main.ts ***/
33+
34+
const MainDbLive = DbInstance.layer(MainDb, {
35+
database: { name: "mydb" },
36+
client: { url: "mongodb://localhost:27017" }
37+
})
38+
39+
const ReplicaDbLive = DbInstance.layer(ReplicaDb, {
40+
database: { name: "mydb" },
41+
client: { url: "mongodb://localhost:37017" }
42+
})
43+
44+
await program.pipe(
45+
Effect.provide(MainDbLive),
46+
Effect.provide(ReplicaDbLive),
47+
Effect.runPromise
48+
)

0 commit comments

Comments
 (0)