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 mongodbnpm 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.
connecturi: string, options?: MongoClientOptions } TaskEither<MongoError, MongoClient>
{ useNewUrlParser: true, useUnifiedTopology: true }
is used a default for MongoClientOptions
.
Example:
const client =
getDb
getDb
is a curried function to retrieve a Db from a MongoClient
.
getDbdb: string Db
Example:
const todosDb =
findOne
findOne
retrives one document matching the query from the collection or null, wraping MongoDB's findOne.
findOnecollection: string, query: FilterQuery<T>, options?: FindOneOptions ReaderTaskEither<Db, MongoError, T | null>
Example:
const todo1 =
findMany
findMany
retrieves an array of documents matching the query from the collection, wrapping MongoDB's find.
findManycollection: string, query: FilterQuery<T>, options?: FindOneOptions ReaderTaskEither<Db, MongoError, T>
Example:
const openTodos =
insertOne
insertOne
adds the provided document to the collection and returns it including the _id
, wrapping MongoDB's insertOne.
insertOnecollection: string, document: T, options?: CollectionInsertOneOptions ReaderTaskEither<Db, MongoError, WithId<T>>
Example:
const newTodo =
insertMany
insertMany
adds all of the provided documents to the collection and returns them as an array including the _id
s, wrapping MongoDB's insertMany.
insertManycollection: string, documents: T, options?: CollectionInsertManyOptions ReaderTaskEither<Db, MongoError, T>
Example:
const newTodos =
updateOne
updateOne
updates one document matching the query in the collection with the provided changes and returns the updated document, wrapping MongoDB's updateOne.
updateOnecollection: string, query: FilterQuery<T>, update: Update<T>, options?: UpdateOneOptions ReaderTaskEither<Db, MongoError, T | null>
Example:
const updatedTodo =
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.
updateManycollection: string, query: FilterQuery<T>, update: Update<T>, options?: UpdateManyOptions ReaderTaskEither<Db, MongoError, T>
Example:
const updatedTodos =
deleteOne
deleteOne
removes one document matching the query from the collection returning the deleted document, wrapping MongoDB's deleteOne.
deleteOnecollection: string, query: FilterQuery<T>, options?: DeleteOneOptions ReaderTaskEither<Db, MongoError, T | null>
Example:
const removedTodo =
deleteMany
deleteOne
removes all documents matching the query from the collection returning the deleted documents as an array, wrapping MongoDB's deleteMany.
deleteManycollection: string, query: FilterQuery<T>, options?: DeleteManyOptions ReaderTaskEither<Db, MongoError, T>
Example:, options?: FindOneOptions
const removedTodos =
This project was bootstrapped with TSDX.