Skip to content

Getting started

@effect-mongodb/services is the package that provides Effect services for effect-mongodb.

Install

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

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

Effect compatibility

Each version of @effect-mongodb/services supports only one major version of Effect:

Effect @effect-mongodb/services
v4 >= 0.8.0
v3 <= 0.7.x

Version 0.7.x receives bug fixes when necessary.

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.DateFromString })
// 1. Create your database service
const Database = DbService.Service("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.

Released under the MIT License.