UNPKG

reactive-superglue

Version:

Stream-based middleware for I/O integration of sources and sinks

101 lines (81 loc) 2.34 kB
var _ = require("highland") var mongo = require("mongodb").MongoClient exports._db=_db exports._file=_file exports._folder=_folder exports._sql=_sql function _sql(db){ } _sql.prototype.query=function(query){ } function _file(name){ } function _db(db) { var self = this; if (typeof db == "string") this.db = _([db]).flatMap(_.wrapCallback(mongo.connect)).doto(db => self.db = _([db])) else this.db = _([db]).doto(db => self.db = _([db])) } _db.prototype.collection = function (name) { if(!this.db._reading) { var s = this.db.map((x) => (x.collection(name))); this.db._reading=true } else { var s = this.db.observe().map((x) => (x.collection(name))); } ["find", "aggregate", "mapReduce", "parallelCollectionScan", "geoNear"].forEach(function (m) { s[m] = function () { return mapDbReadOperation(s, m, arguments) } }); ["insert", "update", "remove"].forEach(function (m) { s[m] = function () { var args = Array.prototype.slice.call(arguments) if(m=="update") {} return mapDbWriteOperation(s, m, args) } }) return s; } module.exports = _db; function mapDbReadOperation(stream, operation, args) { args = Array.prototype.slice.call(args) return stream.flatMap(function (x) { return _(x[operation] .apply(x, args).stream()) }).flatten() } function mapDbWriteOperation(s, m, args) { args = Array.prototype.slice.call(args) var col; return function (x, err, push, next) { if (err) { return push(err) } if (x === _.nil) { if (push) return push(null, x) } if (!col) { s.flatMap(function (x) { col = x }).done(do_op(x, err, push, next)) } else do_op(x, err, push, next) } function do_op(x, next) { return col[m].apply(col, [x].concat(args).concat([next])) } } var mydb=new _db("mongodb://localhost/weplan_partis") mydb .collection("events") .find({event_type: "call_data_connection_state"}) .map(JSON.stringify) .map(x => x + "\n") .pipe(process.stdout) mydb .collection("events") .find({event_type: "sms"}) .map(JSON.stringify) .map(x => x + "\n") .pipe(process.stdout)