@tolinax/ayoune-cli 2026.6.0 → 2026.6.1
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/lib/commands/createDbCommand.js +45 -24
- package/package.json +1 -1
|
@@ -9,6 +9,16 @@ import { isCronDue, getNextRun, validateCron } from "../db/cronMatcher.js";
|
|
|
9
9
|
function generateId() {
|
|
10
10
|
return Math.random().toString(36).substring(2, 10) + Date.now().toString(36);
|
|
11
11
|
}
|
|
12
|
+
function getSourceUri(opts) {
|
|
13
|
+
const uri = opts.from || process.env.MONGO_CONNECTSTRING || process.env.MONGODB_URI;
|
|
14
|
+
if (!uri) {
|
|
15
|
+
cliError("No source database configured. Set MONGO_CONNECTSTRING env var or use --from <uri>", EXIT_MISUSE);
|
|
16
|
+
}
|
|
17
|
+
if (!uri.startsWith("mongodb")) {
|
|
18
|
+
cliError("Source URI must be a MongoDB URI (mongodb:// or mongodb+srv://)", EXIT_MISUSE);
|
|
19
|
+
}
|
|
20
|
+
return uri;
|
|
21
|
+
}
|
|
12
22
|
function formatBytes(bytes) {
|
|
13
23
|
if (bytes === 0)
|
|
14
24
|
return "0 B";
|
|
@@ -21,16 +31,19 @@ export function createDbCommand(program) {
|
|
|
21
31
|
.command("db")
|
|
22
32
|
.description("MongoDB database operations (copy, stats, scheduled replication)")
|
|
23
33
|
.addHelpText("after", `
|
|
34
|
+
Source is automatically resolved from MONGO_CONNECTSTRING env var.
|
|
35
|
+
|
|
24
36
|
Examples:
|
|
25
|
-
ay db copy --
|
|
26
|
-
ay db copy --
|
|
37
|
+
ay db copy --to "mongodb://backup-host/ayoune" --collections "users,orders"
|
|
38
|
+
ay db copy --to "mongodb://..." --all --schedule "0 */6 * * *"
|
|
39
|
+
ay db copy --from "mongodb://other/db" --to "mongodb://..." --collections "logs"
|
|
27
40
|
ay db schedules list
|
|
28
|
-
ay db stats
|
|
41
|
+
ay db stats`);
|
|
29
42
|
// ─── ay db copy ─────────────────────────────────────────────────
|
|
30
43
|
db.command("copy")
|
|
31
|
-
.description("Copy data
|
|
32
|
-
.requiredOption("--from <uri>", "Source MongoDB connection URI")
|
|
44
|
+
.description("Copy data from aYOUne database to another MongoDB instance")
|
|
33
45
|
.requiredOption("--to <uri>", "Target MongoDB connection URI")
|
|
46
|
+
.option("--from <uri>", "Source URI override (default: MONGO_CONNECTSTRING env var)")
|
|
34
47
|
.option("--collections <list>", "Comma-separated collection names")
|
|
35
48
|
.option("--all", "Copy all collections")
|
|
36
49
|
.option("--query <json>", "JSON filter query for source documents")
|
|
@@ -39,11 +52,14 @@ Examples:
|
|
|
39
52
|
.option("--batch-size <number>", "Documents per batch", parseInt, 1000)
|
|
40
53
|
.option("--schedule <cron>", "Save as scheduled copy (cron expression) instead of executing")
|
|
41
54
|
.addHelpText("after", `
|
|
55
|
+
Source defaults to MONGO_CONNECTSTRING env var (the aYOUne database).
|
|
56
|
+
|
|
42
57
|
Examples:
|
|
43
|
-
ay db copy --
|
|
44
|
-
ay db copy --
|
|
45
|
-
ay db copy --
|
|
46
|
-
ay db copy --
|
|
58
|
+
ay db copy --to "mongodb://backup-host/ayoune" --collections "users,orders"
|
|
59
|
+
ay db copy --to "mongodb://..." --all --drop
|
|
60
|
+
ay db copy --to "mongodb://..." --collections "logs" --query '{"status":"active"}'
|
|
61
|
+
ay db copy --to "mongodb://..." --all --upsert --schedule "0 */6 * * *"
|
|
62
|
+
ay db copy --from "mongodb://other/db" --to "mongodb://..." --collections "data"`)
|
|
47
63
|
.action(async (options) => {
|
|
48
64
|
try {
|
|
49
65
|
const opts = { ...program.opts(), ...options };
|
|
@@ -54,9 +70,7 @@ Examples:
|
|
|
54
70
|
if (opts.collections && opts.all) {
|
|
55
71
|
cliError("Use either --collections or --all, not both", EXIT_MISUSE);
|
|
56
72
|
}
|
|
57
|
-
|
|
58
|
-
cliError("--from must be a MongoDB URI (mongodb:// or mongodb+srv://)", EXIT_MISUSE);
|
|
59
|
-
}
|
|
73
|
+
const fromUri = getSourceUri(opts);
|
|
60
74
|
if (!opts.to.startsWith("mongodb")) {
|
|
61
75
|
cliError("--to must be a MongoDB URI (mongodb:// or mongodb+srv://)", EXIT_MISUSE);
|
|
62
76
|
}
|
|
@@ -78,9 +92,9 @@ Examples:
|
|
|
78
92
|
const config = {
|
|
79
93
|
id: generateId(),
|
|
80
94
|
createdAt: new Date().toISOString(),
|
|
81
|
-
from: maskUri(
|
|
95
|
+
from: maskUri(fromUri),
|
|
82
96
|
to: maskUri(opts.to),
|
|
83
|
-
fromUri:
|
|
97
|
+
fromUri: fromUri, // Will be encrypted by addCopyConfig
|
|
84
98
|
toUri: opts.to,
|
|
85
99
|
collections,
|
|
86
100
|
query,
|
|
@@ -106,8 +120,8 @@ Examples:
|
|
|
106
120
|
return;
|
|
107
121
|
}
|
|
108
122
|
// Execute copy
|
|
109
|
-
spinner.start({ text: `Copying from ${maskUri(
|
|
110
|
-
const summary = await executeCopy(
|
|
123
|
+
spinner.start({ text: `Copying from ${maskUri(fromUri)} to ${maskUri(opts.to)}...`, color: "cyan" });
|
|
124
|
+
const summary = await executeCopy(fromUri, opts.to, collections, {
|
|
111
125
|
query,
|
|
112
126
|
drop: opts.drop,
|
|
113
127
|
upsert: opts.upsert,
|
|
@@ -229,21 +243,28 @@ Examples:
|
|
|
229
243
|
cliError(e.message || "Scheduled run failed", EXIT_GENERAL_ERROR);
|
|
230
244
|
}
|
|
231
245
|
});
|
|
232
|
-
// ─── ay db stats
|
|
233
|
-
db.command("stats
|
|
234
|
-
.description("Show database statistics (
|
|
246
|
+
// ─── ay db stats [uri] ──────────────────────────────────────────
|
|
247
|
+
db.command("stats [uri]")
|
|
248
|
+
.description("Show database statistics (defaults to aYOUne database)")
|
|
235
249
|
.addHelpText("after", `
|
|
250
|
+
Defaults to MONGO_CONNECTSTRING (the aYOUne database) when no URI is given.
|
|
251
|
+
|
|
236
252
|
Examples:
|
|
237
|
-
ay db stats
|
|
238
|
-
ay db stats "mongodb://
|
|
253
|
+
ay db stats Stats for aYOUne database
|
|
254
|
+
ay db stats "mongodb://other/db" Stats for a different database
|
|
255
|
+
ay db stats -r table`)
|
|
239
256
|
.action(async (uri, options) => {
|
|
240
257
|
try {
|
|
241
258
|
const opts = { ...program.opts(), ...options };
|
|
242
|
-
|
|
259
|
+
const dbUri = uri || process.env.MONGO_CONNECTSTRING || process.env.MONGODB_URI;
|
|
260
|
+
if (!dbUri) {
|
|
261
|
+
cliError("No database URI. Set MONGO_CONNECTSTRING env var or pass a URI argument.", EXIT_MISUSE);
|
|
262
|
+
}
|
|
263
|
+
if (!dbUri.startsWith("mongodb")) {
|
|
243
264
|
cliError("URI must start with mongodb:// or mongodb+srv://", EXIT_MISUSE);
|
|
244
265
|
}
|
|
245
|
-
spinner.start({ text: `Connecting to ${maskUri(
|
|
246
|
-
const stats = await getDbStats(
|
|
266
|
+
spinner.start({ text: `Connecting to ${maskUri(dbUri)}...`, color: "cyan" });
|
|
267
|
+
const stats = await getDbStats(dbUri);
|
|
247
268
|
spinner.stop();
|
|
248
269
|
const wrapped = {
|
|
249
270
|
payload: stats,
|