reactive-superglue
FRP (functional reactive programming) middleware for integrating diverse data sources and data sinks / endpoints.
We leverage highland.js to provide a light and robust data processing framework, but we are team players, it should work seamlessly with whatever frameworks you are currently using (please report issues!).
reactive-superglue is designed to simplify dealing with real-world data that is on the move. You can call it streams, monoids, observables, but at the end of the day, it usually boils down to read from here, do stuff, write there.
This lib is currently being used in production and battle-tested by TramitApp
##hello superglue
var _=require("superglue")
var db=_.mongodb("mongodb://localhost/test")
db.collection("users")
.find({active: true}, {id_: false, name:true})
.uniq()
.upsert(db.collection("unique_user_names"))
.done()
Simply put, you get a neat chained API and can use all the map
, reduce
, scan
, ... processing APIs of highland.js together with superglue's reading and writing connectors. Some more examples:
##No fluff, just stuff
var _=require("reactive-superglue")
var query=_.db("mongodb://localhost/my_db")
.collection("my_collection")
.find({type: "some_type"});
query.to_jsonfile("path/to/file").done()
var name_is_john_collection=db("mongodb://localhost/another_db").collection("name_is_john_collection")
_.from_jsonfile("path/to/file")
.filter(obj => obj.name=="John")
.upsert(name_is_john_collection)
.done()
##The glued libraries
###mongodb
The mongodb integration provides a Query Builder API and registers (aka mixes in) convenience methods
to the stream for writing to mongodb.
var _=require("reactive-superglue")
var db=_.db("mongodb://localhost/test")
var collection=db.collection("reactive-superglue-collection")
collection.find()
.map(JSON.stringify)
.pipe(process.stdout)
collection.find({field1: "somevalue"}, {_id: 0, field1: 1})
.map(JSON.stringify)
.pipe(process.stdout)
db.collection("some_collection")
.aggregate([{$group: {_id: null, count: {$sum: 1}}}], {allowDiskUse: true})
.to_jsonfile("my/file.json")
db.collection("reactive-superglue-collection")
.find()
.map(x => x.variable)
.reduce(0, (a, b) => a+b)
.map(x => "result is"+x)
.each(function(item){
console.log("Printing result -->", item)
}
var data=[{a:1}, {b:2}]
_(data)
.insert(db.collection("reactive-superglue"))
.done()
_(data)
.upsert(db.collection("reactive-superglue"))
.done()
_(data)
.remove(db.collection("reactive-superglue"))
.done()
####reading from mongodb
A factory function is registered under the _
namespace for each data source. In mongodb's case: _.db(mongo_url)
does the trick, by returning a lazy connection to the database. The mongo_url
must be a valid mongodb connection string url: mongodb://server_name:port/database
####transforming the data flow
You can use all the transforms from highland.js. A couple of examples:
map(fn)
applies fn
to each element:
_.db("mongodb://localhost/test")
.collection("col")
.find()
.map(function(x){return x.some_number})
.map(x => x+2)
.each(x => console.log("received x=", x)
reduce(memo, fn)
acummulates the result of fn
_.db("mongodb://localhost/test")
.collection("col")
.find()
.map(function(x){return x.some_number})
.reduce(0, (a,b) => a+b)
.each(x => console.log("sum of x.some_number", x)
####consuming the data flow
Note that streams are lazy, so you need to pull from them in order to get the data flowing.
- There are several ways to do this:
.done(fn)
consumes the stream and calls fn once finished.
_([{a:1, b:2}, {a:5, b:7}])
.insert(collection("col"))
.done()
.each(fn)
consumes the stream by calling fn with each item in the stream.toArray(fn)
consumes the stream converting it to an array.pipe(writable)
consumes the stream by piping it to a standard node.js stream.Writable, including http responses, files, process.stdout, and so on (although we provide convenience mix-ins
for the most common ones).
This is just highland.js syntax, so be sure to take a look at its docs.