简体   繁体   中英

Loopback - Best practice to update database field for every api call

What is the best practice to update a database field for every api call.

I am using loopback & mongodb

1) I have implemented pre processing directive which is triggering for every call. But the problem is how can I access the db object there?

Middleware json :

{
  "initial:before": {
    "loopback#favicon": {}
  },
  "initial": {
    "./middleware/tracker": {},
  }
 ....
  ...
}

middleware/tracker.js :

module.exports = function(tracker) {
  return function tracker(req, res, next) {
    console.log('Request tracking middleware triggered on %s', req.url);
    // Here I want to update database
    next();
  };
};

You can access the models like this:

const app = require('../server/server');
app.models.SomeModel.sometMethod(params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM