To write a trigger function in plv8 is much more convenient than in PL/pgSQL.

Mainly because objects OLD and NEW are real objects containing also column names. And therefore you do not need to care about actual table structure.

  1. To send them to other variable you simply create new object:
    var p_old_data=new Object(OLD);
  2. To check differences for UPDATE operation you simply iterate over their structure:
    for (var key in NEW) {
       if (NEW[key]!==OLD[key]) { ..... };
    }
    
  3. Everything else is similar to PL/pgSQL:
    • function “returns trigger”
    • based on operation you return NEW or OLD:
      switch(TG_OP) {
      case('INSERT'):
      return NEW;
      break;
      case('DELETE'):
      return OLD;
      break;
      case('UPDATE'):
      return NEW;
      break;
      default:
      return null;
      };