'use struct';

var config = null;

module.exports = (data, DBInstanceIdentifier) => {
  try {
    init(DBInstanceIdentifier);
  }
  catch(error) {
    return Promise.reject(error);
  };

  const es = require('elasticsearch');
  const client = new es.Client({
    host: config.host
  });

  client.bulk({
    body: body(data)
  }, (error, responce) => {
    if (error) return Promise(error);
  });
}

function init(type) {
  config = {
    host: process.env.ELASTICSEARCH_HOST,
    index: 'psql_query_log',
    type: type
  }

  if (typeof config.host === 'undefined') throw new Error('You have to export Elasticsearch host url to "ELASTICSEARCH_HOST"');
}

function body(data) {
  var store = [];
  const description = JSON.stringify({index: {_index: config.index, _type: config.type}});

  data.forEach((value, index, array) => {
    store.push(description);
    store.push(JSON.stringify(value));
  });

  return store;
}

@@ -26,16 +26,35 @@
  = null;
+%0Avar client = null;
 %0A%0Amodule
@@ -240,22 +240,16 @@
 ch');%0A  
-const 
 client =
@@ -300,106 +300,129 @@
 %0A%0A  
-client.bulk(%7B%0A    body: body(data)%0A  %7D, (error, responce) =%3E %7B%0A    if (error) return Promise(error
+return new Promise((resolve, reject) =%3E %7B%0A    const error = insert(data);%0A    if (error) reject(error);%0A    else resolve(
 );%0A 
@@ -949,12 +949,139 @@
 rn store;%0A%7D%0A
+%0Afunction insert(data) %7B%0A  client.bulk(%7B%0A    body: body(data)%0A  %7D, (error, responce) =%3E %7B%0A    if (error) return error;%0A  %7D);%0A%7D%0A

Change to return promise