Skip to content

Commit be7fea4

Browse files
feat: migrate-to-mongo 0.4.6
1 parent a0cd4d3 commit be7fea4

File tree

10 files changed

+2503
-3394
lines changed

10 files changed

+2503
-3394
lines changed

lib/api/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const functions = {
55
uninstall: require('./uninstall.js'),
66
cv: require('./changeVersion.js'),
77
changeVersion: require('./changeVersion.js'),
8+
'migrate-to-mongo': require('./migrateToMongo/index.js'),
89
}
910

1011
module.exports = async (argv) => {

lib/api/migrateToMongo/index.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
const verifyEvolutionInstallation = require('../../utils/verifyEvolutionInstallation.js');
2+
const cliProgress = require('cli-progress');
3+
const inquirer = require('inquirer');
4+
const fs = require('fs');
5+
const path = require('path');
6+
const colors = require('ansi-colors');
7+
const mongoose = require('mongoose');
8+
9+
10+
const migratorsFunctions = {
11+
INSTANCE: require('./migrators/instance.js'),
12+
CONTACTS: require('./migrators/contacts.js'),
13+
NEW_MESSAGE: require('./migrators/newMessage.js'),
14+
MESSAGE_UPDATE: require('./migrators/messageUpdate.js'),
15+
CHATS: require('./migrators/chats.js'),
16+
};
17+
18+
19+
module.exports = async () => {
20+
const isEvolutionInstalled = verifyEvolutionInstallation();
21+
if (!isEvolutionInstalled) return;
22+
23+
const instancesNames = fs.readdirSync(path.join(process.cwd(), 'instances')).filter((file) => {
24+
return fs.statSync(path.join(process.cwd(), 'instances', file)).isDirectory();
25+
});
26+
27+
const questions = [
28+
{
29+
type: 'input',
30+
name: 'mongodbUrl',
31+
message: 'MongoDB url:',
32+
default: 'mongodb://admin:password@localhost:27017',
33+
},
34+
{
35+
type: 'input',
36+
name: 'prefix',
37+
message: 'MongoDB prefix:',
38+
default: 'evolution',
39+
},
40+
{
41+
type: 'checkbox',
42+
name: 'saveData',
43+
message: 'What data do you want to migrate?',
44+
choices: [
45+
{
46+
name: 'Instance',
47+
value: 'INSTANCE',
48+
checked: true,
49+
},
50+
{
51+
name: 'New Message',
52+
value: 'NEW_MESSAGE',
53+
checked: true,
54+
},
55+
{
56+
name: 'Message Update',
57+
value: 'MESSAGE_UPDATE',
58+
checked: true,
59+
},
60+
{
61+
name: 'Contacts',
62+
value: 'CONTACTS',
63+
checked: true,
64+
},
65+
{
66+
name: 'Chats',
67+
value: 'CHATS',
68+
checked: true,
69+
},
70+
],
71+
},
72+
{
73+
type: 'checkbox',
74+
name: 'selectedInstances',
75+
message: 'Select instances to migrate:',
76+
choices: instancesNames.map((instanceName) => {
77+
return {
78+
name: instanceName,
79+
value: instanceName,
80+
checked: true,
81+
}
82+
}),
83+
}
84+
];
85+
86+
// get folders from /instances
87+
const answers = await inquirer.prompt(questions);
88+
89+
90+
// connect to mongodb
91+
console.log('\n\n🌱 Connecting to MongoDB...');
92+
const conn = await mongoose.createConnection(answers.mongodbUrl, {
93+
dbName: answers.prefix + '-whatsapp-api',
94+
}).asPromise();
95+
const connInstance = answers.saveData.includes('INSTANCE') ? await mongoose.createConnection(answers.mongodbUrl, {
96+
dbName: answers.prefix + '-instances',
97+
}).asPromise() : null;
98+
console.log('🌱 Connected to MongoDB!\n\n');
99+
100+
const instancesBar = new cliProgress.SingleBar({
101+
format: 'Instance: ' + colors.blue('{instanceName}') + ' |' + colors.cyan('{bar}') + '| {percentage}% || {value}/{total} Instances',
102+
}, cliProgress.Presets.shades_classic);
103+
104+
instancesBar.start(answers.selectedInstances.length, 0);
105+
for (const instanceName of answers.selectedInstances) {
106+
instancesBar.update({ instanceName });
107+
108+
const instanceBars = new cliProgress.MultiBar({
109+
format: '|' + colors.cyan('{bar}') + '| ' + colors.blue('{process}') + ' | {percentage}% || {value}/{total} Files',
110+
clearOnComplete: true,
111+
}, cliProgress.Presets.shades_classic);
112+
113+
for (const migration of answers.saveData) {
114+
await migratorsFunctions[migration](instanceName, answers, instanceBars, conn, connInstance);
115+
}
116+
117+
instanceBars.stop();
118+
instancesBar.increment();
119+
}
120+
instancesBar.stop();
121+
122+
123+
// disconnect from mongodb
124+
console.log('\n\n🌱 Disconnecting from MongoDB...');
125+
await conn.close();
126+
console.log('🌱 Disconnected from MongoDB!\n\n');
127+
128+
console.log('🌱 Migration completed!\n\n');
129+
process.exit(0);
130+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const FOLDER_PATH = [{
2+
path: "/store/chats",
3+
key: "chats"
4+
}]
5+
6+
const fs = require("fs")
7+
const path = require("path")
8+
9+
10+
module.exports = async (instanceName, options, progressBars, conn) => {
11+
var files = []
12+
13+
FOLDER_PATH.forEach(folder => {
14+
files = files.concat(getFiles(instanceName, folder))
15+
})
16+
const progress = progressBars.create(files.length, 0)
17+
progress.update({ process: 'Chats' })
18+
for (const file of files) {
19+
const collectionName = file.key
20+
const collection = conn.collection(collectionName)
21+
22+
const data = JSON.parse(fs.readFileSync(file.path, 'utf8'))
23+
data._id = file.path.split('\\').pop().split('.')[0]
24+
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
25+
26+
progress.increment()
27+
}
28+
}
29+
30+
function getFiles(instanceName, opts) {
31+
var files = []
32+
const folder = opts.path || opts
33+
const folderPath = path.join(process.cwd(), folder)
34+
35+
fs.readdirSync(folderPath).forEach(file => {
36+
const filePath = path.join(folderPath, file)
37+
if (fs.statSync(filePath).isDirectory()) {
38+
files = files.concat(getFiles(instanceName, { ...opts, path: `${folder}/${file}` }))
39+
} else if (file.includes(instanceName) || folder.includes(instanceName)) {
40+
files.push({ ...opts, path: filePath })
41+
}
42+
})
43+
return files
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const FOLDER_PATH = [{
2+
path: "/store/contacts",
3+
key: "contacts"
4+
}]
5+
6+
const fs = require("fs")
7+
const path = require("path")
8+
9+
10+
module.exports = async (instanceName, options, progressBars, conn) => {
11+
var files = []
12+
13+
FOLDER_PATH.forEach(folder => {
14+
files = files.concat(getFiles(instanceName, folder))
15+
})
16+
const progress = progressBars.create(files.length, 0)
17+
progress.update({ process: 'Contacts' })
18+
for (const file of files) {
19+
const collectionName = file.key
20+
const collection = conn.collection(collectionName)
21+
22+
const data = JSON.parse(fs.readFileSync(file.path, 'utf8'))
23+
data._id = file.path.split('\\').pop().split('.')[0]
24+
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
25+
26+
progress.increment()
27+
}
28+
}
29+
30+
function getFiles(instanceName, opts) {
31+
var files = []
32+
const folder = opts.path || opts
33+
const folderPath = path.join(process.cwd(), folder)
34+
35+
fs.readdirSync(folderPath).forEach(file => {
36+
const filePath = path.join(folderPath, file)
37+
if (fs.statSync(filePath).isDirectory()) {
38+
files = files.concat(getFiles(instanceName, { ...opts, path: `${folder}/${file}` }))
39+
} else if (file.includes(instanceName) || folder.includes(instanceName)) {
40+
files.push({ ...opts, path: filePath })
41+
}
42+
})
43+
return files
44+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const FOLDER_PATH = [
2+
{
3+
path: "/store/auth/apikey",
4+
key: "authentication"
5+
},
6+
{
7+
path: "/instances",
8+
secondaryConnection: true
9+
},
10+
{ path: "/store/chamaai", key: "chamaai" },
11+
{ path: "/store/chatwoot", key: "chatwoot" },
12+
{ path: "/store/proxy", key: "proxy" },
13+
{ path: "/store/rabbitmq", key: "rabbitmq" },
14+
{ path: "/store/settings", key: "settings" },
15+
{ path: "/store/typebot", key: "typebot" },
16+
{ path: "/store/webhook", key: "webhook" },
17+
{ path: "/store/websocket", key: "websocket" },
18+
]
19+
20+
const fs = require("fs")
21+
const path = require("path")
22+
23+
24+
module.exports = async (instanceName, options, progressBars, conn, connInstance) => {
25+
var files = []
26+
27+
FOLDER_PATH.forEach(folder => {
28+
files = files.concat(getFiles(instanceName, folder))
29+
})
30+
const progress = progressBars.create(files.length, 0)
31+
progress.update({ process: 'Instance' })
32+
for (const file of files) {
33+
const collectionName = file.key || instanceName
34+
const collection = (!file.secondaryConnection ? conn : connInstance).collection(collectionName)
35+
36+
const data = JSON.parse(fs.readFileSync(file.path, 'utf8'))
37+
data._id = file.path.split('\\').pop().split('.')[0]
38+
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
39+
progress.increment()
40+
}
41+
}
42+
43+
function getFiles(instanceName, opts) {
44+
var files = []
45+
const folder = opts.path || opts
46+
const folderPath = path.join(process.cwd(), folder)
47+
48+
fs.readdirSync(folderPath).forEach(file => {
49+
const filePath = path.join(folderPath, file)
50+
if (fs.statSync(filePath).isDirectory()) {
51+
files = files.concat(getFiles(instanceName, { ...opts, path: `${folder}/${file}` }))
52+
} else if (file.includes(instanceName) || opts.path.includes(instanceName)) {
53+
files.push({ ...opts, path: filePath })
54+
}
55+
})
56+
return files
57+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const FOLDER_PATH = [{
2+
path: "/store/message-up",
3+
key: "messageUpdate"
4+
}]
5+
6+
const fs = require("fs")
7+
const path = require("path")
8+
9+
10+
module.exports = async (instanceName, options, progressBars, conn) => {
11+
var files = []
12+
13+
FOLDER_PATH.forEach(folder => {
14+
files = files.concat(getFiles(instanceName, folder))
15+
})
16+
const progress = progressBars.create(files.length, 0)
17+
progress.update({ process: 'Message Update' })
18+
for (const file of files) {
19+
const collectionName = file.key
20+
const collection = conn.collection(collectionName)
21+
22+
const data = JSON.parse(fs.readFileSync(file.path, 'utf8'))
23+
data._id = file.path.split('\\').pop().split('.')[0]
24+
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
25+
26+
progress.increment()
27+
}
28+
}
29+
30+
function getFiles(instanceName, opts) {
31+
var files = []
32+
const folder = opts.path || opts
33+
const folderPath = path.join(process.cwd(), folder)
34+
35+
fs.readdirSync(folderPath).forEach(file => {
36+
const filePath = path.join(folderPath, file)
37+
if (fs.statSync(filePath).isDirectory()) {
38+
files = files.concat(getFiles(instanceName, { ...opts, path: `${folder}/${file}` }))
39+
} else if (file.includes(instanceName) || folder.includes(instanceName)) {
40+
files.push({ ...opts, path: filePath })
41+
}
42+
})
43+
return files
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const FOLDER_PATH = [{
2+
path: "/store/messages",
3+
key: "messages"
4+
}]
5+
6+
const fs = require("fs")
7+
const path = require("path")
8+
9+
10+
module.exports = async (instanceName, options, progressBars, conn) => {
11+
var files = []
12+
13+
FOLDER_PATH.forEach(folder => {
14+
files = files.concat(getFiles(instanceName, folder))
15+
})
16+
const progress = progressBars.create(files.length, 0)
17+
progress.update({ process: 'Messages' })
18+
for (const file of files) {
19+
const collectionName = file.key
20+
const collection = conn.collection(collectionName)
21+
22+
const data = JSON.parse(fs.readFileSync(file.path, 'utf8'))
23+
data._id = file.path.split('\\').pop().split('.')[0]
24+
await collection.findOneAndUpdate({ _id: data._id }, { $set: data }, { upsert: true })
25+
26+
progress.increment()
27+
}
28+
}
29+
30+
function getFiles(instanceName, opts) {
31+
var files = []
32+
const folder = opts.path || opts
33+
const folderPath = path.join(process.cwd(), folder)
34+
35+
fs.readdirSync(folderPath).forEach(file => {
36+
const filePath = path.join(folderPath, file)
37+
if (fs.statSync(filePath).isDirectory()) {
38+
files = files.concat(getFiles(instanceName, { ...opts, path: `${folder}/${file}` }))
39+
} else if (file.includes(instanceName) || folder.includes(instanceName)) {
40+
files.push({ ...opts, path: filePath })
41+
}
42+
})
43+
return files
44+
}

0 commit comments

Comments
 (0)