Convert a rethinkdb cursor into a readable stream
npm install rethinkdb-streambash
npm i --save rethinkdb-stream
`Usage
$3
`js
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)
})
`$3
`js
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 }
*/
})
})
`$3
rethinkdb streams have a close method which calls cursor.close()
`js
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)
})
``