import { MongoClient } from "mongodb"; import fs from "node:fs/promises"; import path from "node:path"; interface IYomitanIndex { title: string; sequenced: boolean; format: number; // integer? url: string; revision: string; attribution: string; } interface IYomitanTerm { term: string; reading: string; def_tags: any | null; rules: string; score: number; glossary: any; // structured? sequence: number; term_tags: string; } interface IYomitanTermMeta { term: string; mode: number; data: { reading: string; } | null; } interface IYomitanTag { name: string; category: string; sort_order: number; notes: string; score: number; } const client = new MongoClient("mongodb://root:example@localhost:27017"); await client.connect(); const dbYomitan = client.db("yomitan"); const colIndex = dbYomitan.collection("index"); // await loadFolderToYomitan("tmp/yomitan/jitenon-kotowaza"); await client.close(); async function loadFolderToYomitan(folderpath: string) { const dbx = client.db(path.parse(folderpath).base); for (const filename of await fs.readdir(folderpath)) { if (!filename.endsWith(".json")) continue; console.log(filename); if (filename === "index.json") { await colIndex.insertOne( JSON.parse(await fs.readFile(path.join(folderpath, filename), "utf-8")), ); } else if (filename.startsWith("term_bank_")) { const terms: any[][] = JSON.parse( await fs.readFile(path.join(folderpath, filename), "utf-8"), ); const fields = [ "term", "reading", "def_tags", "rules", "score", "glossary", "structured", "sequence", "term_tags", ]; await dbx .collection("term") .insertMany( terms.map((rs) => Object.fromEntries(rs.map((r, i) => [fields[i], r])), ) as IYomitanTerm[], ); } else if (filename.startsWith("tag_bank_")) { const terms: any[][] = JSON.parse( await fs.readFile(path.join(folderpath, filename), "utf-8"), ); const fields = [`name`, `category`, `sort_order`, `notes`, `score`]; await dbx .collection("tag") .insertMany( terms.map((rs) => Object.fromEntries(rs.map((r, i) => [fields[i], r])), ) as IYomitanTag[], ); } else if (filename.startsWith("term_meta_bank_")) { const terms: any[][] = JSON.parse( await fs.readFile(path.join(folderpath, filename), "utf-8"), ); const fields = ["term", "mode", "data"]; await dbx .collection("term_meta") .insertMany( terms.map((rs) => Object.fromEntries(rs.map((r, i) => [fields[i], r])), ) as IYomitanTermMeta[], ); } else { console.error(filename); } } }