Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function toDB(modelName, projectId, map = false, concurrency = 10) {
- if (!this.stream) {
- throw new Error(`${modelName} import error: no stream present!`);
- }
- map = map ? map : p21map[modelName];
- this.stream.pipe(pressure(function (data, cb) {
- const row = { projectId: projectId };
- const val = Object.values(data);
- if (map.length !== val.length) {
- return cb(new Error(`${modelName} import error: map.length != val.length`));
- }
- // map data (val) to row using map
- for (let i = 0; i < map.length; i++) {
- row[map[i]] = val[i].length ? val[i] : null;
- }
- db[modelName].create(row)
- .then(() => cb())
- .catch(function (err) {
- db.ImportError.create({
- projectId : projectId,
- model : modelName,
- data : JSON.stringify(row),
- error : JSON.stringify(err) // JSON.stringify(err.errors)
- });
- cb();
- });
- }, concurrency));
- return new Promise((resolve, reject) => {
- this.stream.on('end', resolve);
- this.stream.on('error', reject);
- this.stream.resume();
- });
- }
Add Comment
Please, Sign In to add comment