Code Monkey home page Code Monkey logo

mongojs-hooks's Introduction

mongojs-hooks

A lightweight module that wraps mongojs in pre() and post() hooks with support for multi-tenancy.

Build Status NPM version

Installation

$ npm install mongojs-hooks

Usage

To use hooks, you need to define the collection they will be applied to.

var mongo = require("mongojs-hooks");
var myCollection = mongo.collection("my.collection");

myCollection.pre("save", function (next, object, callback) {
	//Do whatever you want with your object here, validation, mutation, etc.

	//Now call next() to propogate to save()
	next(object, callback);
});

myCollection.post("save", function (next) {
	//Will execute after Mongo's save()

	next();
});

The hook will run for that collection anytime you call save().

var db = mongo("my.database");
db.collection("my.collection").save({ my: "object" });

In a multi-tenancy application, your collections will typically be uniquely named but you probably still want to bind the same hooks irrespesctive of the tenant. mongojs-hooks introduces a helper tenant() function to separate collections from their tenant:

//Will call pre()/post() hooks and save { my: "object" } to the collection client_name.my.collection
db.tenant("client_name").collection("my.collection").save({ my: "object" });

mongojs-hooks adds one final helper function to assist in sharing a database connection around your application in the form of db().

//Connects and stores the database connection
var db1 = mongo.db("db", "my.database");

//Can be used in a separate file to retrieve the connection above
var db2 = mongo.db("db");

//db1 === db2

Aside from the added functions above, mongojs-hooks is a transparent wrapper around mongojs so refer to its documentation for more information.

Tests

$ npm test

Contributing

All contributions are welcome! I'm happy to accept pull requests as long as they conform to the following guidelines:

  • Keep the API clean, we prefer ease-of-use over extra features
  • Don't break the build and add tests where necessary
  • Keep the coding style consistent, we follow JSHint's Coding Style

Otherwise, please open an issue if you have any suggestions or find a bug.

License

The MIT License (MIT) - Copyright (c) 2013 Clear Learning Systems

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.