57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { Database } from "arangojs";
|
|
import { effects, types, weapons, armors, shields, talismans, items, regions, enemies } from "../../data";
|
|
const db = new Database({ url: "http://localhost:48529" });
|
|
|
|
console.log("Populating database with ~100 static Ringstats entries...");
|
|
|
|
async function waitForArango() {
|
|
for (let i = 0; i < 30; i++) {
|
|
try {
|
|
await db.version();
|
|
return;
|
|
} catch {
|
|
console.log("Waiting for ArangoDB to be ready...");
|
|
await Bun.sleep(2000);
|
|
}
|
|
}
|
|
throw new Error("ArangoDB not reachable on localhost:48529");
|
|
}
|
|
|
|
await waitForArango();
|
|
|
|
const kv = db.collection("kv");
|
|
if (!(await kv.exists())) {
|
|
await kv.create();
|
|
}
|
|
|
|
const docs: { _key: string; value: any }[] = [];
|
|
|
|
const collect = (prefix: string, list: any[]) => {
|
|
for (const obj of list) {
|
|
const { items, enemies, ...data } = obj;
|
|
docs.push({ _key: `${prefix}:${obj.id}`, value: data });
|
|
if (items && items.length > 0) {
|
|
docs.push({ _key: `${prefix}:${obj.id}:items`, value: items });
|
|
}
|
|
if (enemies && enemies.length > 0) {
|
|
docs.push({ _key: `${prefix}:${obj.id}:enemies`, value: enemies });
|
|
}
|
|
}
|
|
};
|
|
|
|
collect("item_effect", effects);
|
|
collect("item_type", types);
|
|
collect("weapon", weapons);
|
|
collect("armor", armors);
|
|
collect("shield", shields);
|
|
collect("talisman", talismans);
|
|
collect("item", items);
|
|
collect("region", regions);
|
|
collect("enemy", enemies);
|
|
|
|
await kv.import(docs, { overwrite: true, onDuplicate: "replace" });
|
|
|
|
console.log(`Inserted ${effects.length + types.length + weapons.length + armors.length + shields.length + talismans.length + items.length + regions.length + enemies.length} primary entities into ArangoDB.`);
|
|
console.log("Database successfully seeded.");
|
|
process.exit(0);
|