Skip to content

Build your personal knowledge base with Trilium Notes

feat(etapi): expose restoreAsRoot on the note import endpoint

Elian Doranc9e890f95ba1
diff --git a/apps/server/spec/etapi/import-zip.spec.ts b/apps/server/spec/etapi/import-zip.spec.ts
index 7c7adc4d16..f0f5d7b0ae 100644
--- a/apps/server/spec/etapi/import-zip.spec.ts
+++ b/apps/server/spec/etapi/import-zip.spec.ts
@@ -43,4 +43,21 @@ describe("etapi/import", () => {
expect(wrapper?.getChildNotes().map((n) => n.title)).toEqual(expect.arrayContaining(["Journal", "Trilium Demo", "Miscellaneous"]));
expect(becca.getBranchFromChildAndParent("root", "root")).toBeFalsy();
});
+
+ it("demo zip imports flat under root with ?restoreAsRoot=true (whole-database restore)", async () => {
+ const buffer = readFileSync(join(__dirname, "../../src/assets/db/demo.zip"));
+ const response = await supertest(app)
+ .post("/etapi/notes/root/import?restoreAsRoot=true")
+ .auth(USER, token, { "type": "basic"})
+ .set("Content-Type", "application/octet-stream")
+ .set("Content-Transfer-Encoding", "binary")
+ .send(buffer)
+ .expect(201);
+
+ // restoreAsRoot maps the archive's "root" onto the destination root, so its children land directly
+ // under root (no wrapper note) and no self-referential root->root branch is created.
+ expect(response.body.note.title).toStrictEqual("Journal");
+ expect(response.body.branch.parentNoteId).toStrictEqual("root");
+ expect(becca.getBranchFromChildAndParent("root", "root")).toBeFalsy();
+ });
});
diff --git a/apps/server/src/assets/etapi.openapi.yaml b/apps/server/src/assets/etapi.openapi.yaml
index 99e5a98c70..5685e8f4c6 100644
--- a/apps/server/src/assets/etapi.openapi.yaml
+++ b/apps/server/src/assets/etapi.openapi.yaml
@@ -294,6 +294,16 @@ paths:
required: true
schema:
$ref: "#/components/schemas/EntityId"
+ - name: restoreAsRoot
+ in: query
+ required: false
+ description: >-
+ When importing a whole-database ZIP export (one whose top note is the database root), map
+ the archive's "root" note onto {noteId} instead of nesting it under a redundant "root"
+ wrapper note. Typically used with noteId "root" to restore an entire database.
+ schema:
+ type: boolean
+ default: false
post:
description: Imports ZIP file into a given note.
operationId: importZip
diff --git a/apps/server/src/etapi/notes.ts b/apps/server/src/etapi/notes.ts
index 1dec2c0895..731e4de40b 100644
--- a/apps/server/src/etapi/notes.ts
+++ b/apps/server/src/etapi/notes.ts
@@ -173,7 +173,11 @@ function register(router: Router) {
const note = eu.getAndCheckNote(req.params.noteId);
const taskContext = new TaskContext("no-progress-reporting", "importNotes", null);
- zipImportService.importZip(taskContext, req.body, note).then((importedNote) => {
+ // Whole-database restore: map the archive's "root" note onto the target note instead of nesting it
+ // in a redundant "root" wrapper. Use when importing a Trilium root export into "root".
+ const restoreAsRoot = req.query.restoreAsRoot === "true";
+
+ zipImportService.importZip(taskContext, req.body, note, { restoreAsRoot }).then((importedNote) => {
res.status(201).json({
note: mappers.mapNoteToPojo(importedNote),
branch: mappers.mapBranchToPojo(importedNote.getParentBranches()[0])