Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

mongohooks

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongohooks

A thin before/after filter extension for the mongojs node-module

Source
npmnpm
Version
0.2.1
Version published
Weekly downloads
3
50%
Maintainers
1
Weekly downloads
 
Created
Source

MongoHooks

A thin before/after filter extension for the mongojs node-module.

build
status

Install

npm install mongohooks

Usage

var db = require('mongojs')('mydb', ['mycollection']);
var mongohooks = require('mongohooks');

// Add a before filter to db.mycollection.save
mongohooks(db.mycollection).onSave(function (document, next) {...});

// Add a before filter to db.mycollection.insert
mongohooks(db.mycollection).onInsert(function (document, next) {...});

// Add a before filter to db.mycollection.update
mongohooks(db.mycollection).onUpdate(function (query, <update>, next) {...});

// Add a before filter to db.mycollection.find
mongohooks(db.mycollection).onFind(function (criteria, <projection>, next) {...});

// Add an after filter to db.mycollection.find and db.mycollection.findOne
// The after filter is called once for each document in a returned result.
mongohooks(db.mycollection).onDocument(function (document, projection, next) {...});

// Filters are async: Call the `next` callback when you are done
mongohooks(db.mycollection).onFind(function (criteria, next) {
  // do stuff
  next();
});

// Before-filters can abort the execution by passing on an error
mongohooks(db.mycollection).onFind(function (criteria, next) {
  next(new Error());
});

// After-filters can also parse on errors
mongohooks(db.mycollection).onDocument(function (document, projection, next) {
  next(new Error());
});

// Use the `this` keyword to access the collection
mongohooks(db.mycollection).onUpdate(function (query, update, next) {
  // perform a find before each update
  this.findOne({ foo: 1 }, function (err, result) {
    // do stuff
    next();
  });
});

// Filters can be chained and multiple filters of the same type can be added
mongohooks(db.mycollection)
  .onUpdate(function (query, <update>, next) {...})
  .onFind(function (criteria, <projection>, next) {...})
  .onFind(function (criteria, <projection>, next) {...})
  .onDocument(function (document, projection, next) {...});

// Now just use the reqular mongojs API
db.mycollection.find({ ... }, function (err, res) {...});

Disclamer

This module was hacked together in a post-drunken state at a Copenhagen Node.js Hackathon event in just a few hours. Bugs might exist.

License

MIT

Keywords

mongo

FAQs

Package last updated on 24 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts