Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 1.89 KB

File metadata and controls

55 lines (40 loc) · 1.89 KB

@effect-mongodb/services

NPM Version minzipped size dependency count tree shaking support

Effect services for effect-mongodb.

Install

pnpm install @effect-mongodb/services effect-mongodb effect mongodb

Note that effect-mongodb, effect, and mongodb are requested as peer dependencies.

Usage

Here is a simple example of how to use this package:

import { DbInstance, DbService } from "@effect-mongodb/services"
import { Effect, Schema } from "effect"
import { Collection, Db, FindCursor } from "effect-mongodb"

const Person = Schema.Struct({ name: Schema.String, age: Schema.Number, birthday: Schema.Date })

// 1. Create your database service tag
const Database = DbService.Tag("Database")

const program = Effect.gen(function* () {
  // 2. Use your database service
  const db = yield* Database
  const sourceCollection = Db.collection(db, "source", Person)
  const destinationCollection = Db.collection(db, "destination", Person)

  const items = yield* Collection.find(sourceCollection).pipe(FindCursor.toArray)
  yield* Collection.insertMany(destinationCollection, items)
})

// 3. Create a layer for your service, using DbInstance higher-level API
const DatabaseLive = DbInstance.layer(
  Database,
  { database: { name: "mydb" }, client: { url: "mongodb://localhost:27017" } }
)

await program.pipe(
  // 4. Provide the layer to your program
  Effect.provide(DatabaseLive),
  Effect.runPromise
)

Find more examples in the examples folder.