Created
April 12, 2015 08:11
-
-
Save seriousme/8e4918b114d617a920a8 to your computer and use it in GitHub Desktop.
Streaming loki changes into rethinkdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rdb = require('rethinkdb'), | |
loki = require('lokijs'), | |
db, | |
connection; | |
function forwardInsert(db, connection, table, record, callback) { | |
var cb = callback || function (err, obj) { | |
if (err) { | |
throw err; | |
} | |
console.log(JSON.stringify(obj, null, 2)); | |
} | |
db.getCollection(table).insert(record); | |
console.log(db.filename, table); | |
rdb.db(db.filename).table(table).insert(record).run(connection, callback); | |
} | |
function handleError()(err, obj) { | |
if (err) { | |
throw err; | |
} | |
} | |
function run() { | |
var dbname = 'rethinkloki', | |
table = 'users', | |
db = new loki(dbname), | |
users = db.addCollection(table, { | |
unique : ['username'] | |
}); | |
rdb.connect({ | |
host : 'localhost', | |
port : 28015 | |
}, function (err, conn) { | |
if (err) { | |
console.warn(err); | |
throw err; | |
} | |
connection = conn; | |
db.on('insert', function rbdInsert(record) { | |
rdb.db(db.filename).table(table).insert(record).run(connection, handleError); | |
}); | |
db.insert({ | |
username : 'joe', | |
age : 40 | |
}); | |
console.log(users.find()); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment