arangodb / arangojs

The official ArangoDB JavaScript driver.
https://arangodb.github.io/arangojs
Apache License 2.0
600 stars 106 forks source link

Read file line by line to edgecollection #372

Closed Rucchao closed 7 years ago

Rucchao commented 7 years ago

Hi, Could you please tell me how to read file line by line to the edge collection, the first column of each line is _from id , the second is _to id, I am really stuck in here since I am really confused about the usage of the Promise. the library I used to read file is 'line-reader'. Thanks a lot.

var lineReader = require('line-reader'), Promise = require('bluebird');

var eachLine = Promise.promisify(lineReader.eachLine);

return eachLine('test.txt', function(line) { collection2=db.edgeCollection('knows'); collection2.save({ _from: line.split(',')[0], _to: line.split(',')[1] }) }).then(function() { console.log('done'); }).catch(function(err) { console.error(err); });

pluma commented 7 years ago

I think your question is more about the use of line-reader than arangojs.

eachLine seems to treat the per-line-callback as synchronous unless you take a (third) callback argument so you probably have to use it like this:

var collection2 = db.edgeCollection('knows');
eachLine('test.txt', function (line, last, cb) {
  var tokens = line.split(',');
  collection2.save({
    _from: tokens[0],
    _to: tokens[1]
  }).then(function () {
    cb();
  }, function (err) {
    cb(err);
  });
}).then(function () {
  console.log('done');
}, function (err) {
  console.error(err);
});
pluma commented 7 years ago

I'm closing this on the assumption that the question has been answered. Feel free to reopen this issue if there's anything missing in the answer.