siUtil.js 958 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
'use strict'

module.exports = function (siOptions) {
  var siUtil = {}

  siUtil.countDocs = function (callback) {
    var count = 0
    const gte = 'DOCUMENT' + siOptions.keySeparator
    const lte = 'DOCUMENT' + siOptions.keySeparator + siOptions.keySeparator
    siOptions.indexes.createReadStream({gte: gte, lte: lte})
      .on('data', function (data) {
        count++
      })
      .on('error', function (err) {
        return callback(err, null)
      })
      .on('end', function () {
        return callback(null, count)
      })
  }

  siUtil.close = function (callback) {
    siOptions.indexes.close(function (err) {
      while (!siOptions.indexes.isClosed()) {
NGPixel's avatar
NGPixel committed
25
        // log not always working here- investigate
26 27 28 29 30 31 32 33 34 35 36
        if (siOptions.log) siOptions.log.info('closing...')
      }
      if (siOptions.indexes.isClosed()) {
        if (siOptions.log) siOptions.log.info('closed...')
        callback(err)
      }
    })
  }

  return siUtil
}