Javascript monkey补丁rethinkdb .run

时间:2014-10-09 21:12:12

标签: javascript node.js rethinkdb

我有rethinkdb实例,我使用nodejs客户端。

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) {...})

1 个答案:

答案 0 :(得分: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)
    })
})

但那有点脏。

相关问题