|
| 1 | +# QuickDB - A Lightweight Database for Minecraft Bedrock ScriptAPI |
| 2 | + |
| 3 | +**QuickDB** is a simple and efficient database system designed for Minecraft Bedrock Edition ScriptAPI. It utilizes dynamic properties from the `@minecraft/server` module, allowing developers to store and manage key-value pairs in a way similar to JavaScript's `Map` object. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **CRUD Operations**: |
| 10 | + - `set(key, value)` - Save a value to the database. |
| 11 | + - `get(key)` - Retrieve a value by its key. |
| 12 | + - `delete(key)` - Remove a key-value pair from the database. |
| 13 | + - `has(key)` - Check if a key exists in the database. |
| 14 | + |
| 15 | +- **Iteration**: |
| 16 | + - `keys()` - Get all keys in the database. |
| 17 | + - `values()` - Retrieve all values stored in the database. |
| 18 | + - `entries()` - Retrieve all key-value pairs as an array of entries. |
| 19 | + |
| 20 | +- **Database Size**: |
| 21 | + - `size` - Get the total byte count used by the dynamic properties. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +Import `QuickDB` into your ScriptAPI project. Ensure `QuickDB` is included in your `index.js` file for easy integration. |
| 28 | + |
| 29 | +```javascript |
| 30 | +import QuickDB from "./index.js"; |
| 31 | +``` |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +## Documentation |
| 36 | + |
| 37 | +### **Constructor** |
| 38 | + |
| 39 | +```javascript |
| 40 | +const db = new QuickDB("user"); |
| 41 | +``` |
| 42 | + |
| 43 | +- **Parameters**: |
| 44 | + - `id` *(string)*: A unique identifier for your database instance. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +### **Methods** |
| 49 | + |
| 50 | +#### `set(key, value)` |
| 51 | +- **Description**: Stores a value associated with a key. |
| 52 | +- **Parameters**: |
| 53 | + - `key` *(string)*: The key to store the value. |
| 54 | + - `value` *(any)*: The value to be stored. |
| 55 | +- **Returns**: `boolean` - `true` if successful. |
| 56 | + |
| 57 | +```javascript |
| 58 | +db.set("FomoKiwor", { money: 20000 }); |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +#### `get(key)` |
| 64 | +- **Description**: Retrieves the value associated with a key. |
| 65 | +- **Parameters**: |
| 66 | + - `key` *(string)*: The key to retrieve the value. |
| 67 | +- **Returns**: `any` - The value associated with the key. |
| 68 | + |
| 69 | +```javascript |
| 70 | +const userData = db.get("FomoKiwor"); // { money: 20000 } |
| 71 | +``` |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +#### `has(key)` |
| 76 | +- **Description**: Checks if a key exists in the database. |
| 77 | +- **Parameters**: |
| 78 | + - `key` *(string)*: The key to check. |
| 79 | +- **Returns**: `boolean` - `true` if the key exists. |
| 80 | + |
| 81 | +```javascript |
| 82 | +const exists = db.has("FomoKiwor"); // true |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +#### `delete(key)` |
| 88 | +- **Description**: Removes a key-value pair from the database. |
| 89 | +- **Parameters**: |
| 90 | + - `key` *(string)*: The key to remove. |
| 91 | +- **Returns**: `boolean` - `true` if successful. |
| 92 | + |
| 93 | +```javascript |
| 94 | +db.delete("FomoKiwor"); |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +#### `keys()` |
| 100 | +- **Description**: Retrieves all keys in the database. |
| 101 | +- **Returns**: `string[]` - An array of all keys. |
| 102 | + |
| 103 | +```javascript |
| 104 | +const allKeys = db.keys(); // ["key1", "key2"] |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +#### `values()` |
| 110 | +- **Description**: Retrieves all values in the database. |
| 111 | +- **Returns**: `any[]` - An array of all values. |
| 112 | + |
| 113 | +```javascript |
| 114 | +const allValues = db.values(); // [{ money: 20000 }, { items: [] }] |
| 115 | +``` |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +#### `entries()` |
| 120 | +- **Description**: Retrieves all key-value pairs in the database. |
| 121 | +- **Returns**: `Array<[string, any]>` - An array of key-value entries. |
| 122 | + |
| 123 | +```javascript |
| 124 | +const allEntries = db.entries(); // [["key1", { money: 20000 }], ["key2", { items: [] }]] |
| 125 | +``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +### **Property** |
| 130 | + |
| 131 | +#### `size` |
| 132 | +- **Description**: Gets the total byte count used by dynamic properties. |
| 133 | +- **Returns**: `number` - The total byte count. |
| 134 | + |
| 135 | +```javascript |
| 136 | +console.log(db.size); // e.g., 512 |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## Example Usage |
| 142 | + |
| 143 | +```javascript |
| 144 | +import QuickDB from "./index.js"; |
| 145 | + |
| 146 | +const db = new QuickDB("user"); |
| 147 | + |
| 148 | +db.set("FomoKiwor", { money: 20000 }); |
| 149 | +console.log(db.get("FomoKiwor")); // { money: 20000 } |
| 150 | + |
| 151 | +console.log(db.has("FomoKiwor")); // true |
| 152 | +db.delete("FomoKiwor"); |
| 153 | +console.log(db.has("FomoKiwor")); // false |
| 154 | + |
| 155 | +db.set("User1", { score: 100 }); |
| 156 | +db.set("User2", { score: 150 }); |
| 157 | + |
| 158 | +console.log(db.keys()); // ["User1", "User2"] |
| 159 | +console.log(db.values()); // [{ score: 100 }, { score: 150 }] |
| 160 | +console.log(db.entries()); // [["User1", { score: 100 }], ["User2", { score: 150 }]] |
| 161 | +``` |
| 162 | + |
| 163 | +--- |
| 164 | + |
| 165 | +## License |
| 166 | + |
| 167 | +MIT License |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +Developed by [Nperma](https://github.com/nperma) |
0 commit comments