Skip to content

Commit 57b0746

Browse files
authored
Quick DB (#368)
* Simple Database for ScriptAPI Database usiny DynamicDatabase * testing * Readme * Update test.js * Main Database Extension * Delete scripts/quick-db/test.js * QuickDB * Simple Database for ScriptAPI * Simple Database for ScriptAPI * Simple Database for ScriptAPI * Simple Database for ScriptAPI * Simple Database for ScriptAPI * Simple Database for ScriptAPI
1 parent 1b63f4a commit 57b0746

2 files changed

Lines changed: 254 additions & 0 deletions

File tree

scripts/quick-db/README.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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)

scripts/quick-db/index.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Script example for ScriptAPI
2+
// Author: Nperma <https://github.com/nperma>
3+
// Project: https://github.com/JaylyDev/ScriptAPI
4+
import { world, World } from "@minecraft/server";
5+
6+
const DATABASE_PREFIX = "\u0235\u0235";
7+
8+
const {
9+
getDynamicProperty: GET,
10+
setDynamicProperty: SET,
11+
getDynamicPropertyIds: IDS
12+
} = World.prototype;
13+
14+
//adapt code to JalyDev/scriptAPI
15+
class QuickDB {
16+
#identifier;
17+
constructor(id) {
18+
this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`;
19+
}
20+
21+
get size() {
22+
return IDS.call(world).filter((id) => id.startsWith(this.#identifier))
23+
.length;
24+
}
25+
26+
has(key) {
27+
return !!GET.call(world, `${this.#identifier}${key}`);
28+
}
29+
30+
get(key) {
31+
return this.has(key)
32+
? JSON.parse(GET.call(world, `${this.#identifier}${key}`))
33+
: undefined;
34+
}
35+
36+
set(key, value) {
37+
if (typeof key !== "string") return false;
38+
SET.call(world, `${this.#identifier}${key}`, JSON.stringify(value));
39+
return true;
40+
}
41+
42+
delete(key) {
43+
if (!this.has(key)) return false;
44+
SET.call(world, `${this.#identifier}${key}`, undefined);
45+
return true;
46+
}
47+
48+
keys() {
49+
return this.#UIDX("keys");
50+
}
51+
52+
values() {
53+
return this.#UIDX("values");
54+
}
55+
56+
entries() {
57+
return this.#UIDX("entries");
58+
}
59+
60+
#UIDX(type) {
61+
const ids = IDS.call(world);
62+
const result = [];
63+
64+
for (const id of ids) {
65+
if (!id.startsWith(this.#identifier)) continue;
66+
67+
const key = id.replace(this.#identifier, "");
68+
if (type === "keys") {
69+
result.push(key);
70+
} else if (type === "values") {
71+
const value = JSON.parse(this.get(key));
72+
result.push(value);
73+
} else if (type === "entries") {
74+
const value = JSON.parse(this.get(key));
75+
result.push([key, value]);
76+
}
77+
}
78+
79+
return result;
80+
}
81+
}
82+
83+
export default QuickDB;

0 commit comments

Comments
 (0)