lib/Client.js
function delegate(client, fn) {
  // Add this function to the current client
  // and wrap with a connection check
  client[fn] = function(...args) {
    let self = this;
    let p = null;
    if (sand.profiler && sand.profiler.enabled) {
      // Build the profiler request
      let req = `riak ${fn} `;
      if (args[0].bucketType) {
        req += `types/${args[0].bucketType} `;
      }

      if (args[0].bucket) {
        req += `bucket/${args[0].bucket} `;
      }

      if (args[0].indexName) {
        req += `search/${args[0].indexName} `;
      }

      if (args[0].q) {
        req += `query/${args[0].q.replace(/(\w+):\w+/ig, '$1:*')}`
      }

      p = sand.profiler.profile(req.trim());
    }

    return new Promise(function(resolve, reject) {

      function returnResult(err, response, data) {
        p && p.stop();
        if (err) {
          err = new Error(err);
          err.data = data;
          err.req = `${fn}: ${args[0]}`;
          sand.riak.error(`${err.message} ${fn}:`, ...args);
          return reject(err);
        }

        resolve(response);
      }

      client.client[fn](...args, returnResult);
    }).catch(sand.error);
  };
}
function delegate(client, fn) {
  // Add this function to the current client
  // and wrap with a connection check
  client[fn] = function(...args) {
    let self = this;
    let p = null;
    if (sand.profiler && sand.profiler.enabled) {
      // Build the profiler request
      let req = `riak ${fn} `;
      if (args[0].bucketType) {
        req += `types/${args[0].bucketType} `;
      }

      if (args[0].bucket) {
        req += `bucket/${args[0].bucket} `;
      }

      if (args[0].indexName) {
        req += `search/${args[0].indexName} `;
      }

      if (args[0].q) {
        req += `query/${args[0].q.replace(/(\w+):\w+/ig, '$1:*')}`
      }

      p = sand.profiler.profile(req.trim());
    }

    return new Promise(function(resolve, reject) {

      function returnResult(err, response, data) {
        p && p.stop();
        if (err) {
          err = new Error(err);
          err.data = data;
          err.req = `${fn}: ${args[0]}`;
          sand.riak.error(`${err.message} ${fn}:`, ...args);
          return reject(err);
        }

        resolve(response);
      }

      client.client[fn](...args, returnResult);
    }).catch(sand.error);
  };
}
function delegate ( client , fn ) { // Add this function to the current client // and wrap with a connection check client [ fn ] = function ( ... args ) { let self = this ; let p = null ; if ( sand . profiler && sand . profiler . enabled ) { // Build the profiler request let req = ` ${ fn } ` ; if ( args [ 0 ] . bucketType ) { req += ` ${ args [ 0 ] . bucketType } ` ; } if ( args [ 0 ] . bucket ) { req += ` ${ args [ 0 ] . bucket } ` ; } if ( args [ 0 ] . indexName ) { req += ` ${ args [ 0 ] . indexName } ` ; } if ( args [ 0 ] . q ) { req += ` ${ args [ 0 ] . q . replace ( / (\w+):\w+ / ig , '$1:*' ) } ` } p = sand . profiler . profile ( req . trim ( ) ) ; } return new Promise ( function ( resolve , reject ) { function returnResult ( err , response , data ) { p && p . stop ( ) ; if ( err ) { err = new Error ( err ) ; err . data = data ; err . req = ` ${ fn } ${ args [ 0 ] } ` ; sand . riak . error ( ` ${ err . message } ${ fn } ` , ... args ) ; return reject ( err ) ; } resolve ( response ) ; } client . client [ fn ] ( ... args , returnResult ) ; } ) . catch ( sand . error ) ; } ; }
Delegate the function to the client, but
add a connection check first

@param {Client} client - the sand-riak client
@param {string} fn - function name
Delegate the function to the client but add a connection check first