Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var couchbase = require('couchbase');
var ViewQuery = couchbase.ViewQuery;
var run = function () {
importData();
};
var importData = function () {
var raptor = new couchbase.Cluster('couchbase://127.0.0.1');
var raptorBucket = raptor.openBucket('raptor-objects');
var list = require('fs').readFileSync('./export').toString().split("\n");
var cnt = 0;
var importRecord = function (raw, then) {
console.log(raw);
var record;
try {
record = JSON.parse(raw);
}
catch(e) {
console.log("Cannot parse:", raw);
if(then) then();
return;
}
record.settings = {};
if(record.security) {
delete record.security;
}
raptorBucket.upsert(record.id, record, function (err, res) {
if(err) {
console.log('operation failed', err);
throw new Error(err);
}
console.log("Added " + record.id);
if(then) then();
});
};
var oncomplete = function () {
cnt++;
console.log("Inserted " + cnt + ", remain " + list.length);
if(list.length) {
importRecord(list.pop(), oncomplete);
}
};
importRecord(list.pop(), oncomplete);
};
var exportData = function () {
var servioticy = new couchbase.Cluster('couchbase://127.0.0.1:8091');
var servioticyBucket = servioticy.openBucket('serviceobjects');
var servioticyQuery = ViewQuery.from('migrate', 'migrate');
// console.log("query servioticy");
servioticyBucket.query(servioticyQuery, function (err, results) {
// console.log("query done");
if(err) {
throw new Error(err);
}
for(var i in results) {
var obj = results[i].value;
console.log(JSON.stringify(obj));
}
process.exit();
});
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment