All files / src/utils exporter.js

92.31% Statements 36/39
66.67% Branches 4/6
83.33% Functions 5/6
92.31% Lines 36/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 601x 1x 1x 1x 1x   1x   7x 7x 7x 7x 7x       7x 7x 7x 7x 7x 7x 7x     7x 7x   7x 7x   7x 20003x 20003x 40011x   20003x 20003x   27x         27x   27x 20x 20x     7x 7x           1x  
const Promise = require('bluebird');
const fs = require('fs');
const path = require('path');
const jsonStream = require('JSONStream');
const debug = require('debug')('express-cassandra');
 
const exporter = {
  processTableExport(systemClient, fixtureDirectory, keyspace, table) {
    debug('==================================================');
    debug(`Reading table: ${table}`);
    return new Promise((resolve, reject) => {
      const jsonfile = fs.createWriteStream(path.join(fixtureDirectory, `${table}.json`));
      jsonfile.on('error', (err) => {
        reject(err);
      });
 
      let processed = 0;
      const startTime = Date.now();
      jsonfile.on('finish', () => {
        const timeTaken = (Date.now() - startTime) / 1000;
        const throughput = timeTaken ? processed / timeTaken : 0.00;
        debug(`Done with table, throughput: ${throughput.toFixed(1)} rows/s`);
        resolve();
      });
 
      const writeStream = jsonStream.stringify('[', ',', ']');
      writeStream.pipe(jsonfile);
 
      const query = `SELECT * FROM "${keyspace}"."${table}"`;
      const options = { prepare: true, fetchSize: 1000 };
 
      systemClient.eachRow(query, [], options, (n, row) => {
        const rowObject = {};
        row.forEach((value, key) => {
          rowObject[key] = value;
        });
        processed++;
        writeStream.write(rowObject);
      }, (err, result) => {
        Iif (err) {
          reject(err);
          return;
        }
 
        debug(`Streaming ${processed} rows to: ${table}.json`);
 
        if (result.nextPage) {
          result.nextPage();
          return;
        }
 
        debug(`Finalizing writes into: ${table}.json`);
        writeStream.end();
      });
    });
  },
};
 
module.exports = exporter;