-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimportarProfesionales.ts
More file actions
62 lines (54 loc) · 1.63 KB
/
importarProfesionales.ts
File metadata and controls
62 lines (54 loc) · 1.63 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
import * as mysql from 'mysql';
import * as mongodb from 'mongodb';
import * as config from './config';
import * as consultas from './consultas';
var myConnection = mysql.createConnection({
host: config.mysqlServer,
database: config.mysqlDatabase,
user: config.mysqlUser,
password: config.mysqlPassword,
});
const url = config.urlMigracion;
var limit = 999;
var max = 20000;
myConnection.connect(function (err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + myConnection.threadId);
})
run();
async function run() {
let profesionales = null;
for (var i = 0; i <= max; i += limit) {
try {
profesionales = await getProfesionales(i, limit);
if (profesionales.length > 0) {
insertmongo(profesionales);
} else {
console.log('game over');
}
} catch (e) {
console.log('Error: ');
}
}
};
async function getProfesionales(desde, hasta) {
return new Promise((resolve, reject) => {
myConnection.query(consultas.consultaProfesionales, [desde, hasta], function (error, results, fields) {
if (error) reject(error);
resolve(results);
});
});
}
async function insertmongo(datos) {
try {
let conn = await mongodb.MongoClient.connect(url);
let db = await conn.db('andes');
let result = await db.collection('profesional').insertMany(datos, { ordered: false })
conn.close();
} catch (err) {
console.log("error insertMongo----", err, "datos: ", datos);
}
}