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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mal

v0.4.0

Published

MAL MongoDB Access Layer. A MongoDB wrapper using the native-node-driver

Downloads

30

Readme

MAL (Simple MongoDB Access Layer) v0.2

Convenience methods for access a connecting to, authenticating and querying against a MongoDB instance in Node.

MAL provides an easy way to preform operations on a MongoDB instance. mongodb / node-mongodb-native -- sits on mongodb / node-mongodb-native function calls

As a general rule function calls take the format of ('collectionname', ...>>> same rules for params as node-mongodb-native);
*exceptions being the two streams calls where the last parameters are always the writebale stream for streamPipe 
	or readable stream for streamEvents.
** no callback if none is desired but set {safe:false} etc.. same as node-mongodb-native.

To create and instance of MAL

  1. Create a dbSettings object

//example dbSettings Object.

var dbsettings = {
	host: 'host ip or name',
	port: port number,
	db: 'database name',
	options: {auto_reconnect: true},
	username: '...',
	password: '...'
};

//username and password are required only if there is authentication, 
//if there is no authentication required remove the properties from the object.

var dbsettings = {
	host: 'host ip or name',
	port: port number,
	db: 'database name',
	options: {auto_reconnect: true}
};
  1. var dbManager = new MAL(dbsettings, optionalCallback);

optionalCallback is a function that is takes an connected mongoDB server as a parameter.

No CallBack required.
----------------------

var dbManager = new MAL(dbsettings);

Callback required
------------------

//example

function optionalCallback(db){
  var callback = function(err, collection){
	collection.find({status:1}, {id:1..}).toArray(function(error, result) {
		if (error) {
		  console.log(err); 
		}
		else {
			//do something with result
		}
	});
  }

  db.collection('matches', function(error, collection) {
	if (error) callback(error);
	else callback(null, collection);
  });
}

var dbManager = new MAL(dbsettings, optionalCallback);

For another example see in test0.js in tests.

`Obvious 'thing' is optionalCallBack doesn't use the MAL class but rather requires the user to get the collection ref 
and then preform the method call using node-mongodb-native directly for both.`
  1. Function calls

Function calls to the MAL instance follow the same pattern as node-mongodb-native the exception being 
that all the first paramater is the name of the collection being queried.

The first parameter is always the collection name, followed by parameters expected by node-mongodb-native

example:

//Assume
var dbManager = new MAL(dbsettings);
dbManager.find ('col1', {name : 'name'}, {_id:0}, function(err,result){...}); 

//List of calls available in v0.2
.find(collectionName, query, fields, options, callback)
.findOne(collectionName, query, callback) 	
.insert(collection_Name, query, options, callback) 
.save = function(collectionName,obj, callback)
.update = function(collectionName, criteria, update, options, callback) 
.remove = function(collectionName, criteria, callback)
.findAndModify = function(collection_Name, criteria, sort, update, options, callback)
// for Stream methods last parameters must be the writable or readable streams.
.streamPipe = function(collectionName, query, fields, options, wrStream)
.streamEvents = function(collectionName, query, fields, options, xStream)
  1. Streaming Functionality.

MAL provides two streaming methods. (see examples).

//Assume
var dbManager = new MAL(dbsettings);

 a) streamPipe. (see stream in examples)
 	//function(collectionName, query, fields, options, wrStream) {...
	// the first argument is always a collectionName
	// the last
 	dbManager.streamPipe('col1',stream);
	//this calll will return everything from 'col1' 
	//and pipe the results to wrStream where wrStream is a writable stream like response or a tcp socket.

 b) streamEvents.( see sse in examples);
 	//lets say we are streaming to serversent events.
	//create a readable stream
	var dbStream = new stream.Stream();
	dbStream.readable = true;
	//pass it into the streamEvents function
	dbManager.streamEvents('Col1',dbStream);
	res.writeHead(200, {
	  'Content-Type': 'text/event-stream',
	  'Cache-Control': 'no-cache',
	  'Connection': 'keep-alive'
	});
	//flush headers
	res.write('');
	//and listen on events
	dbStream.on('data', function(data) {
	  res.write('data: ' + JSON.stringify(data) + '\r\n\r\n');
	});
	dbStream.on('end', function() {
	  console.log('ended');
	});

see 'server sent events' example on cloudfoundry nodejs using a mongolab mongoDB instance streaming 4.8mb of tweets to the browser with Server Sent Events.

To populated data, pulled tweets from a twitter account that has volume tweets and stuck them in a mongoDB at mongolab.

  1. ToDo.

1. Write more tests
2. Write examples
3. Add mapReduce and other calls
4. Take it from there.