try load to local mongo
This commit is contained in:
1 parent
91623834d6
commit
a26d329d46
7 files changed
+705
-5
No files matched your search
@@ -0,0 +1,118 @@
|
||||
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<IYomitanIndex>("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<IYomitanTerm>("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<IYomitanTag>("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<IYomitanTermMeta>("term_meta")
|
||||
.insertMany(
|
||||
terms.map((rs) =>
|
||||
Object.fromEntries(rs.map((r, i) => [fields[i], r])),
|
||||
) as IYomitanTermMeta[],
|
||||
);
|
||||
} else {
|
||||
console.error(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user