factor out async in reset-db-state

This commit is contained in:
zkldi
2021-04-04 17:58:22 +01:00
parent e1d20c2c49
commit af11ae0b0f
+20 -12
View File
@@ -7,21 +7,29 @@ import path from "path";
const DATA_DIR = path.join(__dirname, "./mock-db");
async function ResetState(file: string) {
let filename = path.basename(file, ".json");
await db.get(filename).remove({});
let fileLoc = path.join(DATA_DIR, file);
let data = JSON.parse(fs.readFileSync(fileLoc, "utf-8"));
if (!Array.isArray(data)) {
throw new Error(`Panic, ${filename} not JSONArray?`);
}
await db.get(filename).insert(data);
}
export default async function ResetDBState() {
const files = fs.readdirSync(DATA_DIR);
const promises = [];
for (const file of files) {
let filename = path.basename(file, ".json");
await db.get(filename).remove({});
let fileLoc = path.join(DATA_DIR, file);
let data = JSON.parse(fs.readFileSync(fileLoc).toString());
if (!Array.isArray(data)) {
throw new Error(`Panic, ${filename} not JSONArray?`);
}
await db.get(filename).insert(data);
promises.push(ResetState(file));
}
await Promise.all(promises);
}