Skip to content

Commit

Permalink
QuickDB (#376)
Browse files Browse the repository at this point in the history
* QuickDB

* QuickDB

fixed some bug

* QuickDB

fixed some bug
  • Loading branch information
nperma authored Jan 1, 2025
1 parent 16cad09 commit b192261
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions scripts/quick-db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(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)();
}

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 };

0 comments on commit b192261

Please sign in to comment.