Skip to content
This repository was archived by the owner on Oct 27, 2023. It is now read-only.

Commit f2f1c9f

Browse files
committed
First commit
0 parents  commit f2f1c9f

File tree

10 files changed

+6263
-0
lines changed

10 files changed

+6263
-0
lines changed

.eslintrc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
"env": {
3+
"commonjs": true,
4+
"es2021": true,
5+
"node": true
6+
},
7+
"extends": "eslint:recommended",
8+
"overrides": [
9+
],
10+
"parserOptions": {
11+
"ecmaVersion": "latest"
12+
},
13+
"rules": {
14+
}
15+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Plexi Development
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Install
2+
3+
```
4+
npm i -g quick.db-mongodb-migration-tool
5+
```
6+
7+
# Usage
8+
9+
(in a terminal)
10+
11+
```
12+
migration-tool
13+
```

eslintrc.json

Whitespace-only changes.

index.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/usr/bin/env node
2+
const fs = require("fs");
3+
const { MongoClient } = require("mongodb");
4+
const { QuickDB } = require("quick.db");
5+
6+
function fatal(msg, aditionalError = null) {
7+
console.log(msg);
8+
if (aditionalError) console.error(aditionalError);
9+
process.exit(1);
10+
}
11+
12+
async function main() {
13+
const inquirer = (await import("inquirer")).default;
14+
const answers = await inquirer.prompt([
15+
{
16+
type: "list",
17+
name: "choice",
18+
message: "How do you want to connect to mongodb?",
19+
choices: [
20+
{ name: "ip - port - user - password - database", value: 0 },
21+
{ name: "Mongodb url string", value: 1 },
22+
],
23+
},
24+
{
25+
type: "input",
26+
name: "url",
27+
message: "What is the mongodb connection string?",
28+
when: (answers) => {
29+
return answers.choice == 1;
30+
},
31+
},
32+
{
33+
type: "input",
34+
name: "ip",
35+
message: "What is the server ip?",
36+
when: (answers) => {
37+
return answers.choice == 0;
38+
},
39+
},
40+
{
41+
type: "input",
42+
name: "port",
43+
message: "What is the server port?",
44+
when: (answers) => {
45+
return answers.choice == 0;
46+
},
47+
},
48+
{
49+
type: "input",
50+
name: "user",
51+
message: "What is the user?",
52+
when: (answers) => {
53+
return answers.choice == 0;
54+
},
55+
},
56+
{
57+
type: "input",
58+
name: "password",
59+
message: "What is the password?",
60+
when: (answers) => {
61+
return answers.choice == 0;
62+
},
63+
},
64+
{
65+
type: "input",
66+
name: "database",
67+
message: "What is the database to use?",
68+
when: (answers) => {
69+
return answers.choice == 0;
70+
},
71+
},
72+
{
73+
type: "input",
74+
name: "file",
75+
message: "Where should the sqlite database be saved?",
76+
},
77+
]);
78+
79+
if (answers.choice == 0) {
80+
answers.url = `mongodb://${answers.user}:${answers.password}@${answers.ip}:${answers.port}/${answers.database}`;
81+
}
82+
83+
if (fs.existsSync(answers.file)) {
84+
fatal(`file: ${answers.file}: already exist - not overwritting`);
85+
}
86+
87+
const db = new QuickDB({ filePath: answers.file });
88+
89+
const mongoClient = new MongoClient(answers.url);
90+
let connected = true;
91+
const e = await mongoClient
92+
.connect()
93+
.catch((e) => ((connected = false), e));
94+
if (!connected) {
95+
fatal(`Couldn't connec to ${answers.url}`, e);
96+
}
97+
98+
const dbConn = mongoClient.db();
99+
const collections = await dbConn.listCollections().toArray();
100+
101+
// Process all collections
102+
console.log("Processing all collections");
103+
for (const collection of collections) {
104+
if (collection.type != "collection") continue;
105+
console.log(`Processing collection: ${collection.name}`);
106+
const colConn = await dbConn.collection(collection.name);
107+
const colName = collection.name == "jsons" ? "json" : collection.name;
108+
const entries = await colConn.find({}).toArray();
109+
const table = db.table(colName);
110+
111+
for (const entry of entries) {
112+
console.log(`Processing entry: ${entry.ID}`);
113+
await table.set(entry.ID, entry.data);
114+
}
115+
}
116+
117+
await mongoClient.close();
118+
console.log("Done");
119+
}
120+
121+
main();

0 commit comments

Comments
 (0)