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

am2

v1.0.4

Published

am2 is simplified mongodb library

Downloads

2

Readme

AM2 - Abstracted MongoDB Module

AM2 is light weight, promise based abstracted Mongodb module.

Methods

am2.connect(string url, string db, object options)

connect to MongoDB.

options:

  • bool reconnect: reconnect automatically if disconnected from MongoDB
  • number connectTimeout: connection timeout. default is 1000(ms).
am2.connect('localhost', 'test')
.then( () => {
	console.log('Connected!');
}).catch( (err) => {
	console.error(err);
});

am2.disconnect(bool force)

disconnect from MongoDB.

am2.connect('localhost', 'test')
.then( () => {
	console.log('Connected!');
	
	am2.disconnect().then( () => {
		console.log('disconnected!');
	}).catch( (err) => {
		console.err(err);
		
		// if failed to disconnect, do forcely.
		am2.disconnect(true).then( () => {
			console.log('Forcely disconnected.');
		}).catch( (err) => {
			console.error(err);
		});
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.insert(string collection, object doc)

am2.insert(string collection, array doc)

insert document or documents into collection. if doc is object, insert single object. else, is array, insert muliple objects.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.insert('user', {
		name: '.modernator',
		age: 25,
		sex: 'male'
	}).then( () => {
		console.log('doc inserted');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.update(string collection, object filter, object update, object options)

update document or documents of collection. basically update single object but you can update multiple at same time with options.many = true.

options:

  • bool many: update multiple docs matched by filter. default is false.
  • bool upsert: create new document if there is no matched. default is false.
am2.connect('localhost', 'test')
.then( () => {
	
	am2.update('user', {
		sex: 'male'
	}, {
		$set: {
			sex: 'man'
		},
		$currentDate: {
			lastModified: true
		}
	}, {
		many: true
	}).then( () => {
		console.log('docs updated');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.replace(string collection, object filter, object doc, object options)

replace document that matched by filter.

options:

  • bool upsert: create new document if there is no matched. default is false.
am2.connect('localhost', 'test')
.then( () => {
	
	am2.replace('user', {
		name: '.modernator'
	}, {
		age: 25,
		url: 'http://modernator.me'
	}).then( () => {
		console.log('doc replaced');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.delete(string collection, object filter, object options)

delete document or documents.

options:

  • bool many: delete all documents that matched by filter. default is false.
am2.connect('localhost', 'test')
.then( () => {
	
	am2.delete('user', {
		sex: 'male'
	}, {
		many: true
	}).then( () => {
		console.log('doc deleted');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.drop(string collection)

drop collection.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.drop('user').then( () => {
		console.log('collection deleted');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.find(string collection, object query, object options)

find documents.

options:

  • number skip: number of skipping documents from result. default is undefined.
  • number limit: number of limited documents from result. default is undefined.
  • array paginate: combine skip and limit as single argument with array. first element is skip and second is limit. if skip and limit set with paginate, they will ignored. default is undefined.
  • array excludes: list of field that want to exclude from result. default is undefined.
am2.connect('localhost', 'test')
.then( () => {
	
	am2.find('user', {}, {
		excludes: ['name']	// fields for exclude from result
	}).then( (docs) => {
		console.log(docs);
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch((err) => {
	console.error(err);
});

am2.count(string collection, object query, object options)

count matched documents in collection.

options:

  • number skip
  • number limit
am2.connect('localhost', 'test')
.then( () => {
	
	am2.count('user').then( (result) => {
		console.log(result);
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch((err) => {
	console.error(err);
});

am2.createIndex(string collection, object index, object options)

create index.

options: see here.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.createIndex('user', {
		name: 1
	}, {
		unique: true,
		sparse: true
	}).then( () => {
		console.log('index created');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.dropIndex(string collection, object index)

drop index.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.dropIndex('user', {
		name: 1
	}).then( () => {
		console.log('index deleted');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.dropIndexes(string collection)

delete all indexes.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.dropIndexes('user').then( () => {
		console.log('all indexes deleted');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.reindex(string collection)

reindex collection.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.reIndex('user').then( () => {
		console.log('reindexed');
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch( (err) => {
	console.error(err);
});

am2.group(string collection, array keys, object options)

group collection. see here.

options:

  • object init
  • function reduce
  • function finalize
  • bool command
am2.connect('localhost', 'test')
.then( () => {
	
	am2.group('user', [], {
		init: { count: 0 },
		reduce: function (obj, prev) { prev.count++; }
	}).then( (docs) => {
		console.log(docs);
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch((err) => {
	console.error(err);
});

am2.distinct(string collection, string key, object query)

distinct collection.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.distinct('user', 'name', {
		sex: 'male'
	}).then( (docs) => {
		console.log(docs);
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch((err) => {
	console.error(err);
});

am2.aggregate(string collection, object pipeline, object options)

see this.

am2.connect('localhost', 'test')
.then( () => {
	
	am2.aggregate('user', [
		{
			$group: {
				_id: "$name"
			}
		},
		{
			$project: {
				_id: 0,
				path: "$_id"
			}
		},
		{
			$sort: {
				path: 1
			}
		},
		{
			$limit: 3
		}
	]).then( (docs) => {
		console.log(docs);
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch((err) => {
	console.error(err);
});

am2.mapReduce(string collection, function map, function reduce, object options)

see this.

am2.connect('localhost', 'test')
.then( () => {
	
	function map() {
		emit(this.sex, 1);
	}
	
	function reduce(key, values) {
		return Array.sum(values);
	}
	
	am2.mapReduce('user', map, reduce , {
		out: 'user_mu'
	}).then( (docs) => {
		console.log(docs);
	}).catch( (err) => {
		console.error(err);
	}).then( () => {
		am2.disconnect();
	});
	
}).catch((err) => {
	console.error(err);
});

you can access "ObjectID" with "am2.ObjectID".

AM2 is MIT licensed. any question: mail to me [email protected].