diff --git a/database-seeds/scripts/export.js b/database-seeds/scripts/export.js index 6a00cf8d7..354b9c884 100644 --- a/database-seeds/scripts/export.js +++ b/database-seeds/scripts/export.js @@ -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}.`); +} \ No newline at end of file diff --git a/database-seeds/scripts/remove-_id.js b/database-seeds/scripts/remove-_id.js index 76fa681fb..742908706 100644 --- a/database-seeds/scripts/remove-_id.js +++ b/database-seeds/scripts/remove-_id.js @@ -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 +}