From b619ecaf9022b4f678a6ebcd80c0e2ea31d7ec4a Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Wed, 1 Jan 2025 09:09:19 +0700 Subject: [PATCH 1/7] QuickDB --- scripts/quick-db/index.js | 62 ++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index 5e9d93e5..2c077955 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -24,7 +24,10 @@ class QuickDB { } has(key) { - return !!GET.call(world, `${this.#identifier}${key}`); + return !!( + GET.call(world, `${this.#identifier}${key}`) && + GET.call(world, `${this.#identifier}${key}`) !== undefined + ); } get(key) { @@ -46,38 +49,55 @@ class QuickDB { } keys() { - return this.#UIDX("keys"); + return Array.from(this.#UIDX("keys")); } values() { - return this.#UIDX("values"); + return Array.from(this.#UIDX("values")); } entries() { - return this.#UIDX("entries"); + return Array.from(this.#UIDX("entries")); } #UIDX(type) { - const ids = IDS.call(world); - const result = []; - - for (const id of ids) { - if (!id.startsWith(this.#identifier)) continue; - - const key = id.replace(this.#identifier, ""); - if (type === "keys") { - result.push(key); - } else if (type === "values") { - const value = JSON.parse(this.get(key)); - result.push(value); - } else if (type === "entries") { - const value = JSON.parse(this.get(key)); - result.push([key, value]); + const ids = this.getIds(); + let u_idx = 0; + const len = ids.length; + + return function* () { + while (u_idx < len) { + const id = ids[u_idx]; + const key = id.split(this.#identifier)[1]; + const value = this.get(id); + switch (type) { + case "key": + yield key; + break; + case "value": + yield this.has(key) ? JSON.parse(value) : undefined; + break; + case "entry": + yield [key, JSON.parse(value)]; + break; + } + u_idx++; } - } + }.bind(this)(); + } - return result; + getIds() { + return world + .getDynamicPropertyIds() + .filter((id) => id.startsWith(this.#identifier)); + } + + clear() { + for (const id of this.getIds()) { + this.delete(id.replace(this.#identifier,"")); + } } } export default QuickDB; +export { QuickDB }; From e7e402ce1a422c4f1a1aa3fcba37ae185238571e Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Wed, 1 Jan 2025 09:11:18 +0700 Subject: [PATCH 2/7] QuickDB fixed some bug --- scripts/quick-db/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index 2c077955..69cbf4b4 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -69,7 +69,7 @@ class QuickDB { while (u_idx < len) { const id = ids[u_idx]; const key = id.split(this.#identifier)[1]; - const value = this.get(id); + const value = this.get(key); switch (type) { case "key": yield key; From 72f930c87db16f0339933a41cf04cd2f1f7cb72e Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Wed, 1 Jan 2025 09:13:04 +0700 Subject: [PATCH 3/7] QuickDB fixed some bug --- scripts/quick-db/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index 69cbf4b4..ad10da80 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -77,7 +77,7 @@ class QuickDB { case "value": yield this.has(key) ? JSON.parse(value) : undefined; break; - case "entry": + case "entries": yield [key, JSON.parse(value)]; break; } From b8bd4a177f39c282b0f43329b9dca30a5452d7cf Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Sat, 11 Jan 2025 17:05:23 +0700 Subject: [PATCH 4/7] QuickDB update fixed method `.values()` and `.keys()` --- scripts/quick-db/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index ad10da80..9469c0a3 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -49,11 +49,11 @@ class QuickDB { } keys() { - return Array.from(this.#UIDX("keys")); + return Array.from(this.#UIDX("key")); } values() { - return Array.from(this.#UIDX("values")); + return Array.from(this.#UIDX("value")); } entries() { From 25d11f53cf7900cf2ccf5c35cb5853ba54e5ee80 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Sat, 11 Jan 2025 21:45:45 +0700 Subject: [PATCH 5/7] QuickDB --- scripts/quick-db/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index 9469c0a3..cbacbc39 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -100,4 +100,3 @@ class QuickDB { } export default QuickDB; -export { QuickDB }; From 538f6b726a44995681ae4428b0ed399d80ba7032 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Fri, 4 Apr 2025 21:09:49 +0700 Subject: [PATCH 6/7] QuickDB | FIXED --- scripts/quick-db/index.js | 162 +++++++++++++++++++++++++------------- 1 file changed, 108 insertions(+), 54 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index cbacbc39..0afec2a9 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -1,7 +1,8 @@ // Script example for ScriptAPI // Author: Nperma // Project: https://github.com/JaylyDev/ScriptAPI -import { world, World } from "@minecraft/server"; + +import { world, system, World } from "@minecraft/server"; const DATABASE_PREFIX = "\u0235\u0235"; @@ -11,92 +12,145 @@ const { getDynamicPropertyIds: IDS } = World.prototype; -//adapt code to JalyDev/scriptAPI class QuickDB { #identifier; + __cache; + + /** + * @param {string} id - Unique database identifier. + */ constructor(id) { + if (typeof id !== "string" || !id.trim()) { + throw new Error("Invalid database ID"); + } this.#identifier = `${DATABASE_PREFIX}${id}${DATABASE_PREFIX}`; + this.__cache = {}; + + for (const keyFull of this.getIds()) { + const key = keyFull.replace(this.#identifier, ""); + let value; + system.run(() => { + value = GET.call(world, keyFull); + }); + this.__cache[key] = JSON.parse(value); + } } + /** @returns {number} */ get size() { - return IDS.call(world).filter((id) => id.startsWith(this.#identifier)) - .length; + return this.keys().length; } - has(key) { - return !!( - GET.call(world, `${this.#identifier}${key}`) && - GET.call(world, `${this.#identifier}${key}`) !== undefined - ); + /** @returns {string[]} */ + keys() { + return Object.keys(this.__cache); } - get(key) { - return this.has(key) - ? JSON.parse(GET.call(world, `${this.#identifier}${key}`)) - : undefined; + /** @returns {any[]} */ + values() { + return Object.values(this.__cache); + } + + /** @returns {[string, any][]} */ + entries() { + return Object.entries(this.__cache); } + /** + * Stores a key-value pair. + * @param {string} key + * @param {any} value + * @returns {void} + */ set(key, value) { - if (typeof key !== "string") return false; - SET.call(world, `${this.#identifier}${key}`, JSON.stringify(value)); - return true; + if (typeof key !== "string" || !key.trim()) + throw new Error("Key must be a non-empty string"); + system.run(() => + SET.call(world, this.#identifier + key, JSON.stringify(value)) + ); + this.__cache[key] = value; } + /** + * Deletes a key. + * @param {string} key + * @returns {boolean} + */ delete(key) { if (!this.has(key)) return false; - SET.call(world, `${this.#identifier}${key}`, undefined); + system.run(() => SET.call(world, this.#identifier + key)); + delete this.__cache[key]; return true; } - keys() { - return Array.from(this.#UIDX("key")); - } - - values() { - return Array.from(this.#UIDX("value")); + /** + * Retrieves a value. + * @param {string} key + * @returns {any} + */ + get(key) { + if (typeof key !== "string" || !key.trim()) + throw new Error("Key must be a non-empty string"); + return this.__cache[key]; } - entries() { - return Array.from(this.#UIDX("entries")); + /** + * Checks if a key exists. + * @param {string} key + * @returns {boolean} + */ + has(key) { + return key in this.__cache; } - #UIDX(type) { - const ids = this.getIds(); - let u_idx = 0; - const len = ids.length; - - return function* () { - while (u_idx < len) { - const id = ids[u_idx]; - const key = id.split(this.#identifier)[1]; - const value = this.get(key); - switch (type) { - case "key": - yield key; - break; - case "value": - yield this.has(key) ? JSON.parse(value) : undefined; - break; - case "entries": - yield [key, JSON.parse(value)]; - break; - } - u_idx++; - } - }.bind(this)(); + /** @returns {string[]} */ + static get ids() { + let keys; + system.run(() => { + keys = IDS.call(world) + .filter((id) => id.startsWith(DATABASE_PREFIX)) + .map( + (k) => + k + .slice(DATABASE_PREFIX.length) + .split(DATABASE_PREFIX)[0] + ); + }); + return [...new Set(keys)]; } + /** @returns {string[]} */ getIds() { - return world - .getDynamicPropertyIds() - .filter((id) => id.startsWith(this.#identifier)); + let result; + system.run(() => { + result = IDS.call(world).filter((id) => + id.startsWith(this.#identifier) + ); + }); + return result; } + /** Clears the database. */ clear() { - for (const id of this.getIds()) { - this.delete(id.replace(this.#identifier,"")); + for (const key of this.keys()) { + this.delete(key); + } + this.__cache = {}; + } + + /** Clears all databases globally. */ + static clearAll() { + let keys; + system.run(() => { + keys = IDS.call(world).filter((id) => + id.startsWith(DATABASE_PREFIX) + ); + }); + for (const real_id of keys) { + system.run(() => SET.call(world, real_id)); } } } export default QuickDB; +export { QuickDB }; From a7e3fed7e46bbaf11671e4a27787f62fab7a7393 Mon Sep 17 00:00:00 2001 From: Nperma <129764133+nperma@users.noreply.github.com> Date: Sat, 5 Apr 2025 07:40:13 +0700 Subject: [PATCH 7/7] QuickDB (Fixed V2) --- scripts/quick-db/index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/quick-db/index.js b/scripts/quick-db/index.js index 9c810974..99b396d8 100644 --- a/scripts/quick-db/index.js +++ b/scripts/quick-db/index.js @@ -5,10 +5,6 @@ import { world, system, World } from "@minecraft/server"; - - -import { world,system, World } from '@minecraft/server'; - const DATABASE_PREFIX = '\u0235\u0235'; const {