loopback: logging queries executed by datasource

非 Y 不嫁゛ 提交于 2019-12-12 10:18:24

问题


Is there a remote method in Loopback data-juggler or any other loopback component which will let me log the query that is executed by the datasource.

Eg: If i'm using MySQL connector, then when MODEL_NAME.findById() is called, i should be able to get

SELECT * from DATABASE_NAME.MODEL_TABLE where id = WHATEVER_ID

Similarly for MongoDB, it should return equivalent mongo query It'd be great if i am able to log query.explain() of mongo here itself

I've tried running my app as DEBUG=loopback:connector:* node . as suggested here https://groups.google.com/forum/#!topic/loopbackjs/rpii8R8iUkw

It helps, but i'm not able to understand if the query used mongo indexes or not.

Is there a better alternative where i can get response from the datasource and trim it to my requirements? (like just showing if index was used or not)


回答1:


about query execute command, maybe you can see this document Connector hooks

in my application, I use after execute to log insert and delete method

return db.observe('after execute', function(ctx, next) {
  let sql = ctx.req.sql;
  let isInsert = _.startsWith(sql, 'INSERT INTO');
  let isDelete = _.startsWith(sql, 'DELETE FROM');

  // logic code

  return next();
});


来源:https://stackoverflow.com/questions/42088404/loopback-logging-queries-executed-by-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!