π§± DBMS Module
1import { DBMS } from 'hbh-dbms';
β οΈ DBMS is NOT a constructor
It is a namespace that exposes multiple DB classes.
π§± Available DB Classes
1DBMS.BaseDB2DBMS.UserDB3DBMS.ProductDB4DBMS.ChannelDB5DBMS.ContentDB6DBMS.BaseDB7DBMS.UserFileManager
HBH-DBMS is designed to be used directly, just like this:
1const userDB = new DBMS.UserDB();2const productDB = new DBMS.ProductDB();3const channelDB = new DBMS.ChannelDB();4const contentDB = new DBMS.ContentDB();
This is the recommended and documented approach.
π§± BaseDB (Internal Engine)
1const base = new DBMS.BaseDB("custom/path");
Handles:
- Atomic JSON read/write
- Directory integrity
- Traversal protection
- Directory safety
- Atomic updates
β οΈ Normally used internally by all DB modules, but exposed for advanced users.
π€ UserDB
Manage user-related data.
Manages users as root entities.
1const userDB = new DBMS.UserDB();
β Create User
1await userDB.create(2 "Alice@example.com",3 {4 email: "Alice@example.com",5 Info: {6 Author: "Alice",7 password: "12345678"8 }9 }10);
π What happens internally:
- User directory created
Users/Ae/Alice@example.com/ data.jsonwritten- Duplicate IDs prevented
π Read User
1const user = await userDB.read("Alice@example.com");
βοΈ Rename User (Update ID)
1await userDB.update(2 "Alice@example.com",3 "AliceDemo@gmail.com"4);
β Renames folder β Keeps all nested data intact
βοΈ Update User Info
1await userDB.updateInfo(2 "AliceDemo@gmail.com",3 {4 id2: 123456,5 password: "1234@Alice"6 }7);
β Merges data β Does not overwrite existing keys
β Delete User
1await userDB.delete("AliceDemo@example.com");
Deletes:
- User
- Products
- Channels
- Content
- Files
π List Users
1const users = await userDB.list();
π¦ ProductDB
1const productDB = new DBMS.ProductDB('Alice@example.com');
Products are owned by users.
β Create Product
1await productDB.create("Product1");
π Path created:
/Users/Ae/Alice@example.com/Product1/
π Read Product
1await productDB.read("Product1");
βοΈ Rename Product
1await productDB.update("Product1","Product2");
β Delete Product
1await productDB.delete("Product2");
π List Products
1await productDB.list();
πΊ ChannelDB
Channels live inside products. Channels can contain content, posts, or media.
1const channelDB = new DBMS.ChannelDB('Alice@example.com', 'Product1');2channelDB.Limit = 2;
π Limit controls max channels inside product db.
β Create Channel
1await channelDB.create("Channel1");
π Path created:
1/Users/Ae/Alice@example.com/Product1/Channel1
π Read Channel
1await channelDB.read("Channel1");
βοΈ Rename Channel
1await channelDB.update("Channel1", "Channel2");
βοΈ Update Channel Info
1await channelDB.updateInfo(2 "Channel2",3 { author: "HashirAttari" }4);
π Find Channel
1await channelDB.find("Channel1");
π List Channels
1await channelDB.list();
β Delete Channel
1await channelDB.delete("Channel2");
π ContentDB
Used for all content, including blogs, posts, articles, and videos, in JSON format. Content exists inside channels.
1const contentDB = new DBMS.ContentDB(channelDB, 'Channel1');2contentDB.Limit = 2;
π Limit controls max content inside channel db.
β Create Content
1const contentID = await contentDB.create(2 type = "list" | 'tutorial',3 contentName = "First-Content",4 body = ["Hello", "User", "ALice"] | { Name: "Alice", message: 'Hello' }5);
π Path created:
1/Users/Ae/Alice@example.com/Product1/Channel1/${ContentType}/First-Content.json
π Read Content
1await contentDB.read(contentID);
βοΈ Update Content Body
1await contentDB.updateContent(2 contentID,3 ["Alice", "AliceDemo"]4);
βοΈ Update Content Info
1await contentDB.updateInfo(2 contentID,3 {4 tags: ["updated"],5 author: "New Author"6 }7);
β Delete Content
1await contentDB.delete(contentID);
π List Content
1await contentDB.list({type: 'list' | 'all'});
π Find Content
1await contentDB.find(contentID);
π UserFileManager
UserFileManager handles user file operations inside products, including uploads, downloads, renames, deletes, and recycle bin functionality.
1const ufm = new DBMS.UserFileManager("Alice@example.com");
π Works per user and integrates with UserDB and ProductDB.
π Files are isolated per product to maintain safety.
β Create / Upload File
1await ufm.upload(req, "Alice@example.com", "Product1", "rename");
-
Handles HTTP file uploads via
formidable. -
Supports collision modes:
'skip'β skip if file exists'overwrite'β replace existing'rename'β auto-rename new file
-
Validates quota (
maxBytes) before saving. -
Stores files in:
/Users/Ae/Alice@example.com/Product1/<fileName>
- Adds metadata and tracks usage in the productβs config.
π Read File
1const content = await ufm.readFileContent("Alice@example.com", "Product1", "myfile.txt");
- Returns text content only (binary files will throw an error).
βοΈ Update File
1await ufm.updateFileContent("Alice@example.com", "Product1", "myfile.txt", "Updated text");
- Replaces file content.
- Validates text content.
- Updates usage tracking.
β Delete File
1await ufm.deleteFile("Alice@example.com", "Product1", "myfile.txt");
- Deletes a file.
- Updates usage accordingly.
π¦ Delete All Files in Product
1await ufm.deleteAllFiles("Alice@example.com", "Product1");
- Deletes all files/folders inside a product.
- Skips .config folder.
- Resets usage in config.
π Rename / Move File
1await ufm.renameFile("Alice@example.com", "Product1", "old.txt", "new.txt");2await ufm.moveFile("Alice@example.com", "Product1", "old.txt", "folder/new.txt");
- Moves or renames files safely inside product directories.
- Validates safe paths.
π Create Folder
1await ufm.createFolder("Alice@example.com", "Product1", "newFolder");
- Creates a directory inside a product.
π Recycle Bin
- Move to Bin:
1await ufm.BinFile("Alice@example.com", "Product1", "file.txt");
- Recover File:
1await ufm.RecoverFile("Alice@example.com", "Product1", "file.txt.hbhbin");
- List Trash:
1const trashFiles = await ufm.ListTrash("Alice@example.com", "Product1");
- Empty Trash:
1await ufm.EmptyTrash("Alice@example.com", "Product1");
π List Files / Directories
1await ufm.listFlat("Alice@example.com", "Product1"); // flat list2await ufm.listTree("Alice@example.com", "Product1"); // tree structure3await ufm.listDir("Alice@example.com", "Product1"); // directories only
- Filters .config folders automatically.
- Returns full metadata for files in tree/flat listings.
π Usage Info
1const { used, limit } = await ufm.usage("Alice@example.com", "Product1");
usedβ total bytes usedlimitβ maximum allowed bytes (defaultMAX_USER_STORAGE_BYTES)
βοΈ Product Management
1await ufm.createProduct("Product1");2await ufm.deleteProduct("Product1");
- Creates a product with initial config.
- Deletes product safely, including files.