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

mongolocal

v1.2.10

Published

Basic local implementation of MongoDB

Downloads

15

Readme

Mongo Local

Stripped-down version of MongoDB module that operates on a collection stored in memory. Doc matching and updating is done using the fauxmongo module.

Collections can be stored in an array, or an object with keys in the object corresponding to the doc's ID.

NOTE: This project is not affiliated with MongoDB

Changelog

Version 1.2.10

  • Fixed bug in cursor's hasNext function

Version 1.2.9

  • Added option to generate _id using external function

Version 1.2.8

  • Added hasNext function to cursor

Version 1.2.7

  • Fixed bug that caused cursor to skip when removing items from array-based collection

Version 1.2.6

  • Fixed bug in upsert that prevented callback from being triggered

Version 1.2.5

  • Fixed bug in remove function that triggered emitter before deletion was complete

Version 1.2.4

  • Fixed bug in capped insert function

Version 1.2.3

  • Made docsLinkedList attribute public, made docsLinkedList's list attribute public
  • Added tests to test sending arbitrary tags through options argument of insert/update/remove

Version 1.2.2

  • Fixed bug in cursor's forEach function

Version 1.2.1

  • Find now returns cursor, in addition to accepting callback

Previous versions

Docs on Github

Installation

Bower

bower install

Node

npm install

Usage

Initialize

Mongolocal sets up the collection

var mongolocal = require('mongolocal');
var collection = mongolocal();

Pass in an array

var mongolocal = require('mongolocal');
var extCollection = [];
var collection = mongolocal({
	collection: extCollection
});

Pass in an object

var mongolocal = require('mongolocal');
var extCollection = {};
var collection = mongolocal({
	collection: extCollection
});

Capped collection

var mongolocal = require('mongolocal');
var collection = mongolocal({
	max: 1000
});

Specify a function to generate _id

var maxId = 0;
function generateId() {
	maxId++;
	return maxId;
}

var mongolocal = require('mongolocal');
var collection = mongolocal({
	objectId: generateId
});

Find documents

var cursor = collection.find({key: "val"});
cursor.toArray(function(error, results) {
	if(error)
		console.error("Something went wrong");
	else {
		results.forEach(function(doc) {
			console.log(doc.key);
		});
	}
});

Insert document

var doc = {key: "val1"};
collection.insert(doc, function (error, writeOp) {
	if(error)
		console.error("Something went wrong");
	else
		console.log("Document inserted with ID", doc._id);
});

Update document

var doc = {key: "someval1"};
collection.insert(doc, function (error, writeOp) {
	if(error)
		console.error("Something went wrong on insertion");
	else {
		var id = doc._id;
		collection.update({_id: id}, {key: "val2"}, function(error, success) {
			if(error)
				console.error("Something went wrong on update");
			else
				console.log("Update succeeded");
		}
	}
});

Remove document

var doc = {key: "someval1"};
collection.insert(doc, function (error, writeOp) {
	if(error)
		console.error("Something went wrong on insertion");
	else {
		var id = doc._id;
		collection.remove(id, function(error, success) {
			if(error)
				console.error("Something went wrong on removal");
			else
				console.log("Removal succeeded");
		}
	}
});

Callbacks

Potentially useful when the collection is managed by some other software (eg: an array in Polymer)

Insert

var mongolocal = require('mongolocal');
var extCollection = []; // collection is a simple array
var collection = mongolocal({
	collection: extCollection,
	insert: function(doc) {
		console.log(doc._id);
		extCollection.push(doc);
	}
});

var doc = {key: "val1"}
collection.insert(doc, function(error, writeResult) {
	if(error)
		throw error;
	else
		console.log(doc._id + "inserted");
});

Update

var mongolocal = require('mongolocal');
var extCollection = []; // collection is a simple array
var collection = mongolocal({
	collection: extCollection,
	update: function(index, updated) {
		collection[index] = updated;
	}
});

var doc = {key: "val1"}
collection.insert(doc, function(error, writeResult) {
	if(error)
		throw error;

	var id = doc._id;

	collection.update({_id: id}, {key: "someval22"}, function(error, updated) {
		if(error)
			throw error;
		else
			console.log(id + " updated successfully");
	});
});

Remove

var mongolocal = require('mongolocal');
var extCollection = []; // collection is a simple array
var collection = mongolocal({
	collection: extCollection,
	remove: function(index) {
		collection.splice(index, 0);
	}
});

var doc = {key: "val1"}
collection.insert(doc, function(error, writeResult) {
	if(error)
		throw error;
	else {
		var id = doc._id;
		collection.remove({_id: id}, function(error, success) {
			if(error)
				throw error;
			else
				console.log(id + " removed");
		});
	}
});