From f6ee0b727bd1950fc518babe434af3d84883fc33 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Tue, 25 Feb 2025 20:38:20 +0000 Subject: [PATCH] Change DB move API --- .../sync-client/src/persistence/database.ts | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/frontend/sync-client/src/persistence/database.ts b/frontend/sync-client/src/persistence/database.ts index 4d36e867..495c9ccd 100644 --- a/frontend/sync-client/src/persistence/database.ts +++ b/frontend/sync-client/src/persistence/database.ts @@ -88,28 +88,6 @@ export class Database { await this.save(); } - public async moveDocument({ - documentId, - oldRelativePath, - relativePath, - parentVersionId, - hash - }: { - documentId: DocumentId; - oldRelativePath: RelativePath; - relativePath: RelativePath; - parentVersionId: VaultUpdateId; - hash: string; - }): Promise { - this.documents.delete(oldRelativePath); - this.documents.set(relativePath, { - documentId, - parentVersionId, - hash - }); - await this.save(); - } - public async removeDocument(relativePath: RelativePath): Promise { this.documents.delete(relativePath); await this.save(); @@ -121,6 +99,29 @@ export class Database { return this.documents.get(relativePath); } + public async updatePath( + oldRelativePath: RelativePath, + newRelativePath: RelativePath + ): Promise { + const document = this.documents.get(oldRelativePath); + if (!document) { + throw new Error( + `Cannot update physical path for document that does not exist: ${oldRelativePath}` + ); + } + + if (this.documents.has(newRelativePath)) { + throw new Error( + `Cannot update physical path to path that is already in use: ${newRelativePath}` + ); + } + + this.documents.delete(oldRelativePath); + this.documents.set(newRelativePath, document); + + await this.save(); + } + private async save(): Promise { await this.saveData({ documents: Object.fromEntries(this.documents.entries()),