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

mongosmash

v1.0.0

Published

A JS Harmony MongoDB/NeDB ODM

Downloads

15

Readme

MongoSmash

Mongo only pawn in game of life

Build Status Code Climate Dependency Status

MongoSmash is a super-minimal MongoDB/NeDB ODM for Node.js. It lets you treat JavaScript object as normal, and have changes persisted to MongoDB or NeDB. MongoSmash is implemented using Object.observe, and so is subject to its limitations on changes it can track, and requires Node 0.11.10+. You'll need to invoke node with the --harmony flag.

WARNING: MongoSmash is incomplete. It works, but it's missing a lot of features!

Example

var dbURI = 'mongodb://127.0.0.1:27017/test';
require('mongodb').MongoClient.connect(dbURI, function(err, db) {
  if (err) throw err;

  var smash = require('mongosmash')(db);

  var fido = {breed: 'golden retriever', age: 3, gender: 'm', name: 'Fido'};

  // smash.create(modelname, obj, cb) is shorthand for smash.new + smash.save
  smash.new('dog', fido); // adds observers. 'dog' is the mongodb collection name
  smash.save(fido, function(err, savedFido) { // savedFido has a _id
    if (err) throw err;
    
    savedFido.owner = 'Joe';
    smash.save(fido, function(err) { // no savedFido this time since it's an update
      if (err) throw err;

      smash.find({name: 'einstein'}, function(err, dogs){
        if (err) throw err;

        var einstein = dogs[0];
        // ... do something w/ einstein
      });
    });
  });
});

Benchmarks

You can run the benchmarks yourself as shown below. The results here are from my MacBookAir5,2. K-ops/sec means "thousand calls of the given function per second", so higher is better.

$ node -v
v0.11.11
$ npm run bench

> [email protected] bench /Users/benglish/mongosmash
> node --harmony bench/test

Failed to load c++ bson extension, using pure JS version
........................

OPERATION     | MONGOOSE KOps/s | MONGOSMASH KOps/s | DIFF %
------------------------------------------------------------
new           | 34.026          | 98.806            | 190.38
save          | 3.504           | 4.999             | 42.67
find          | 2.627           | 2.91              | 10.77
edit          | 55.441          | 174.096           | 214.02
saved edited  | 2.425           | 3.946             | 62.72
delete        | 5.915           | 6.99              | 18.17

Of course a head-to-head comparison with Mongoose isn't necessarily realistic, since MongoSmash doesn't do things like validations. I recommend using a tool like revalidator to do your object validations.

API

new MongoSmash(db)

You must pass in either an already-connected MongoDB connection (from nove-mongodb-native) or the NeDB module in its entirety. For the moment, only in-memory NeDB is supported.

#new(modelName, obj)

Sets up an object (obj) to have its changes tracked so we can save() it later. The modelName is equivalent to a MongoDB collection name. If you're doing this, you probably actually want create.

#save(obj, callback)

Does a MongoDB insert or update depending on the current status of the object. The error-first-style callback, which will call with a resultant object if it's a new object. The new object will also have a brand-new _id.

#create(modelName, obj, callback)

Runs new and then save immediately.

#find(query, callback)

Calls the callback error-first-style with an array of objects matching the MongoDB query. For the moment, projections are not supported.

#delete(obj, callback)

Deletes obj from the database, callback only takes in error.

Contributing

Please do! Pull requests, bug reports and feature requests are more than welcome, and should be done using Github PRs and Issues. Please try to conform to existing style (though I'm not very stylish), and don't forget tests and docs! Also, if making a performance improvement, feel free to update the benchmark data. TODOs are in Github Issues.

LICENSE

See LICENSE.txt