Giter Site home page Giter Site logo

iddogino / mongotriggers Goto Github PK

View Code? Open in Web Editor NEW
22.0 3.0 6.0 20 KB

Adds the ability to add triggers and listeners to mongoDB operations, when using the mongoJS driver

JavaScript 100.00%
trigger javascript mongodb database-operation database

mongotriggers's Introduction

MongoDB Triggers

This light weight library was written to mimic the triggers feature found in many SQL server, and much needed in mongoDB.

There are 2 types of triggers:

  • Triggers - these will function as middleware, and thus will be called before the database operation is executed. Within these triggers, you may modify the query (or inserted document), or check the data, and abort the operation if it is incorrect. At the end of the trigger function, you must call next() to proceed. If you pass a parameter to the next() function, it'll abort execution, and pass it as an argument to the callback function. A possible use for triggers may be to check whether an email exists in the database before inserting a user with that email, or to add a joinedOn date when a new user is saved.

  • Listeners - these will be called following the execution of the database operation. A use for listeners may be to update additional pieces of data following the operation, or notify someone about the operation.

Triggers

//Add a trigger to db.user.save()
triggers(db.users).save(function(document, next) {
    //...
    next();
});
    
//Add a trigger to db.user.insert()
triggers(db.users).insert(function(document, next) {
    //...
    next();
});
    
//Add a trigger to db.user.update()
triggers(db.users).update(function(query, <update>, <options>, next) {
    //...
    next();
});
    
//Add a trigger to db.user.remove()
triggers(db.users).remove(function(query, next) {
    //...
    next();
});

Triggers can abort execution like that:

triggers(db.users).save(function(document, next) {
    //Check if a user with the same email exists in data base:
    db.users.findOne({email:document.email}, function(err,doc) {
        if(doc)
            next(new Error());
        else
            next();
    });
});

Filters can also modify the document:

triggers(db.users).save(function(document, next) {
    //Add a 'created' date to the new user
    document.created = new Date();
    next();
});

Listeners

Listeners are created using the 'on' function:

triggers(db.members).on('save', function (error, result, query, update, options) {
  // error   : null (unless something went wrong)
  // result  : { ... } (in case of the save command, this will be a lastErrorObject)
  // query   : { _id: "foo" }
  // update  : { name: "Anders" }
  // options : undefined (since no options object was passed to the update function)
});

You can listen to:

  • save
  • insert
  • update
  • remove
  • find
  • findOne

Chaining:

Operation may also be chained like that:

triggers(db.members)
    .update(function(query, <update>, <options>, next) {})
    .save(function(document, next) {})
    .on('remove', function(error, result, document) {});

mongotriggers's People

Contributors

amovah avatar iddogino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongotriggers's Issues

Is this reliable for production?

Thanks for contributing your work to the community.
I was developing an application which requires event-triggers on a Mongo Database.

I had few queries:

  • Do you recommend this library for production? You might be well aware of its performance and reliability.
  • The page on NPM mentions this is MIT Licensed, but you haven't mentioned the license on this repo.

mongodb

Is there a way with use mongodb nodejs native driver ?
when i use triggers(db.collection("test")).on('insert'.....
its not triggered. But "mongojs" works well.

Error when It created a trigger

In the test.js exists error to run in node.js:
triggers(db.users).save(function (document, next) {
//Add a 'created' date to the new user
document.created = new Date();
next();
}
);
produce:
TypeError: next is not a function
at Collection. (/home/user/dev/mongoTriggers-master/test.js:8:9)
at next (/home/user/dev/mongoTriggers-master/index.js:97:25)
at Collection.save (/home/user/dev/mongoTriggers-master/index.js:99:13)
at Collection.save (/home/user/dev/mongoTriggers-master/node_modules/mongojs/lib/collection.js:112:47)
at next (/home/user/dev/mongoTriggers-master/index.js:96:31)
at Collection. (/home/user/dev/mongoTriggers-master/test.js:8:9)
at next (/home/user/dev/mongoTriggers-master/index.js:97:25)
at Collection.save (/home/user/dev/mongoTriggers-master/index.js:99:13)
at Object. (/home/user/dev/mongoTriggers-master/test.js:12:10)
at Module._compile (module.js:570:32)

TypeError: Cannot read property 'save' of undefined

Hi,

When trying to use this:

triggers(db.users).update(function() {
    console.log("Database Updated");
});

I keep getting this error:

D:\Projects\Easybike\node_modules\mongo-triggers\index.js:101
    collection.save = injectTriggers(collection.save, triggers.save, listeners
                                               ^
TypeError: Cannot read property 'save' of undefined
    at module.exports (D:\Projects\Easybike\node_modules\mongo-triggers\index.js:101:48)
    at Object.<anonymous> (D:\Projects\Easybike\server.js:37:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Am I doing something wrong/what could be the problem?

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.