-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
84 lines (68 loc) · 2.71 KB
/
main.js
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
var DBDumper = require('./dumper');
var encoders = require('./encoders');
var program = require('commander');
var info = require('./package.json');
function toList(val) {
return val.split(',').map(function(ele) { return ele.replace(/^\s*|\s*$/g, ''); });
}
var encodersNames = Object.keys(encoders).join('|');
program
.version(info.version)
.option('-t, --tables [tables]', 'Table name', toList, [])
.option('-l, --limit [limit]', 'Condition to limite results of table', toList, [])
.option('-q, --query [query]', 'Query')
.option('-s, --source [source]', 'Path of the JSON config file source')
.option('-r, --force-references [depth]', 'References', parseInt, 0)
.option('-o, --output <outputfile>', 'Name of output file')
.option('-e, --encoder <' + encodersNames + '>', 'Name of encoder', new RegExp('^(' + encodersNames + ')$', 'ig'), 'sql')
.option('-H, --host <host>', 'Database host')
.option('-P, --port <port>', 'Database port')
.option('-d, --database <database>', 'Database schema')
.option('-u, --user <user>', 'Database username')
.option('-p, --password <password>', 'Database password')
.option('-c, --max-conn-limit []', 'Database max limit of parallel connections')
.parse(process.argv);
if (program.source) {
try {
program = require(program.source);
} catch (err) {
console.error('O caminho do arquivo informado não é acessível ou não existe.');
process.exit(1);
}
}
if (program.tables.length == 0) {
console.error('Informe a tabela a ser pesquisada');
process.exit(1);
}
if (!program.host || !program.port || !program.database || !program.user || !program.password) {
console.error('Informe os dados para conexão com o banco de dados.');
process.exit(1);
}
var entities = [];
for (var i = 0; i < program.tables.length; i++) {
var limit = program.limit[i] ? program.limit[i] : 'where 1=1';
if (program.query) {
limit = null;
}
entities.push({
table: program.tables[i],
query: program.query || null,
limit: limit,
forceReferences: program.forceReferences || 0
});
}
console.log('-- Dump params');
console.log('-- Date...: ', new Date());
console.log('-- Encoder: ', program.encoder || sql);
console.log('-- Output.: ', program.output || 'console');
var dumper = new DBDumper({
host: program.host,
port: program.port,
database: program.database,
user: program.user,
password: program.password,
maxConnLimit: program.maxConnLimit || 100,
}, program.encoder, program.output);
dumper.dump(entities, function(err) {
if (err) throw err;
});