const Bigtable = require('@google-cloud/bigtable');

const TABLE_ID = 'my-table';
const INSTANCE_ID = 'my-bigtable-instance';

(async () => {
  try {
    // Creates a Bigtable client
    const bigtable = new Bigtable();

    // Connect to an existing instance:my-bigtable-instance
    const instance = bigtable.instance(INSTANCE_ID);

    // Connect to an existing table:my-table
    const table = instance.table(TABLE_ID);

    // Read a row from my-table using a row key
    let [singleRow] = await table.row('test-2018-08-08T07:15:37.590Z').get();
    console.log(
      `Row key: ${singleRow.id}\nData: ${singleRow.data.msgs.msg[0].value}`
    );

    // Read the entire table
    const filter = [
      {
        column: {
          cellLimit: 1, // Only retrieve the most recent version of the cell.
        },
      },
    ];
    const [allRows] = await table.getRows({filter});
    for (const row of allRows) {
      console.log(
        `Row key: ${row.id}\nData: ${row.data.msgs.msg[0].value}`
      );
    }

  } catch (err) {
    // Handle error performing the read operation
    console.error(`Error reading row r1:`, err);
  }
})()
