简体   繁体   English

环回连接器挂钩

[英]Loopback connector hook

I want to log all insert sql statements of my model. 我想记录我模型的所有插入sql语句。 According to the loopback documentation the connector hook is ideal for this. 根据回送文档 ,连接器挂钩是理想的选择。

model.js 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 getDataSource不是函数

However if I do console.log(Model) I can see the function. 但是,如果我执行console.log(Model) ,则可以看到该功能。

Idea taken from here 这里取的想法

Model.getDataSource is not a valid role for the Model. Model.getDataSource不是模型的有效角色。
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) {
         // ...
     });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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