Guest User

Untitled

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