简体   繁体   中英

Loopback connector hook

I want to log all insert sql statements of my model. According to the loopback documentation the connector hook is ideal for this.

model.js

var _ = require('underscore');
module.exports = function(Model) {
  //...
  var connector = Model.getDataSource().connector;
  connector.observe('after execute', function(ctx, next) {
    var sql = ctx.req.sql;
    var isInsert = _.startsWith(sql, 'INSERT INTO');
    next();
  });
}

I am getting

getDataSource is not a function

However if I do console.log(Model) I can see the function.

Idea taken from here

Model.getDataSource is not a valid role for the Model.
You can create a boot script to accomplish the desired:

module.exports = function (server) {
     var myConnector = server.datasources.MyDataSource.connector;
     return myConnector.observe ('after execute', function (ctx, next) {
         // ...
     });
};

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