-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·129 lines (104 loc) · 4.48 KB
/
Copy pathindex.js
File metadata and controls
executable file
·129 lines (104 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var reader;
module.exports.load = function(options, cb){
const lineReader = require('line-reader');
reader = {};
lineReader.open(options.file, {
encoding : options.encoding
}, function(err, lr) {
if (err) throw err;
reader = new Reader(Object.assign(options, {reader : lr}));
});
return { read: fakeReader }
}
function fakeReader(cb){
setTimeout(() => {
reader ? reader.read(cb) : fakeReader(cb)
}, 50);
}
class Reader{
constructor(options){
this.header = options.header;
this.onFinish = options.onFinish;
this.reader = options.reader;
this.separator = options.separator;
this.index = 0;
this.mapLinker = options.mapLinker;
this.indexesMap = this.getIndexesByMapper();
options.onStart();
}
getIndexesByMapper(){
let map = {};
const models = require('mongoose').models;
for(let model in models){
const schema = models[model].schema.obj;
const modelKey = model[0].toLowerCase() + model.slice(1);
map[modelKey] = {};
for(let prop in schema){
for(let attr in schema[prop]){
if(attr == this.mapLinker.attr){
if(this.mapLinker.type == "index"){
map[modelKey][prop] = schema[prop][attr];
} else if(this.mapLinker.type == "name"){
if(!this.columns){
const separator = this.separator;
let columns = [];
this.reader.nextLine((err, line) => columns = line.split(separator));
this.columns = columns;
}
let colIndex = this.columns.map(col => {
if(this.mapLinker.model && this.mapLinker.model.remove){
if(this.mapLinker.model && this.mapLinker.model.remove){
let cols = col.split(this.mapLinker.model.separator);
cols[0] = cols[0].replace(this.mapLinker.model.remove, "");
col = cols.join(this.mapLinker.model.separator);
}
}
return col.toLowerCase();
}).indexOf(modelKey.toLowerCase() + this.mapLinker.model.separator + prop);
if(colIndex > -1){
map[modelKey][prop] = colIndex;
}
}
}
}
}
}
return map;
}
read(cb){
if (this.reader.hasNextLine()) {
return new Promise((resolve, reject) => {
this.reader.nextLine(function(err, line) {
resolve(line)
});
})
.then(line => {
if(line){
let cols = line.toString().split(this.separator);
let entities = {};
// o header do this.mapLinker.type === "name" é obrigatório e lido em getIndexesByMapper()
if(this.header || this.mapLinker.type === "name"){
for(let entity in this.indexesMap){
entities[entity] = this.parse(cols, this.indexesMap[entity]);
}
return cb(entities, ++this.index);
}
this.header = true;
}
})
.then(() => this.read(cb));
} else {
this.onFinish(this.index);
this.reader.close(function(err) {
if (err) throw err;
});
}
}
parse(cols, map){
let myObj = {};
for(let index in map){
myObj[index] = cols[map[index]];
}
return myObj;
}
}