Improve export scripts

This commit is contained in:
zkldi
2021-12-09 21:22:03 +00:00
parent fa25f6e962
commit 39135df021
2 changed files with 45 additions and 20 deletions
+29 -14
View File
@@ -4,11 +4,15 @@ const { StaticConfig } = require("tachi-common");
const logger = require("./logger");
const path = require("path");
const fs = require("fs");
const DeterministicCollectionSort = require("./deterministic-collection-sort");
const { RemoveUnderscoreID } = require("./remove-_id");
const program = new Command();
program
.option("-c, --connection <127.0.0.1:27017/my_database>")
.option("-t, --useTabs");
.option("-t, --useTabs")
.option("-a, --all")
.argument("[collection]");
program.parse(process.argv);
const options = program.opts();
@@ -23,15 +27,19 @@ const db = monk(options.connection);
const collectionsDir = path.join(__dirname, "../collections");
const collections = [
"folders",
"tables",
];
const collections = [];
if (options.all) {
collections.push("folders", "tables");
collections.push(...StaticConfig.allSupportedGames.map(e => `songs-${e}`));
collections.push(...StaticConfig.allSupportedGames.map(e => `charts-${e}`));
} else if (program.args[0]) {
collections.push(program.args[0]);
}
// Add the songs-{game} and charts-{game} collections.
collections.push(...StaticConfig.allSupportedGames.map(e => `songs-${e}`));
collections.push(...StaticConfig.allSupportedGames.map(e => `charts-${e}`));
(async () => {
logger.info(`Exporting ${collections.length} collections.`);
@@ -39,16 +47,23 @@ collections.push(...StaticConfig.allSupportedGames.map(e => `charts-${e}`));
for (const collection of collections) {
logger.info(`Exporting ${collection}.`);
const data = await db.get(collection).find({});
logger.info(`Got ${data.length} documents.`);
const pt = path.join(collectionsDir, `${collection}.json`);
fs.writeFileSync(pt, JSON.stringify(data, null, options.useTabs ? "\t" : undefined));
logger.info(`Wrote to ${pt}.`);
await ExportCollection(collection);
}
DeterministicCollectionSort();
RemoveUnderscoreID();
logger.info(`Done!`);
process.exit(0);
})();
async function ExportCollection(collection) {
const data = await db.get(collection).find({});
logger.info(`Got ${data.length} documents.`);
const pt = path.join(collectionsDir, `${collection}.json`);
fs.writeFileSync(pt, JSON.stringify(data, null, options.useTabs ? "\t" : undefined));
logger.info(`Wrote to ${pt}.`);
}
+16 -6
View File
@@ -1,9 +1,19 @@
const { IterateCollections } = require("./util");
IterateCollections((data) => {
for (const d of data) {
delete d._id;
}
function RemoveUnderscoreID() {
IterateCollections((data) => {
for (const d of data) {
delete d._id;
}
return data;
});
return data;
});
}
if (require.main === module) {
RemoveUnderscoreID();
}
module.exports = {
RemoveUnderscoreID
}