@thingd/cli 0.80.0 → 0.81.0

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAeA,OAAO,EAYL,MAAM,EACN,KAAK,YAAY,EAClB,MAAM,aAAa,CAAC;AASrB,KAAK,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAEjD,KAAK,YAAY,GAAG;IAClB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B,CAAC;AAEF,KAAK,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC;AAE1C,MAAM,MAAM,aAAa,GAAG;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,KAAK,CAAC,EAAE,YAAY,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAC7B,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,UAAU,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,YAAY,CAAC;IACrB,MAAM,EAAE,YAAY,CAAC;IACrB,KAAK,EAAE,YAAY,CAAC;IACpB,MAAM,EAAE,OAAO,CAAC;CACjB,CAAC;AAsCF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,O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AsB,MAAM,CAC1B,IAAI,WAAwB,EAC5B,OAAO,GAAE,aAAkB,GAC1B,OAAO,CAAC,MAAM,CAAC,CA8EjB;AAwxCD,wBAAsB,MAAM,CAC1B,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,GACtC,OAAO,CAAC,IAAI,CAAC,CAgBf;AA4BD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,UAAU,GAAG,iBAAiB,CAuDxE;AAiDD,wBAAgB,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAEjE;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAE/E;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,SAAS,CAGlF;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAMrE;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEnF;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAMrF;AAyFD,wBAAgB,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAEpF;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAElE"}
package/dist/index.js CHANGED
@@ -106,6 +106,8 @@ Usage:
106
106
  thingd cloud api-key create <project> <name>
107
107
  thingd db checkpoint [--path <path>]
108
108
  thingd db integrity [--path <path>]
109
+ thingd db backup --out <archive.tar> [--path <path>]
110
+ thingd db restore --in <archive.tar> --destination <path> [--replace]
109
111
  thingd db reencrypt --source <path> --destination <path> [--allow-plaintext-output]
110
112
 
111
113
  Options:
@@ -329,11 +331,19 @@ async function runCommand(context) {
329
331
  await runDbIntegrity(context);
330
332
  return;
331
333
  }
334
+ if (sub === "backup") {
335
+ await runDbNativeBackup(context);
336
+ return;
337
+ }
338
+ if (sub === "restore") {
339
+ await runDbNativeRestore(context);
340
+ return;
341
+ }
332
342
  if (sub === "reencrypt") {
333
343
  await runDbReencrypt(context);
334
344
  return;
335
345
  }
336
- throw new Error(`Unknown db subcommand: ${sub}. Expected: checkpoint, integrity, reencrypt`);
346
+ throw new Error(`Unknown db subcommand: ${sub}. Expected: checkpoint, integrity, backup, restore, reencrypt`);
337
347
  }
338
348
  throw new Error(`Unknown command: ${command}`);
339
349
  }
@@ -427,6 +437,40 @@ async function runDbIntegrity(context) {
427
437
  }
428
438
  });
429
439
  }
440
+ async function runDbNativeBackup(context) {
441
+ const output = stringFlag(context.parsed, "out");
442
+ if (!output) {
443
+ throw new Error("db backup requires --out <archive.tar>");
444
+ }
445
+ const connection = resolveConnection(context);
446
+ if (connection.cloud || connection.driver !== "native") {
447
+ throw new Error("db backup requires a local native database; logical snapshots support remote stores");
448
+ }
449
+ const db = await ThingD.open({
450
+ path: connection.path,
451
+ driver: "native",
452
+ encryption: connection.encryptionKey ? { key: connection.encryptionKey } : undefined,
453
+ });
454
+ try {
455
+ db.walCheckpoint();
456
+ }
457
+ finally {
458
+ await db.close();
459
+ }
460
+ const { createNativeArchive } = await import("./native-archive.js");
461
+ await createNativeArchive(connection.path, output);
462
+ writeJson(context.stdout, { ok: true, out: resolve(output), format: "thingd-native-tar" }, context.pretty);
463
+ }
464
+ async function runDbNativeRestore(context) {
465
+ const input = stringFlag(context.parsed, "in");
466
+ const destination = stringFlag(context.parsed, "destination");
467
+ if (!input || !destination) {
468
+ throw new Error("db restore requires --in <archive.tar> and --destination <path>");
469
+ }
470
+ const { restoreNativeArchive } = await import("./native-archive.js");
471
+ await restoreNativeArchive(input, destination, hasFlag(context.parsed, "replace"));
472
+ writeJson(context.stdout, { ok: true, destination: resolve(destination), format: "thingd-native-tar" }, context.pretty);
473
+ }
430
474
  async function runCompletions(context) {
431
475
  const shell = optionalToken(context.parsed, 1) ?? "bash";
432
476
  const cmds = [
@@ -0,0 +1,3 @@
1
+ export declare function createNativeArchive(sourcePath: string, outputPath: string): Promise<void>;
2
+ export declare function restoreNativeArchive(archivePath: string, destinationPath: string, replace: boolean): Promise<void>;
3
+ //# sourceMappingURL=native-archive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native-archive.d.ts","sourceRoot":"","sources":["../src/native-archive.ts"],"names":[],"mappings":"AA0DA,wBAAsB,mBAAmB,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAoC/F;AAED,wBAAsB,oBAAoB,CACxC,WAAW,EAAE,MAAM,EACnB,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,IAAI,CAAC,CAkEf"}
@@ -0,0 +1,134 @@
1
+ import { execFile, execFileSync } from "node:child_process";
2
+ import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { basename, dirname, isAbsolute, join, relative, resolve } from "node:path";
5
+ import { promisify } from "node:util";
6
+ const execFileAsync = promisify(execFile);
7
+ const METADATA_NAME = "thingd-backup.json";
8
+ async function runTar(args) {
9
+ try {
10
+ await execFileAsync("tar", args, { maxBuffer: 16 * 1024 * 1024 });
11
+ }
12
+ catch (error) {
13
+ const detail = error instanceof Error ? error.message : String(error);
14
+ throw new Error(`tar operation failed: ${detail}`);
15
+ }
16
+ }
17
+ function assertDirectory(path, label) {
18
+ if (!existsSync(path) || !lstatSync(path).isDirectory()) {
19
+ throw new Error(`${label} must be an existing directory: ${path}`);
20
+ }
21
+ }
22
+ function assertArchivePathSafe(path) {
23
+ if (path.startsWith("/") || path.startsWith("\\") || /^[A-Za-z]:[\\/]/.test(path)) {
24
+ throw new Error(`Unsafe archive path: ${path}`);
25
+ }
26
+ const normalized = path.replaceAll("\\", "/");
27
+ if (normalized.split("/").includes("..")) {
28
+ throw new Error(`Unsafe archive path: ${path}`);
29
+ }
30
+ }
31
+ function archiveEntries(archive) {
32
+ const result = execFileSync("tar", ["-tf", archive], {
33
+ encoding: "utf8",
34
+ maxBuffer: 16 * 1024 * 1024,
35
+ });
36
+ return result.split(/\r?\n/).filter(Boolean);
37
+ }
38
+ export async function createNativeArchive(sourcePath, outputPath) {
39
+ const source = resolve(sourcePath);
40
+ const output = resolve(outputPath);
41
+ assertDirectory(source, "Native database");
42
+ const outputRelative = relative(source, output);
43
+ if (outputRelative && !outputRelative.startsWith("..") && !isAbsolute(outputRelative)) {
44
+ throw new Error("Backup archive must be outside the source database directory");
45
+ }
46
+ if (existsSync(output)) {
47
+ throw new Error(`Backup destination already exists: ${output}`);
48
+ }
49
+ mkdirSync(dirname(output), { recursive: true });
50
+ const staging = mkdtempSync(join(tmpdir(), "thingd-backup-"));
51
+ const metadata = {
52
+ format: "thingd-native-tar",
53
+ version: 1,
54
+ sourceName: basename(source),
55
+ createdAt: new Date().toISOString(),
56
+ };
57
+ const metadataPath = join(staging, METADATA_NAME);
58
+ writeFileSync(metadataPath, `${JSON.stringify(metadata, null, 2)}\n`, "utf8");
59
+ try {
60
+ await runTar([
61
+ "-cf",
62
+ output,
63
+ "-C",
64
+ dirname(source),
65
+ basename(source),
66
+ "-C",
67
+ staging,
68
+ METADATA_NAME,
69
+ ]);
70
+ }
71
+ finally {
72
+ rmSync(staging, { recursive: true, force: true });
73
+ }
74
+ }
75
+ export async function restoreNativeArchive(archivePath, destinationPath, replace) {
76
+ const archive = resolve(archivePath);
77
+ const destination = resolve(destinationPath);
78
+ if (!existsSync(archive) || !lstatSync(archive).isFile()) {
79
+ throw new Error(`Backup archive not found: ${archive}`);
80
+ }
81
+ if (existsSync(destination) && !replace) {
82
+ throw new Error(`Restore destination already exists: ${destination}; pass --replace to replace it`);
83
+ }
84
+ mkdirSync(dirname(destination), { recursive: true });
85
+ const entries = archiveEntries(archive);
86
+ for (const entry of entries) {
87
+ assertArchivePathSafe(entry);
88
+ }
89
+ if (!entries.includes(METADATA_NAME)) {
90
+ throw new Error(`Invalid Thingd archive: missing ${METADATA_NAME}`);
91
+ }
92
+ const roots = new Set(entries
93
+ .filter((entry) => entry !== METADATA_NAME)
94
+ .map((entry) => entry.split("/")[0])
95
+ .filter(Boolean));
96
+ if (roots.size !== 1) {
97
+ throw new Error("Invalid Thingd archive: expected exactly one database directory");
98
+ }
99
+ const stagingParent = mkdtempSync(join(dirname(destination), ".thingd-restore-"));
100
+ const root = [...roots][0];
101
+ if (!root) {
102
+ throw new Error("Invalid Thingd archive: missing database directory");
103
+ }
104
+ const staging = join(stagingParent, root);
105
+ let previous;
106
+ try {
107
+ await runTar(["-xf", archive, "-C", stagingParent]);
108
+ const metadata = JSON.parse(readFileSync(join(stagingParent, METADATA_NAME), "utf8"));
109
+ if (metadata.format !== "thingd-native-tar" || metadata.version !== 1) {
110
+ throw new Error("Invalid Thingd archive metadata");
111
+ }
112
+ assertDirectory(staging, "Restored native database");
113
+ if (!existsSync(join(staging, "lock")) || !existsSync(join(staging, "keyspaces"))) {
114
+ throw new Error("Invalid Thingd archive: missing lock or keyspaces directory");
115
+ }
116
+ if (replace && existsSync(destination)) {
117
+ previous = `${destination}.previous-${Date.now()}`;
118
+ renameSync(destination, previous);
119
+ }
120
+ renameSync(staging, destination);
121
+ if (previous) {
122
+ rmSync(previous, { recursive: true, force: true });
123
+ }
124
+ }
125
+ catch (error) {
126
+ if (previous && !existsSync(destination) && existsSync(previous)) {
127
+ renameSync(previous, destination);
128
+ }
129
+ throw error;
130
+ }
131
+ finally {
132
+ rmSync(stagingParent, { recursive: true, force: true });
133
+ }
134
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thingd/cli",
3
- "version": "0.80.0",
3
+ "version": "0.81.0",
4
4
  "description": "CLI, Interactive TUI Dashboard, and MCP server for thingd — a fast object-first data engine for applications and AI agents.",
5
5
  "type": "module",
6
6
  "homepage": "https://engine.thingd.cloud",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "@modelcontextprotocol/sdk": "^1.30.0",
45
- "@thingd/sdk": "^0.80.0",
45
+ "@thingd/sdk": "^0.81.0",
46
46
  "cli-table3": "^0.6.5",
47
47
  "picocolors": "^1.1.1",
48
48
  "zod": "^4.4.3"