This repository was archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·121 lines (110 loc) · 3.51 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
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
#!/usr/bin/env node
const fs = require("fs");
const { MongoClient } = require("mongodb");
const { QuickDB } = require("quick.db");
function fatal(msg, aditionalError = null) {
console.log(msg);
if (aditionalError) console.error(aditionalError);
process.exit(1);
}
async function main() {
const inquirer = (await import("inquirer")).default;
const answers = await inquirer.prompt([
{
type: "list",
name: "choice",
message: "How do you want to connect to mongodb?",
choices: [
{ name: "ip - port - user - password - database", value: 0 },
{ name: "Mongodb url string", value: 1 },
],
},
{
type: "input",
name: "url",
message: "What is the mongodb connection string?",
when: (answers) => {
return answers.choice == 1;
},
},
{
type: "input",
name: "ip",
message: "What is the server ip?",
when: (answers) => {
return answers.choice == 0;
},
},
{
type: "input",
name: "port",
message: "What is the server port?",
when: (answers) => {
return answers.choice == 0;
},
},
{
type: "input",
name: "user",
message: "What is the user?",
when: (answers) => {
return answers.choice == 0;
},
},
{
type: "input",
name: "password",
message: "What is the password?",
when: (answers) => {
return answers.choice == 0;
},
},
{
type: "input",
name: "database",
message: "What is the database to use?",
when: (answers) => {
return answers.choice == 0;
},
},
{
type: "input",
name: "file",
message: "Where should the sqlite database be saved?",
},
]);
if (answers.choice == 0) {
answers.url = `mongodb://${answers.user}:${answers.password}@${answers.ip}:${answers.port}/${answers.database}`;
}
if (fs.existsSync(answers.file)) {
fatal(`file: ${answers.file}: already exist - not overwritting`);
}
const db = new QuickDB({ filePath: answers.file });
const mongoClient = new MongoClient(answers.url);
let connected = true;
const e = await mongoClient
.connect()
.catch((e) => ((connected = false), e));
if (!connected) {
fatal(`Couldn't connec to ${answers.url}`, e);
}
const dbConn = mongoClient.db();
const collections = await dbConn.listCollections().toArray();
// Process all collections
console.log("Processing all collections");
for (const collection of collections) {
if (collection.type != "collection") continue;
console.log(`Processing collection: ${collection.name}`);
const colConn = await dbConn.collection(collection.name);
const colName = collection.name == "jsons" ? "json" : collection.name;
const entries = await colConn.find({}).toArray();
const table = db.table(colName);
for (const entry of entries) {
console.log(`Processing entry: ${entry.ID}`);
await table.set(entry.ID, entry.data);
}
}
await mongoClient.close();
console.log("Done");
}
main();