2014-10-09 146 views
2

我有rethinkdb实例,我使用的是nodejs客户端。Javascript猴子补丁rethinkdb .run

rethinkdb 
    .db(p.rdb_database) 
    .table(p.table) 
    .orderBy({index: 'uidAndDate'}) 
    .filter({}) 
    .run(rethinkdbConnection, function (error, cursor) {...}) 

有没有办法给猴子打补丁.run函数? 我要监控的rethinkdb客户端这样的 - 添加before功能

rethinkdb 
    .db(p.rdb_database) 
    .table(p.table) 
    .orderBy({index: 'uidAndDate'}) 
    .filter({}) 
    .before(function(error, query, result, next){ 
    console.log('query: ',query); 
    console.log('result: ',result); 
    next(error); 
    }) 
    .run(rethinkdbConnection, function (error, cursor) {...}) 

回答

2

可以猴修补它,像这样的东西

TermBase = r.expr(1).constructor.__super__.constructor.__super__ 
TermBase.run_copy = TermBase.run; 
Termbase.run = function(callback) { 
    console.log("query", this.toString()); 
    this.run_copy(function(error, result) { 
     if (error) { 
      console.log("error", error) 
     } 
     else { 
      console.log("result", result) 
     } 
     callback(error, result) 
    }) 
}) 

但是,这是一种肮脏的。

+0

谢谢!我会试试看! – vodolaz095 2014-10-09 23:34:00