npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongad

v0.2.2

Published

A monadic wrapper for MongoDB

Downloads

12

Readme

Mon(g)ad is a thin layer wrapping MongoDB CRUD operations in data types from fp-ts.

Installation

Add Mon(g)ad and its peerDependencies to your project via npm or yarn.

yarn add mongad mongodb
npm install mongad mongodb --save

API

Mon(g)ad provides two functions for each CRUD operation, one for a single document and one for an array of documents.

connect

connect establishes a connection to a MongoDB, wrapping MongoDB's connect method.

connect(uri: string, options?: MongoClientOptions }) => TaskEither<MongoError, MongoClient>

{ useNewUrlParser: true, useUnifiedTopology: true } is used a default for MongoClientOptions.

Example:

import { connect } from 'mongad'

const client = connect('mongodb://localhost')()

getDb

getDb is a curried function to retrieve a Db from a MongoClient.

getDb(db: string) => (client: MongoClient) => Db

Example:

import { map } from 'fp-ts/lib/TaskEither'
import { connect, getDb } from 'mongad'

const todosDb = map(getDb('todo_db'))(connect('mongodb://localhost'))()

findOne

findOne retrives one document matching the query from the collection or null, wraping MongoDB's findOne.

findOne<T>(collection: string, query: FilterQuery<T>, options?: FindOneOptions) => ReaderTaskEither<Db, MongoError, T | null>

Example:

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { findOne } from 'mongad'

const todo1 = run(findOne('todos', { _id: '1' }), db)

findMany

findMany retrieves an array of documents matching the query from the collection, wrapping MongoDB's find.

findMany<T>(collection: string, query: FilterQuery<T>, options?: FindOneOptions) => ReaderTaskEither<Db, MongoError, T[]>

Example:

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { findMany } from 'mongad'

const openTodos = run(findMany('todos', { done: false }), db)

insertOne

insertOne adds the provided document to the collection and returns it including the _id, wrapping MongoDB's insertOne.

insertOne<T>(collection: string, document: T, options?: CollectionInsertOneOptions) => ReaderTaskEither<Db, MongoError, WithId<T>>

Example:

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { insertOne } from 'mongad'

const newTodo = run(
  insertOne('todos', { description: 'Do something', done: false }),
  db
)

insertMany

insertMany adds all of the provided documents to the collection and returns them as an array including the _ids, wrapping MongoDB's insertMany.

insertMany<T>(collection: string, documents: T[], options?: CollectionInsertManyOptions) => ReaderTaskEither<Db, MongoError, T[]>

Example:

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { insertMany } from 'mongad'

const newTodos = run(
  insertMany('todos', [
    { description: 'Do something', done: false },
    { description: 'Something else ', done: false },
  ]),
  db
)

updateOne

updateOne updates one document matching the query in the collection with the provided changes and returns the updated document, wrapping MongoDB's updateOne.

updateOne<T>(collection: string, query: FilterQuery<T>, update: Update<T>, options?: UpdateOneOptions) => ReaderTaskEither<Db, MongoError, T | null>

Example:

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { updateOne } from 'mongad'

const updatedTodo = run(
  updateOne('todos', { _id: '1' }, { $set: { done: true } }),
  db
)

updateMany

updateMany updates all of the documents matching the query in the collection with the provided changes and returns the updated documents as array, wrapping MongoDB's updateMany.

updateMany<T>(collection: string, query: FilterQuery<T>, update: Update<T>, options?: UpdateManyOptions) => ReaderTaskEither<Db, MongoError, T[]>

Example:

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { updateMany } from 'mongad'

const updatedTodos = run(
  updateMany('todos', { done: false }, { $set: { done: true } }),
  db
)

deleteOne

deleteOne removes one document matching the query from the collection returning the deleted document, wrapping MongoDB's deleteOne.

deleteOne<T>(collection: string, query: FilterQuery<T>, options?: DeleteOneOptions) => ReaderTaskEither<Db, MongoError, T | null>

Example:

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { deleteOne } from 'mongad'

const removedTodo = run(deleteOne('todos', { _id: '1' }), db)

deleteMany

deleteOne removes all documents matching the query from the collection returning the deleted documents as an array, wrapping MongoDB's deleteMany.

deleteMany<T>(collection: string, query: FilterQuery<T>, options?: DeleteManyOptions) => ReaderTaskEither<Db, MongoError, T[]>

Example:, options?: FindOneOptions

import { run } from 'fp-ts/lib/ReaderTaskEither'
import { deleteMany } from 'mongad'

const removedTodos = run(deleteMany('todos', { done: true }), db)

This project was bootstrapped with TSDX.