Guest User

Untitled

a guest
May 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. function toDB(modelName, projectId, map = false, concurrency = 10) {
  2. if (!this.stream) {
  3. throw new Error(`${modelName} import error: no stream present!`);
  4. }
  5.  
  6. map = map ? map : p21map[modelName];
  7.  
  8. this.stream.pipe(pressure(function (data, cb) {
  9. const row = { projectId: projectId };
  10. const val = Object.values(data);
  11.  
  12. if (map.length !== val.length) {
  13. return cb(new Error(`${modelName} import error: map.length != val.length`));
  14. }
  15.  
  16. // map data (val) to row using map
  17. for (let i = 0; i < map.length; i++) {
  18. row[map[i]] = val[i].length ? val[i] : null;
  19. }
  20.  
  21. db[modelName].create(row)
  22. .then(() => cb())
  23. .catch(function (err) {
  24. db.ImportError.create({
  25. projectId : projectId,
  26. model : modelName,
  27. data : JSON.stringify(row),
  28. error : JSON.stringify(err) // JSON.stringify(err.errors)
  29. });
  30.  
  31. cb();
  32. });
  33. }, concurrency));
  34.  
  35. return new Promise((resolve, reject) => {
  36. this.stream.on('end', resolve);
  37. this.stream.on('error', reject);
  38. this.stream.resume();
  39. });
  40. }
Add Comment
Please, Sign In to add comment