Guest User

Untitled

a guest
Apr 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function import_csv(fileName, modelName) {
  2. const errFile = fs.createWriteStream('csv/err/'+fileName+'.json');
  3.  
  4. fs.createReadStream('csv/'+fileName)
  5. .pipe(iconv.decodeStream('win1252'))
  6. .pipe(csv({ separator: ';' }))
  7. .pipe(pressure(function (data, cb) {
  8. const row = Object.keys(data).reduce((acc, key) => {
  9. const newKey = key.replace(/-/g, '_');
  10. const newData = data[key].length ? data[key] : null;
  11. return { ...acc, [newKey]: newData };
  12. }, {});
  13.  
  14. db[modelName].create(row)
  15. .then(() => cb())
  16. .catch(function (err) {
  17. row.errors = err.errors;
  18. JSON.stringify(row).pipe(errFile);
  19. });
  20. }, concurrency));
  21. }
Add Comment
Please, Sign In to add comment