Giter Site home page Giter Site logo

rethinkdb-stream's Introduction

rethinkdb-stream Build Status js-standard-style

Convert a rethinkdb cursor into a readable stream (object mode stream)

Installation

npm i --save rethinkdb-stream

Usage

Changes Cursor Example:

var r = require('rethinkdb')
var conn = r.connect({/* opts */})
var rethinkdbStream = require('rethinkdb-stream')

r.table('foo').changes().run(conn)
  .then(function (cursor) {
    var stream = rethinkdbStream(cursor)
    stream.on('error', /* handle error */)
    stream.pipe(/* other stream */)
    /*
      // data events:
      { new_val: { id: 1 }, old_val: null }
      { new_val: { id: 2 }, old_val: null }
      { new_val: { id: 3 }, old_val: null }
    */
  })
  .then(function () {
    // insert document 1
    return r.table('foo').insert({ id: 1 }).run(conn)
  })
  .then(function () {
    // insert document 2
    return r.table('foo').insert({ id: 2 }).run(conn)
  })
  .then(function () {
    // insert document 3
    return r.table('foo').insert({ id: 3 }).run(conn)
  })

Query Cursor Example:

var r = require('rethinkdb')
var conn = r.connect({/* opts */})
var rethinkdbStream = require('rethinkdb-stream')

var rows = [
  { id: 1 },
  { id: 2 },
  { id: 3 }
]
r.table('foo').insert(rows).run(conn).then(function () {
  return r.table('foo').run(conn).then(function (cursor) {
    var stream = rethinkdbStream(cursor)
    stream.on('error', /* handle error */)
    stream.pipe(/* other stream */)
    /*
      // data events:
      { id: 1 }
      { id: 2 }
      { id: 3 }
    */
  })
})

Close Example:

rethinkdb streams have a close method which calls cursor.close()

var r = require('rethinkdb')
var conn = r.connect({/* opts */})
var rethinkdbStream = require('rethinkdb-stream')

r.table('foo').changes().run(conn)
  .then(function (cursor) {
    var stream = rethinkdbStream(cursor)
    stream.on('error', /* handle error */)
    stream.pipe(/* other stream */)
    return stream.close() // returns a promise, immediately closes the underlying cursor
    /*
      // data events:
      (none)
    */
  })
  .then(function () {
    // insert document 1
    return r.table('foo').insert({ id: 1 }).run(conn)
  })
  .then(function () {
    // insert document 2
    return r.table('foo').insert({ id: 2 }).run(conn)
  })
  .then(function () {
    // insert document 3
    return r.table('foo').insert({ id: 3 }).run(conn)
  })

License

MIT

rethinkdb-stream's People

Contributors

tjmehta avatar greenkeeper[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.