diff --git a/assets/import/example_band.json b/assets/import/example_band.json new file mode 100644 index 0000000..5d41cde --- /dev/null +++ b/assets/import/example_band.json @@ -0,0 +1,7 @@ +{ + "id": "1", + "name": "BAND-MAID", + "description": "Band-Maid (stylized as BAND-MAID) is an all girl rock band from Tokyo that formed in July 2013. The band combines a rock sound with a maid image modeled on Japanese maid cafés.", + "imageURL": "https://cdn.discordapp.com/attachments/851543504831119380/1009467684490063892/unknown.png", + "enabled": 0 +} \ No newline at end of file diff --git a/assets/import/example_characters.json b/assets/import/example_characters.json new file mode 100644 index 0000000..5d9ce36 --- /dev/null +++ b/assets/import/example_characters.json @@ -0,0 +1,18 @@ +[ + { + "id": "1", + "bandId": "1", + "name": "Miku Kobato", + "description": "Miku Kobato is a Japanese singer, songwriter and guitarist. She is the initial founding member and main lyricist for BAND-MAID.", + "imageIdentifier": "bandmaid/miku.png", + "enabled": 0 + }, + { + "id": "2", + "bandId": "1", + "name": "Akane Hirose", + "description": "Akane Hirose is a Japanese drummer and founding member of BAND-MAID.", + "imageIdentifier": "bandmaid/akane.png", + "enabled": 0 + } +] \ No newline at end of file diff --git a/import.js b/import.js new file mode 100644 index 0000000..3932573 --- /dev/null +++ b/import.js @@ -0,0 +1,86 @@ +require("dotenv").config(); +const { Console } = require("console"); +const fs = require("fs"); +const dbUtil = require("./util/db") +const { Band, Character } = require("./models"); + +const logger = new Console({ + stdout: process.stdout, + stderr: process.stderr +}); + +//TODO: Fix ./data folders permission so wen can move out dataset in there + +async function importBands() { + const bandFiles = fs.readdirSync("./assets/import/bands").filter(file => file.endsWith(".json")); + //read json file and parse it into a javascript object + for (const file of bandFiles) { + let band = fs.readFileSync(`./assets/import/bands/${file}`); + band = JSON.parse(band); + logger.log(`Importing band: ${band.name}`); + //check if band exists in database + let existingBand = await db.Band.findOne({ + where: { + name: band.name + } + }) + + if (existingBand) { + logger.log(`Band ${band.name} already exists in database`); + continue; + } else { + //create band in database + await db.Band.create({ + id: band.id, + name: band.name, + description: band.description, + imageURL: band.imageURL, + enabled: band.enabled + }); + logger.log(`Created band ${band.name} in database`); + } + } +} + + +async function importCharacters() { + const characterFiles = fs.readdirSync("./assets/import/characters").filter(file => file.endsWith(".json")); + //read json file and parse it into a javascript object + for (const file of characterFiles) { + let characters = fs.readFileSync(`./assets/import/characters/${file}`); + characters = JSON.parse(characters); + for (character of characters) { + logger.log(`Importing character: ${character.name}`); + //check if character exists in database + let existingCharacter = await db.Character.findOne({ + where: { + id: character.id + } + }) + if (existingCharacter) { + logger.log(`Character ${character.name} already exists in database`); + continue; + } else { + //create band in database + await db.Character.create({ + id: character.id, + bandId: character.bandId, + name: character.name, + description: character.description, + imageIdentifier: character.imageIdentifier, + enabled: character.enabled + }); + logger.log(`Created character ${character.name} in database`); + } + } + } +} + +logger.log("Importing..."); +dbUtil.syncDb(); +db = dbUtil.getDb(); +importBands(); +importCharacters(); +logger.log("Import complete"); + + diff --git a/package.json b/package.json index 19c8810..ff27935 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "dev": "nodemon --inspect=0.0.0.0:9229 index.js" + "dev": "nodemon --inspect=0.0.0.0:9229 index.js", + "import": "node import.js" }, "author": "", "license": "ISC",