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

minimongoose

v0.0.4

Published

Use your mongoose schemas and queries in the browser

Downloads

5

Readme

MiniMongoose

  • Reuse your mongoose schemas in the browser
  • Run mongoose queries in the browser (limit, skip, populate, .exec etc) against your backend
  • Built in caching layer prevents duplication of both "in flight" and previous queries

After reading a lot about Relay, I wondered if it would be possile to do something similar with just mongoose. At first, I just commented out the monogdb driver in mongoose. The browserify build (debug: false), was in the multiple megabytes! MiniMongoose is my attemp to support mongoose schemas and queries in a much smaller library.

When minified, it's around 500kb. A huge amount of that is simply from require('mongoose').Schema. Re-implimenting just the Schema methods needed for this library would get it much closer to Relay's 200kb minified. It would also be much smaller if it did not depend on jquery for ajax.

Use

$ npm install minimongoose

/* pre compiled to es5 */
var MiniMongoose = require('minimongoose').MiniMongoose;

var Schema = MiniMongoose.Schema;

/* 
define your api end point that will receive payloads
*/

var MnM = new MiniMongoose({
    resourcePrefix: '/api/'
});

var car = new Schema({
    brand: {type: Schema.ObjectId, ref: 'Brand'}
});

// load the schemas
MnM.model('Brand', {});

MnM.model('Car', car);

MnM.models.Car
.find({name:'Mustang'})
.populate('brand')
.lean()
.exec(function(err, results){
    document.querySelector('.results')
    .innerHTML = JSON.stringify(results, null, 4);
});

One big difference from Mongoose:

The queries return Immutable Ordered maps (using _id as the map keys).

If you don't like that, it's easily corrected with toJS().

MnM.models.Car
.find()
.limit(3)
.populate('brand')
.exec(function(err, cars){
  console.log( cars.toJS() );
  /* 
  {
    "562d869b32c4e3e31af61599": {
        "_id": "562d869b32c4e3e31af61599",
        "updated_at": "2015-10-26T01:49:15.665Z",
        "brand": {
            "_id": "562d869b32c4e3e31af61595",
            "updated_at": "2015-10-26T01:49:15.658Z",
            "name": "Ford",
            "__v": 0
        },
        "name": "Mustang",
        "__v": 0
    }
  }
  */
});

The immutable implimentation actually speeds up the querying and populating of larger collections.

Example APP

Once you've cloned the git repo down: $ npm install && cd example && npm install && node server.js vist localhost:3000

Server API

You can define your own /api/ route, but here's what the example app uses.

It expects a payload of:

{
  modelName:'Car',
  op: 'find',
  match: '{name: \'Mustang\' }',
  fields: ...
  skip: ...
  limit: ...
  sort: ...
}

var express = require('express');
var router = express.Router();

function mongoMongooseMediator(req, res, cb){
  var params = req.body;
  var model = req.db[params.modelName];
  var query = model[params.operation](JSON.parse(params.match));

  if (params.fields){
    query = query.select(params.fields);
  }
  if (params.sort){
    query = query.sort(params.sort);
  }
  if (params.limit){
    query = query.limit(params.limit);
  }
  if (params.skip){
    query = query.skip(params.skip);
  }
  query.lean().exec(cb);
}

router.post('/', function(req, res) {
  var modelName = req.body.modelName;
  mongoMongooseMediator(req, res, function(err, results){
    res.send(JSON.stringify({
      modelName: modelName,
      results: results
    }));
  });
});

module.exports = router;

es6

/* if you want the es6 files, in order to do your own external aliasing for libraries like jquery etc */ 
import MiniMongoose from 'minimongoose/src/mini-mongoose';

Road map

  • get the minified size down closer to 200kb.
  • fill in other mongoose methods besides find, findOne, and count (such as findAndModify).

I've stubbed out where findOneAndModify and the other mongoose methods would go, but for now a lot of them are no-ops (see src/client-db/collection and src/model). I mainly focused on the find API and making the mquery api work nicely.