@giorgi-g/csv-parser
Version:
CSV parser for migrations with Sequelizer
155 lines (154 loc) • 6.73 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CSVParser = void 0;
const getStream = require("get-stream");
const fs = require("fs");
const { DB_ENV, FILE_EXTENSION, FILES_DIR } = require("../../config");
const { parse } = require("csv-parse");
class CSVParser {
constructor(fileName, options) {
this.fileName = '';
this.mapKeyIndexes = [];
this.mapKeySeparator = '-';
this.mergeMapKeyValues = false;
this.delimiter = ',';
this.rootDir = FILES_DIR;
this.rootEnv = DB_ENV;
this.fileExtension = FILE_EXTENSION;
this.mergeMapKeyData = (currentRow, prevData) => {
const array = [];
array.push(currentRow);
if (Array.isArray(prevData)) {
prevData.forEach(item => {
array.push(item);
});
}
else {
array.push(prevData);
}
return array;
};
this.rowToObject = (row = [], columns = []) => {
const currentRow = {};
row.forEach((item, index) => {
// @ts-ignore
currentRow[columns[index]] = item;
});
if (this.classPath != null) {
const plug = require(this.classPath);
const constructorName = Object.keys(plug)[0];
const plugin = new plug[constructorName](currentRow);
if (this.classObjectGetterName != null) {
return plugin[this.classObjectGetterName];
}
return plugin;
}
return currentRow;
};
this.generateMapKey = (row) => {
let mapKey = '';
this.mapKeyIndexes.forEach((key, index) => {
if (index !== this.mapKeyIndexes.length - 1) {
mapKey += `${row[key]}${this.mapKeySeparator}`;
}
else {
mapKey += row[key];
}
});
return mapKey;
};
this.fileName = fileName;
if (options.mapKeyIndexes != null && options.mapKeyIndexes.length)
this.mapKeyIndexes = options.mapKeyIndexes;
if (options.mapKeySeparator != null && options.mapKeySeparator.length)
this.mapKeySeparator = options.mapKeySeparator;
if (options.classPath != null)
this.classPath = options.classPath;
if (options.classObjectGetterName != null)
this.classObjectGetterName = options.classObjectGetterName;
if (options.delimiter != null)
this.delimiter = options.delimiter;
if (options.rootDir != null)
this.rootDir = options.rootDir;
if (options.rootEnv != null)
this.rootEnv = options.rootEnv;
if (options.fileExtension != null)
this.fileExtension = options.fileExtension;
if (options.mergeMapKeyValues != null)
this.mergeMapKeyValues = options.mergeMapKeyValues;
if (this.classPath != null && !fs.existsSync(this.classPath)) {
this.classPath = `../../../../../${this.classPath}`;
}
}
Read() {
return __awaiter(this, void 0, void 0, function* () {
const filePath = `./${this.rootDir}/${this.fileName}_${this.rootEnv}.${this.fileExtension}`;
const parseStream = parse({ delimiter: this.delimiter });
const data = yield getStream.array(fs.createReadStream(filePath).pipe(parseStream));
const columns = Object.values(data[0]);
if (data.length && !this.mapKeyIndexes.length) {
const rows = new Map();
data.forEach((row, rowIndex) => {
if (rowIndex !== 0) {
const currentRow = this.rowToObject(row, columns);
rows.set(rowIndex - 1, currentRow);
}
});
return rows;
}
if (data.length && this.mapKeyIndexes.length) {
const rows = new Map();
data.forEach((row, rowIndex) => {
if (rowIndex !== 0) {
const key = this.generateMapKey(row);
const currentRow = Object.assign({}, row);
if (this.classPath == null) {
if (this.mergeMapKeyValues) {
const prevData = rows.get(key);
if (prevData != null) {
const arr = this.mergeMapKeyData(currentRow, prevData);
rows.set(key, arr);
}
else {
rows.set(key, currentRow);
}
}
else {
rows.set(key, currentRow);
}
}
else {
const currentRow = this.rowToObject(row, columns);
if (this.mergeMapKeyValues) {
const prevData = rows.get(key);
if (prevData != null) {
const arr = this.mergeMapKeyData(currentRow, prevData);
rows.set(key, arr);
}
else {
rows.set(key, currentRow);
}
}
else {
rows.set(key, currentRow);
}
}
}
});
return rows;
}
return new Map();
});
}
;
}
exports.CSVParser = CSVParser;