-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayers.ts
41 lines (31 loc) · 1.27 KB
/
layers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as DbService from "@effect-mongodb/services/DbService"
import * as MongoClientService from "@effect-mongodb/services/MongoClientService"
import * as Collection from "effect-mongodb/Collection"
import * as Db from "effect-mongodb/Db"
import * as FindCursor from "effect-mongodb/FindCursor"
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
import * as Layer from "effect/Layer"
const Todo = Schema.Struct({
userId: Schema.Number,
id: Schema.Number,
title: Schema.String,
completed: Schema.Boolean
})
const MyDb = DbService.Tag("MyDb")
const program = Effect.gen(function*() {
const db = yield* MyDb
const sourceCollection = Db.collection(db, "source", Todo)
const destinationCollection = Db.collection(db, "destination", Todo)
const items = yield* Collection.find(sourceCollection).pipe(FindCursor.toArray)
yield* Collection.insertMany(destinationCollection, items)
})
/*** main.ts ***/
const MyMongoClient = MongoClientService.Tag("MyMongoClient")
const MyDbLive = DbService.layer(MyDb, MyMongoClient, "mydb")
const MyMongoClientLive = MongoClientService.layer(MyMongoClient, "mongodb://localhost:27017")
const MainLive = Layer.provide(MyDbLive, MyMongoClientLive)
await program.pipe(
Effect.provide(MainLive),
Effect.runPromise
)