Change DB move API

This commit is contained in:
Andras Schmelczer 2025-02-25 20:38:20 +00:00
parent a5bcaec9fe
commit f6ee0b727b
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -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<void> {
this.documents.delete(oldRelativePath);
this.documents.set(relativePath, {
documentId,
parentVersionId,
hash
});
await this.save();
}
public async removeDocument(relativePath: RelativePath): Promise<void> {
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<void> {
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<void> {
await this.saveData({
documents: Object.fromEntries(this.documents.entries()),