-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
93 lines (85 loc) · 3.04 KB
/
import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require("dotenv").config();
const { Console } = require("console");
const fs = require("fs");
const dbUtil = require("./util/db")
const { Group, 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 runImport() {
dbUtil.syncDb();
db = dbUtil.getDb();
await importGroups();
await importCharacters();
}
async function importGroups() {
const groupFiles = fs.readdirSync("./assets/import/groups").filter(file => file.endsWith(".json"));
//read json file and parse it into a javascript object
for (const file of groupFiles) {
let group = fs.readFileSync(`./assets/import/groups/${file}`);
group = JSON.parse(group);
logger.log(`Importing group: ${group.name}`);
//check if group exists in database
let existingGroup = await db.Group.findOne({
where: {
name: group.name
}
})
if (existingGroup) {
logger.log(`Group ${group.name} already exists in database`);
continue;
} else {
//create group in database
await db.Group.create({
id: group.id,
name: group.name,
description: group.description,
imageURL: group.imageURL,
enabled: group.enabled
});
logger.log(`Created group ${group.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 group in database
await db.Character.create({
id: character.id,
groupId: character.groupId,
name: character.name,
description: character.description,
imageIdentifier: character.imageIdentifier,
enabled: character.enabled
});
logger.log(`Created character ${character.name} in database`);
}
}
}
}
logger.log("Importing...");
runImport().then(() => {
logger.log("Import complete");
process.exit(0);
}).catch((err) => {
logger.error(err);
process.exit(1);
});