Object Store
by Nicholas C. Zakas
If you find this useful, please consider supporting my work with a donation or nominate me for a GitHub Star.
Description
An implementation of an in-memory object store modeled on cloud drives like Google Drive. This is useful mostly for testing purposes.
Usage
Node.js
Install using [npm][npm] or [yarn][yarn]:
npm install @humanwhocodes/object-store
# or
yarn add @humanwhocodes/object-store
Import into your Node.js project:
const { ObjectStore } = require("@humanwhocodes/object-store");
import { ObjectStore } from "@humanwhocodes/object-store";
Deno
Install using JSR:
deno add @humanwhocodes/object-store
#or
jsr add @humanwhocodes/object-store
Then import into your Deno project:
import { ObjectStore } from "@humanwhocodes/object-store";
Bun
Install using this command:
bun add @humanwhocodes/object-store
Import into your Bun project:
import { ObjectStore } from "@humanwhocodes/object-store";
Browser
It's recommended to import the minified version to save bandwidth:
import { ObjectStore } from "https://cdn.skypack.dev/@humanwhocodes/object-store?min";
However, you can also import the unminified version for debugging purposes:
import { ObjectStore } from "https://cdn.skypack.dev/@humanwhocodes/object-store";
API
Creating Files
const store = new ObjectStore();
const file = store.createFile("foo.txt", { content: "Foo", parentId: "folder_id" });
console.log(file);
Note: When parentId is omitted, the root folder is used.
Copying Files
const store = new ObjectStore();
const file = store.createFile("foo.txt", { content: "Foo", parentId: "folder_id" });
const copiedFile = store.copyFile(file.id, { parentId: "some_other_folder_id", name: "bar.txt"});
console.log(copiedFile);
Note: name is optional. When parentId is not specified, the ID of the original's parent is used.
Moving/Renaming Files
const store = new ObjectStore();
const file = store.createFile("foo.txt", { content: "Foo", parentId: "folder_id" });
const updatedFile = store.updateFile(file.id, { parentId: "some_other_folder_id", name: "bar.txt"});
console.log(updatedFile);
Note: Both name and parentId are optional.
Retrieving Files
const store = new ObjectStore();
const file = store.createFile("foo.txt", { content: "Foo" });
const retrievedFile = store.getFile(file.id);
console.log(retrievedFile);
Retrieving File Content
const store = new ObjectStore();
const file = store.createFile("foo.txt", { content: "Foo" });
const content = store.getFileContent(file.id);
console.log(content);
Deleting Files
const store = new ObjectStore();
const file = store.createFile("foo.txt", { content: "Foo" });
store.deleteFile(file.id);
Creating Folders
const store = new ObjectStore();
const folder = store.createFolder("my-folder", { parentId: "parent_folder_id" });
console.log(folder);
Note: When parentId is omitted, the root folder is used.
Copying Folders
const store = new ObjectStore();
const folder = store.createFolder("my-folder", { parentId: "parent_folder_id" });
const copiedFolder = store.copyFolder(folder.id, { parentId: "some_other_folder_id", name: "my-folder-copy"});
console.log(copiedFolder);
Note: name is optional. When parentId is not specified, the ID of the original's parent is used.
Moving/Renaming Folders
const store = new ObjectStore();
const folder = store.createFolder("my-folder", { parentId: "parent_folder_id" });
const updatedFolder = store.updateFolder(file.id, { parentId: "some_other_folder_id", name: "my-new-name"});
console.log(updatedFolder);
Deleting Folders
const store = new ObjectStore();
const folder = store.createFolder("my-folder");
store.deleteFolder(folder.id);
Developer Setup
- Fork the repository
- Clone your fork
- Run
npm install to setup dependencies
- Run
npm test to run tests
License
Apache 2.0