A monadic wrapper for MongoDB
npm install mongad
Mon(g)ad is a thin layer wrapping MongoDB CRUD operations in data types from fp-ts.
Add Mon(g)ad and its peerDependencies to your project via npm or yarn.
``bash`
yarn add mongad mongodb
npm install mongad mongodb --save
Mon(g)ad provides two functions for each CRUD operation, one for a single document and one for an array of documents.
connect establishes a connection to a MongoDB, wrapping MongoDB's connect method.
`ts`
connect(uri: string, options?: MongoClientOptions }) => TaskEither
{ useNewUrlParser: true, useUnifiedTopology: true } is used a default for MongoClientOptions.
Example:
`js
import { connect } from 'mongad'
const client = connect('mongodb://localhost')()
`
getDb is a curried function to retrieve a Db from a MongoClient.
`ts`
getDb(db: string) => (client: MongoClient) => Db
Example:
`js
import { map } from 'fp-ts/lib/TaskEither'
import { connect, getDb } from 'mongad'
const todosDb = map(getDb('todo_db'))(connect('mongodb://localhost'))()
`
findOne retrives one document matching the query from the collection or null, wraping MongoDB's findOne.
`ts`
findOne
Example:
`js
import { run } from 'fp-ts/lib/ReaderTaskEither'
import { findOne } from 'mongad'
const todo1 = run(findOne('todos', { _id: '1' }), db)
`
findMany retrieves an array of documents matching the query from the collection, wrapping MongoDB's find.
`ts`
findMany
Example:
`js
import { run } from 'fp-ts/lib/ReaderTaskEither'
import { findMany } from 'mongad'
const openTodos = run(findMany('todos', { done: false }), db)
`
insertOne adds the provided document to the collection and returns it including the _id, wrapping MongoDB's insertOne.
`ts`
insertOne
Example:
`js
import { run } from 'fp-ts/lib/ReaderTaskEither'
import { insertOne } from 'mongad'
const newTodo = run(
insertOne('todos', { description: 'Do something', done: false }),
db
)
`
insertMany adds all of the provided documents to the collection and returns them as an array including the _ids, wrapping MongoDB's insertMany.
`ts`
insertMany
Example:
`js
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 updates one document matching the query in the collection with the provided changes and returns the updated document, wrapping MongoDB's updateOne.
`ts`
updateOne
Example:
`js
import { run } from 'fp-ts/lib/ReaderTaskEither'
import { updateOne } from 'mongad'
const updatedTodo = run(
updateOne('todos', { _id: '1' }, { $set: { done: true } }),
db
)
`
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.
`ts`
updateMany
Example:
`js
import { run } from 'fp-ts/lib/ReaderTaskEither'
import { updateMany } from 'mongad'
const updatedTodos = run(
updateMany('todos', { done: false }, { $set: { done: true } }),
db
)
`
deleteOne removes one document matching the query from the collection returning the deleted document, wrapping MongoDB's deleteOne.
`ts`
deleteOne
Example:
`js
import { run } from 'fp-ts/lib/ReaderTaskEither'
import { deleteOne } from 'mongad'
const removedTodo = run(deleteOne('todos', { _id: '1' }), db)
`
deleteOne removes all documents matching the query from the collection returning the deleted documents as an array, wrapping MongoDB's deleteMany.
`ts`
deleteMany
Example:, options?: FindOneOptions
`js
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.