Skip to content

Latest commit

 

History

History
149 lines (136 loc) · 4.67 KB

README.md

File metadata and controls

149 lines (136 loc) · 4.67 KB

mongo-func

A functional approach to MongoDB

Build Status Coverage Status bitHound Score Dependencies npm version

Install

$ npm install mongo-func --save

What

Providing a functional library to common MongoDB functions:

All functions are curried by default therefore the functions aren't applied until the final parameter is provided. This allows you to create small, composable functions that are easy to test.

Each function also returns a promise with the result.

Usage

var mf = require('mongo-func');

findOne

Find one document from mongo based on the document query.

Parameters:
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.findOne(connectionString, collectionName, query)
Example:
var findOner = mf.findOne('mongodb://localhost:27017/mongo-func');
var findOneFromTest = findOner('test');

findOneFromTest({scooby: 'doo'})().then(function(doc) {
    console.log(doc);
});

find

Find all documents from mongo based on the document query. Query can include options sort, limit and skip.

Parameters:
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.find(connectionString, collectionName, query)
Example:
var finder = mf.find('mongodb://localhost:27017/mongo-func');
var finderFromTest = finder('test');

finderFromTest({scooby: 'doo'})().then(function(docs) {
    console.log(docs);
});

count

Count all documents from mongo matching the document query

Parameters:
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.count(connectionString, collectionName, query)
Example:
var counter = mf.count('mongodb://localhost:27017/mongo-func');
var counterFromTest = counter('test');

counterFromTest({scooby: 'doo'})().then(function(count) {
    console.log(count);
});

insert

Insert document into mongo

Parameters:
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.insert(connectionString, collectionName, document)
Example:
var inserter = mf.insert('mongodb://localhost:27017/mongo-func');
var insertToTest = inserter('test');

insertToTest({scooby: 'doo', scrappy: 'woo'})().then(function() {
    console.log('inserted');
});

update

Update document in mongo based on query object with the update object

Parameters:
//input: String, String, Object OR Function (returns Object), Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
//if update is a Function then another Function is returned ( accepts parameters of update function )
mf.update(connectionString, collectionName, query, update)
Example:
var updater = mf.update('mongodb://localhost:27017/mongo-func');
var updateToTest = updater('test');

updateToTest({scooby: 'doo'}, {$set: {scooby: 'too'}})().then(function() {
    console.log('Updated');
});

remove

Removes documents from mongo based on the query object

Parameters:
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.remove(connectionString, collectionName, query)
Example:
var remover = mf.remove('mongodb://localhost:27017/mongo-func');
var removeFromTest = remover('test');

removeFromTest({scrappy: 'foo'})().then(function() {
    console.log('removed');
});

dropCollection

Drop collection from mongo

Parameters:
//input: String, String
//output: Function
mf.dropCollection(connectionString, collectionName)
Example:
mf.dropCollection('mongodb://localhost:27017/mongo-func', 'test')()
    .then(function() { console.log('dropped'); } );