1

i want to know if there is way to create something like an After insert trigger or an asynchronous event that looks regularly through a log table and then apply the changes to another database.

i found something about the use of middlewares or the use of vanilla js functions but because of the different nommenclatures or perhaps the unclear documentation i don't think that's going to work for me. if anyone could clarify the use of post hooks on update or a promise in a function that would be great.

Basically what i want is a trigger or an event that listens to an order details table, i have 2 types of orders: current and delivered in the first type a field called deliveryDate is empty when it's updated and a value is entered in the field it should capture it and insert it in another Database.

i have a function called update that is executed everytime the route '/myApp' gets a POST request here is an example not far from what i want to do here is the route:

 module.exports = (app) => {
 const Something= require('../controllers/something.controller.js')
 // Create a new something
 app.post('/Myapp', Something.update);}

here is the controller:

const Something = require('../models/something.model.js');
const SomethingLog = require('../models/something.log.model.js');
exports.update = (req, res) => {//function body} 

can i create a post hook under the exports.update function like putting the hook in a function and if i do so how will i be able to execute it should i a just assign it to the default post route wouldn't that conflict with the existing route? thank you

Sid Barrack
  • 1,235
  • 7
  • 17

2 Answers2

1

According to this & this You can add a hook on after updation like this:

schema.post("update", function(doc) {
  console.log('Updated');
});
Zunnurain Badar
  • 920
  • 2
  • 7
  • 28
1

You can use mongoose middlewares for this

There are two types in middleware:

  1. Pre

before the operation is carried out

schema.pre('update', function(next) {

    // do something

    next();   //dont forget next();

});
  1. Post

after the operation is carried out

schema.post('update', function(next) {

    // do something

    next();   //dont forget next();

});

Note:

you will need to write this middleware in your collection file on which you want this operations to be carried out.

Second, "update" is the operation done on your collection, in case of "pre" middleware will execute before the update on collection and when middleware calls next() it proceeds with the update operation and in case of "post" middleware is executed after the update operation

niranjan_harpale
  • 2,048
  • 1
  • 17
  • 21
  • and what are 'update' and next representing here excuse if i sound a little bit repetitive but i want to get a thorough understanding of this – Sid Barrack Sep 17 '19 at 10:05
  • 'update' is the operation being performed on your collection. And if you define a middleware on the update operation, this middleware will get called. – niranjan_harpale Sep 17 '19 at 10:07
  • There are other operations supported to like "save", "remove", "deleteOne", "fineOneAndUpdate" etc, read docs. – niranjan_harpale Sep 17 '19 at 10:08
  • "next" is supposed to be called when things you want to do in middleware are over, and you want to pass the control to next operation. – niranjan_harpale Sep 17 '19 at 10:09
  • Like in pre middleware on "update", the update won't happen if next is not called in middleware. – niranjan_harpale Sep 17 '19 at 10:10
  • when you perform db.your_collection.update(....) operation, as we have middleware on the "update", this middleware will be called. – niranjan_harpale Sep 17 '19 at 10:12
  • but i have a function called update that is executed everytime the route `'/myApp'` gets a `POST` request here is an example not far from what i want to do here is the route: `module.exports = (app) => { const Something= require('../controllers/something.controller.js'); // Create a new something app.post('/Myapp', something.update);}` here is the controller: `const Something = require('../models/something.model.js'); const SomethingLog = require('../models/something.log.model.js'); exports.update = (req, res) => {//function body}` – Sid Barrack Sep 17 '19 at 10:17
  • can i create my hook under the `exports.update` function like putting the hook in a function and if i do so how will i be able to execute it should i a just assign it to the default post route? thank you – Sid Barrack Sep 17 '19 at 10:19
  • Okay i understand a little bit what you were explaining for example if i have this in my model file `const ConsumableSchema = mongoose.Schema({ reference: String, description: String, quantity: Number, approv : Number, price: Number, unit: String }); ` i should do something like `ConsumableSchema.pre('update' function(){})` in the same file if i understand this correctly – Sid Barrack Sep 17 '19 at 10:33