A functional approach to MongoDB
$ npm install mongo-func --save
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.
var mf = require('mongo-func');
Find one document from mongo based on the document query.
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.findOne(connectionString, collectionName, query)
var findOner = mf.findOne('mongodb://localhost:27017/mongo-func');
var findOneFromTest = findOner('test');
findOneFromTest({scooby: 'doo'})().then(function(doc) {
console.log(doc);
});
Find all documents from mongo based on the document query. Query can include options sort, limit and skip.
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.find(connectionString, collectionName, query)
var finder = mf.find('mongodb://localhost:27017/mongo-func');
var finderFromTest = finder('test');
finderFromTest({scooby: 'doo'})().then(function(docs) {
console.log(docs);
});
Count all documents from mongo matching the document query
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.count(connectionString, collectionName, query)
var counter = mf.count('mongodb://localhost:27017/mongo-func');
var counterFromTest = counter('test');
counterFromTest({scooby: 'doo'})().then(function(count) {
console.log(count);
});
Insert document into mongo
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.insert(connectionString, collectionName, document)
var inserter = mf.insert('mongodb://localhost:27017/mongo-func');
var insertToTest = inserter('test');
insertToTest({scooby: 'doo', scrappy: 'woo'})().then(function() {
console.log('inserted');
});
Update document in mongo based on query object with the update object
//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)
var updater = mf.update('mongodb://localhost:27017/mongo-func');
var updateToTest = updater('test');
updateToTest({scooby: 'doo'}, {$set: {scooby: 'too'}})().then(function() {
console.log('Updated');
});
Removes documents from mongo based on the query object
//input: String, String, Object OR Function (returns Object)
//output: Function ( accepts parameters of query function )
mf.remove(connectionString, collectionName, query)
var remover = mf.remove('mongodb://localhost:27017/mongo-func');
var removeFromTest = remover('test');
removeFromTest({scrappy: 'foo'})().then(function() {
console.log('removed');
});
Drop collection from mongo
//input: String, String
//output: Function
mf.dropCollection(connectionString, collectionName)
mf.dropCollection('mongodb://localhost:27017/mongo-func', 'test')()
.then(function() { console.log('dropped'); } );