-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
executable file
·52 lines (47 loc) · 1.36 KB
/
index.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
import mongoose from 'mongoose';
import Knex from 'knex';
import getFromMongo from './src/get-from-mongo.js';
import putToPostgres from './src/put-to-postgres.js';
export default async ({ connections, collections }) => {
console.log('Starting migration...');
let mongooseConn;
process.stdout.write('Connectiong to mongo... ');
try {
mongooseConn = await mongoose.connect(connections.mongo,
{
useNewUrlParser: true, useUnifiedTopology: true
});
console.log('connected.');
} catch (err) {
console.log('ERROR MONGO');
console.err(err);
}
let knex;
process.stdout.write('Connectiong to postgres... ');
try {
knex = Knex({
client: 'pg',
connection: connections.postgres
});
console.log('connected.');
} catch (err) {
console.log('ERROR POSTGRES');
console.err(err);
}
performProcess(mongooseConn, knex, collections)
.then(() => console.log('Finished successfully.'))
.catch((err) => console.error(err))
.finally(() => process.exit(0));
};
async function performProcess(mongooseConn, knex, collections) {
for (const collection of collections) {
const rows = await getFromMongo(mongooseConn, collection.collectionName);
const idsMap = await putToPostgres({
knex,
collections,
tableName: collection.tableName,
rows
});
collection.idsMap = idsMap;
}
}