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

mongorules

v0.1.4-alpha

Published

A small but fierce wrapper around the native mongodb driver - leveraging schemas - a syntactic mirror of the mongo shell interface.

Downloads

4

Readme

Mongorules (alpha)

Build Status

A small but fierce wrapper around the native mongodb driver - leveraging schemas - a syntactic mirror of the mongo shell interface.

Intro

Abiding by the the LOTR philosophy (one API to rule them all), mongorules adds a little extra sauce on top of the node-mongodb-native driver:

Schemas enforce consistency to operations:

  • insert()
  • update()
  • save()
  • findAndModify()

Transforms easily modify the payload.

Promises wrap all mongodb native methods and thus become yieldable.

Model methods can be attached to collection models.

Requirements

  • Node >= v6.0.0
  • Mongodb >= 2.6

Getting started

First, install mongorules and mongodb:

npm install --save mongorules mongodb

Second, init mongodb:


const mongorules = require('mongorules');
const mongodb = require('mongodb');
const url = process.env.MONGO_URL;

const connection = yield mongorules.connect('local', url, mongodb);
const db = mongorules.addDatabase('local', 'api-development', connection);

Third, add models:

const mongorules = require('mongorules');
const schema = require('./schemas/users.js');
const methods = require('./methods/users.js');

mongorules.addModels('local', 'api-development', {
  users: {
    schema,
    methods,
    onError: function(errors, info) {}
  }
});

Add default db (optional).

const mongorules = require('mongorules');
mongorules.setDefaultDb('local', 'api-development');

Now, write queries:

var result, users;
const {db} = require('mongorules'); // This works because we setDefaultDb().

result = yield db.users.insert({ name: 'jay' });
result = yield db.users.find({ name: 'jay' });
users = yield result.toArray();  

If we did not call setDefaultDb() we would retrieve the db instance via:

const mongorules = require('mongorules');
const db = mongorules.getDatabase('local', 'api-development');
// .addDatabase() returns a db instance as well.

// db.users.insert({...})

Supported operations

All mongodb native operations are supported and are wrapped in promises.

The following operations will enforce schema validation:

  • insert()
  • update()
  • save()
  • findAndModify()

Update operations

Mongorules supports validation for the following mongodb update operators:

  • $inc
  • $mul
  • $set
  • $min
  • $max
  • $addToSet
  • $push

Upsert operations are supported as well (validated as an insert).

Docs

Performance

See performance tests against the native mongodb driver and mongoose at the mongorules-performance-analysis repo.

License

MIT