Home

Awesome

NoSQL Stream Build Status npm downloads license

Add the streamable ability to the abstract-nosql database.

Usage

Once the Database implements the AbstractIterator:

the db should be the streamable.

But, you should install the nosql-stream package first.

npm install nosql-stream

you should install the nosql-stream package first.

npm install nosql-stream

var addStreamFeatureTo = require('nosql-stream')
var LevelDB = addStreamFeatureTo(require('nosql-leveldb'))

The readStream/createReadStream, keyStream/createKeyStream, valueStream/createValue and writeStream/createWriteStream methods will be added to the database.

AbstractNoSql.keyStream(createKeyStream)

create a readable stream.

the data item is key.

AbstractNoSql.valueStream(createValueStream)

create a readable stream.

the data item is value.

AbstractNoSql.readStream(createReadStream)

create a readable stream.

the data item is an object: {key:key, value:value}.

arguments

return

Events

the standard 'data', 'error', 'end' and 'close' events are emitted. the 'last' event will be emitted when the last data arrived, the argument is the last raw key. if no more data the last key is undefined.

var MemDB = require("nosql-memdb")


var db1 = MemDB("db1")
var db2 = MemDB("db2")

var ws = db1.writeStream()
var ws2 = db2.createWriteStream()

ws.on('error', function (err) {
  console.log('Oh my!', err)
})
ws.on('finish', function () {
  console.log('Write Stream finish')
  //read all data through the ReadStream
  db1.readStream().on('data', function (data) {
    console.log(data.key, '=', data.value)
  })
  .on('error', function (err) {
    console.log('Oh my!', err)
  })
  .on('close', function () {
    console.log('Stream closed')
  })
  .on('end', function () {
    console.log('Stream closed')
  })
  .pipe(ws2) //copy Database db1 to db2:
})

ws.write({ key: 'name', value: 'Yuri Irsenovich Kim' })
ws.write({ key: 'dob', value: '16 February 1941' })
ws.write({ key: 'spouse', value: 'Kim Young-sook' })
ws.write({ key: 'occupation', value: 'Clown' })
ws.end()

filter usage:

db.createReadStream({filter: function(key, value){
    if (/^hit/.test(key))
        return db.FILTER_INCLUDED
    else key == 'endStream'
        return db.FILTER_STOPPED
    else
        return db.FILTER_EXCLUDED
}})
  .on('data', function (data) {
    console.log(data.key, '=', data.value)
  })
  .on('error', function (err) {
    console.log('Oh my!', err)
  })
  .on('close', function () {
    console.log('Stream closed')
  })
  .on('end', function () {
    console.log('Stream closed')
  })

next and last usage for paged data demo:


var callbackStream = require('callback-stream')

var lastKey = null;

function nextPage(db, aLastKey, aPageSize, cb) {
  var stream = db.readStream({next: aLastKey, limit: aPageSize})
  stream.on('last', function(aLastKey){
    lastKey = aLastKey;
  });

  stream.pipe(callbackStream(function(err, data){
    cb(data, lastKey)
  }))

}

var pageNo = 1;
dataCallback = function(data, lastKey) {
    console.log("page:", pageNo);
    console.log(data);
    ++pageNo;
    if (lastKey) {
      nextPage(db, lastKey, 10, dataCallback);
    }
    else
      console.log("no more data");
}
nextPage(db, lastKey, 10, dataCallback);

ReadStream

ReadStream is used to search and read the abstract-nosql database.

You must implement the db.iterator(options), iterator.next() and iterator.end() to use. (See AbstractIterator)

The resulting stream is a Node.js-style Readable Stream where 'data' events emit objects with 'key' and 'value' pairs.

You can also use the gt, lt and limit options to control the range of keys that are streamed. And you can use the filter function to filter the resulting stream.

ReadStream(db, [options[, makeData]])

arguments


var NoSQLStream=require('nosql-stream')
var FILTER_EXCLUDED = require('nosql-stream/lib/consts').FILTER_EXCLUDED
var ReadStream = NoSQLStream.ReadStream


function filter(key,value) {
  if (key % 2 === 0) return FILTER_EXCLUDED
}
var readStream = ReadStream(db, {filter:filter})
//or:
var readStream = new ReadStream(db, {filter:filter})

  readStream.on('data', function (data) {
    console.log(data.key, '=', data.value)
  })
  .on('error', function (err) {
    console.log('Oh my!', err)
  })
  .on('close', function () {
    console.log('Stream closed')
  })
  .on('end', function () {
    console.log('Stream closed')
  })


WriteStream

WriteStream is used to write data to the abstract-nosql database.

The WriteStream is a Node.js-style Writable Stream which accepts objects with 'key' and 'value' pairs on its write() method.

The WriteStream will buffer writes and submit them as a batch() operations where writes occur within the same tick.

Usage

WriteStream(db, [options])

arguments


var NoSQLStream=require('nosql-stream')
var WriteStream = NoSQLStream.WriteStream

var ws = WriteStream(db)
//or:
var ws = new WriteStream(db)


ws.on('error', function (err) {
  console.log('Oh my!', err)
})
ws.on('finish', function () {
  console.log('Write Stream finish')
})

ws.write({ key: 'name', value: 'Yuri Irsenovich Kim' })
ws.write({ key: 'dob', value: '16 February 1941' })
ws.write({ key: 'spouse', value: 'Kim Young-sook' })
ws.write({ key: 'occupation', value: 'Clown' })
ws.end()

AbstractIterator

You must implement the AbstractIterator if you wanna the database supports the ReadStreamable ability.

AbstractIterator

The following methods need to be implemented:

Sync methods:

AbstractIterator#_nextSync()

Get the next element of this iterator.

return

AbstractIterator#_endSync()

end the iterator.

Async methods:

these async methods are optional to be implemented.

AbstractIterator#_next(callback)

AbstractIterator#_end(callback)