-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
309 lines (286 loc) · 9.59 KB
/
app.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
var express = require('express');
var session = require('express-session');
var app = express();
var cors = require("cors");
app.use(cors())
//uuid automatically generate unique strings
const uuidv4 = require('uuid/v4');
// const mongo = require('mongodb').MongoClient;
const mongoose = require("mongoose");
const processFileASync = require("./processFile");
var bodyParser = require('body-parser');
//bodyParser is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.
// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(
session({
secret: "Shh, its a secret!",
name: 'aaa',
// store: sessionStore, // connect-mongo session store
proxy: true,
resave: true,
saveUninitialized: true
})
);
var db = null;
var sourceDb = {
dbClient: null,
dbCon: null,
dbName: null
};
var destinationDb = {
dbClient: null,
dbCon: null,
dbName: null
};
// connectToMongo("mongodb://localhost:27017/seasions").then(database => {
// db = database;
// });
// var MongoClient = require('mongodb').MongoClient
// , format = require('util').format;
app.get('/', function(req, res) {
console.log(req.session,'req.session')
if(req.session.page_views){
req.session.page_views++;
res.send("You visited this page " + req.session.page_views + " times");
} else {
req.session.page_views = 1;
res.send("Welcome to this page for the first time!");
}
});
app.post('/initiate-db-migration', function (req, res) {
console.log(processFileASync, "processFileASync");
if((!req.body.sourceDb && !req.body.destinationDb) && req.body.dbName) return handleError(req,res,'Please provide db name and also sourceDb or destinationDb flag true or false');
// let obj = findSeasion(req,null,null);
// if(req.body.sourceDb && obj.sourceDb) return res.status(200).send('Already Connection Exist');
// if(req.body.destinationDb && obj.destinationDb) return res.status(200).send('Already Connection Exist');
connectToMongo(req.body.sourceDb).then(client => {
if(client && client.name) {
getCollectionsList(client)
.then(allCollections => {
console.log(allCollections, "allCollections");
if(allCollections && allCollections.length > 0) {
sourceDb.collections = allCollections;
sourceDb.dbClient = client;
sourceDb.dbName = client.name;
sendData(client, req.body.destinationDb, allCollections);
}
})
res.status(200).send('Connected')
} else {
return handleError(req,res,'Connection failed');
}
}).catch(err => {
console.log(err,'err')
if(err) return handleError(req,res,err);
});
});
async function sendData(client,destinationDb,allCollections) {
connectToMongo(destinationDb).then(detClient => {
if (detClient && detClient.name) {
destinationDb.dbClient = detClient;
// destinationDb.dbCon = client.db(req.body.dbName);
destinationDb.dbName = detClient.name;
console.log(detClient.name, "destinationDb.dbName");
console.log(client.name,'source db');
migrate(client, detClient, allCollections);
}
});
}
app.get('/getCollections/:dbType', function(req,res) {
let dbType = req.params.dbType;
if(dbType !== 'sourceDb' && dbType !== 'destinationDb') return handleError(req,res,'which db source or detination');
getCollectionsList(dbType === 'sourceDb' ? sourceDb.dbClient : destinationDb.dbClient)
.then(response => {
res.status(200).send(response);
}).catch(err => {
return handleError(req,res,err);
});
});
app.get('/getDataOfCollection/:dbType/:collectionName', function(req,res) {
let collectionName = req.params.collectionName;
let dbClient = req.params.dbType === 'sourceDb' ? sourceDb.dbClient : destinationDb.dbClient;
if(!collectionName || !dbClient) {
return handleError(req,res,'Please Provide a valid collection name.');
}
findDataInCollection(dbClient,collectionName)
.then(data => {
res.status(200).send(data);
}).catch(err => {
return handleError(req,res,err)
})
});
app.get('/export-db', function(req, res) {
let dbClient = sourceDb.dbClient;
getCollectionsList(dbClient)
.then(collections => {
if (collections && collections.length > 0) {
collections.forEach(eachCollection => {
exportCollectionToJson(collection);
// findDataInCollection(dbClient, eachCollection).then(dataOfCollection => {
// processFileASync.writeFile(eachCollection + ".json", JSON.stringify(dataOfCollection), function(err,data) {
// if (err) return handleError(req, res, "err");
// console.log(data, "d");
// });
// });
});
res.status(200).send('Export In Proccess. Please wait');
} else {
return handleError(req,res,'No Collections Record found try connecting again')
}
})
.catch(err => {
console.log(err,'err');
return handleError(req, res, err);
});
});
async function exportCollectionToJson(dbClient, eachCollection) {
findDataInCollection(dbClient, eachCollection).then(dataOfCollection => {
if(dataOfCollection) {
processFileASync.writeFile(eachCollection + ".json", JSON.stringify(dataOfCollection),function(err, data) {
if (err) return handleError(req, res, "err");
console.log(data, "d");
return data;
});
} else {
console.log('no data in collecton' + eachCollection);
}
});
}
async function migrate(sourceDb, destinationDb, collections) {
collections.forEach(eachCollection => {
let ignore = ["objectlabs-system.admin.collections", "objectlabs-system", "system.indexes"];
if (ignore.indexOf(eachCollection) === -1) {
findDataInCollection(sourceDb, eachCollection)
.then(dataOfCollection => {
writeDataInCollection(destinationDb, eachCollection, dataOfCollection)
.then(res => {})
.catch(errr => {
console.log(errr, "errr in writeDataInCollection");
});
});
}
});
}
app.get("/migrate", function(req,res) {
getCollectionsList(sourceDb.dbClient).then(collections => {
if (collections && collections.length > 0) {
count = collections.length;
console.log(collections, "allcollections");
res.status(200).send("Export In Proccess. Please wait");
} else {
return handleError(
req,
res,
"No Collections Record found try connecting again"
);
}
});
})
app.get("/import-db", function(req, res) {
let dbClient = sourceDb.dbClient;
getCollectionsList(dbClient)
.then(collections => {
if (collections && collections.length > 0) {
collections.forEach(eachCollection => {
console.log(eachCollection, "eachCollection");
processFileASync.readFile(eachCollection + '.json')
.then(collectionResData => {
collectionResData = JSON.parse(collectionResData);
writeDataInCollection(destinationDb.dbClient, eachCollection,collectionResData)
.then(res => {
}).catch(errr => {
console.log(errr, "errr in writeDataInCollection");
})
}).catch(err => {
console.log(err,'err while reading files from directory');
});
});
}
})
.catch(err => {
return handleError(req, res, err);
});
});
app.get("/test-reading", function(req,res) {
processFileASync.readFile('file.json')
.then(content => {
console.log(content,'c')
}).catch(err => {
console.log(err,'')
})
});
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
});
function handleError(req,res,msg) {
return res.status(500).send(msg);
}
function findSeasion(req,sourceDb,destinationDb) {
let obj = {};
if(req.session.uuid && req.session.mongoConnection){
obj.uuid = req.session.uuid;
obj.mongoConnection = ++req.session.mongoConnection;
} else {
obj.uuid = req.session.uuid = uuidv4();
obj.mongoConnection = req.session.mongoConnection = 1;
}
if(sourceDb || req.session.sourceDb) {
obj.sourceDb = req.session.sourceDb ? req.session.sourceDb : req.session.sourceDb = sourceDb;
}
if(destinationDb || req.session.destinationDb) {
obj.destinationDb = req.session.destinationDb ? req.session.destinationDb : req.session.destinationDb = destinationDb;
}
return obj;
}
function findDataInCollection(dbInstance,collectionName) {
return new Promise((resolve, reject) => {
var collection = dbInstance.db.collection(collectionName);
collection.find().toArray(function(err, data) {
if (err) reject(err);
resolve(data);
});
});
}
function writeDataInCollection(dbClient,collectionName,data) {
return new Promise((resolve, reject) => {
var collection = dbClient.db.collection(collectionName);
console.log(collection,data,'d');
collection.insertMany(data, { "ordered":false }, function(err, response) {
if (err) return reject(err);
resolve(response);
});
});
}
function getCollectionsList(client) {
return new Promise((resolve,reject) => {
let allCollections = [];
//create client by providing database name
client.db.listCollections().toArray(function(err, collections) {
if(err) reject(err);
//iterate to each collection detail and push just name in array
collections.forEach(eachCollectionDetails => {
allCollections.push(eachCollectionDetails.name);
});
//close client
resolve(allCollections);
});
});
}
function connectToMongo(url) {
return new Promise((resolve, reject) => {
mongoose.createConnection(url, { useNewUrlParser: true, useUnifiedTopology: true },
function(err, db) {
if (err) {
return reject(err);
}
resolve(db);
}
);
});
}