Skip to content

Commit

Permalink
feat: updateFile/updateFolder to allow moving
Browse files Browse the repository at this point in the history
Release-As: 0.1.0
  • Loading branch information
nzakas committed Jun 19, 2024
1 parent af5cb77 commit 5cd1267
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/object-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class Folder extends Entry {
add(entry) {
this.#entries.push(entry);
this.modifiedAt = new Date();
entry.modifiedAt = new Date(this.modifiedAt);
}

/**
Expand All @@ -232,6 +233,7 @@ class Folder extends Entry {
remove(entry) {
this.#entries = this.#entries.filter(e => e.id !== entry.id);
this.modifiedAt = new Date();
entry.modifiedAt = new Date(this.modifiedAt);
}

/**
Expand Down Expand Up @@ -383,11 +385,12 @@ export class ObjectStore {
* @param {Object} options The options to update the file.
* @param {string} [options.name] The new name of the file.
* @param {string|ArrayBuffer|undefined} [options.content] The new content of the file.
* @param {string} [options.parentId] The ID of the new parent folder.
* @returns {Object} The updated file record.
* @throws {Error} If the file is not found.
* @throws {Error} If the file is not a file.
*/
updateFile(id, { name, content }) {
updateFile(id, { name, content, parentId }) {
const file = this.#objects.get(id);

if (!file) {
Expand All @@ -406,6 +409,21 @@ export class ObjectStore {
/** @type {File} */ (file).content = content;
}

if (parentId) {
const newParent = this.#objects.get(parentId);

if (!newParent) {
throw new TypeError(`Parent "${parentId}" not found.`);
}

if (newParent.type !== "folder") {
throw new TypeError(`Parent "${parentId}" not a folder.`);
}

this.#parents.get(file.id).remove(file);
this.#register(file, /** @type {Folder} */ (newParent));
}

return file.toJSON();
}

Expand Down Expand Up @@ -484,9 +502,10 @@ export class ObjectStore {
* @param {string} id The ID of the folder to update.
* @param {Object} options The options to update the folder.
* @param {string} [options.name] The new name of the folder.
* @param {string} [options.parentId] The ID of the new parent folder.
* @returns {Object} The updated folder record.
*/
updateFolder(id, { name }) {
updateFolder(id, { name, parentId }) {
const folder = this.#objects.get(id);

if (!folder) {
Expand All @@ -501,6 +520,21 @@ export class ObjectStore {
folder.name = name;
}

if (parentId) {
const newParent = this.#objects.get(parentId);

if (!newParent) {
throw new TypeError(`Parent "${parentId}" not found.`);
}

if (newParent.type !== "folder") {
throw new TypeError(`Parent "${parentId}" not a folder.`);
}

this.#parents.get(folder.id).remove(folder);
this.#register(folder, /** @type {Folder} */ (newParent));
}

return folder.toJSON();
}

Expand Down
44 changes: 44 additions & 0 deletions tests/object-store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,28 @@ describe("ObjectStore", () => {

});

it("should update a file's parent folder", done => {
const folder1 = store.createFolder("foo");
const folder2 = store.createFolder("bar");
const file = store.createFile("baz.txt", { parentId: folder1.id });

setTimeout(() => {
store.updateFile(file.id, { parentId: folder2.id });
const updatedFile = store.getFile(file.id);

assert.strictEqual(updatedFile.parent_id, folder2.id);
assert.notStrictEqual(updatedFile.created_at, updatedFile.modified_at);
assert.notStrictEqual(file.modified_at, updatedFile.modified_at);

const updatedFolder1 = store.getFolder(folder1.id);
assert.strictEqual(updatedFolder1.entries.length, 0);

const updatedFolder2 = store.getFolder(folder2.id);
assert.strictEqual(updatedFolder2.entries.length, 1);
done();
}, 0);

});

it("should throw an error when the file doesn't exist", () => {
assert.throws(() => {
Expand Down Expand Up @@ -317,6 +338,29 @@ describe("ObjectStore", () => {
}, 0);
});

it("should update a folder's parent folder", done => {
const folder1 = store.createFolder("foo");
const folder2 = store.createFolder("bar");
const subfolder = store.createFolder("baz", { parentId: folder1.id });

setTimeout(() => {
store.updateFolder(subfolder.id, { parentId: folder2.id });
const updatedFolder = store.getFolder(subfolder.id);

assert.strictEqual(updatedFolder.parent_id, folder2.id);
assert.notStrictEqual(updatedFolder.created_at, updatedFolder.modified_at);
assert.notStrictEqual(subfolder.modified_at, updatedFolder.modified_at);

const updatedFolder1 = store.getFolder(folder1.id);
assert.strictEqual(updatedFolder1.entries.length, 0);

const updatedFolder2 = store.getFolder(folder2.id);
assert.strictEqual(updatedFolder2.entries.length, 1);
done();
}, 0);

});

it("should throw an error when the folder doesn't exist", () => {
assert.throws(() => {
store.updateFolder("123", { name: "foo" });
Expand Down

0 comments on commit 5cd1267

Please sign in to comment.