-
Notifications
You must be signed in to change notification settings - Fork 0
/
importTBCA.ts
35 lines (27 loc) · 1.13 KB
/
importTBCA.ts
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
import { Food } from '@/model/foodModel'
import TBCAJson from './tbca.json'
import { upsertFood, deleteAll } from '@/controllers/food'
const parallelLimit = require('async/parallelLimit');
//TODO: Remove all code related to TBCA, as well as the TBCA.json file
const TBCA = TBCAJson as unknown as { [key: string]: Food }
export async function deleteAndReimportFoods(progressCallback?: (total: number) => void) {
const total = Object.values(TBCA).length;
console.log('Deleting all foods...');
await deleteAll();
console.log('Finished deleting foods.');
console.log('Waiting 3 seconds...');
// sleep for 1 second to allow the server to catch up
await new Promise(r => setTimeout(r, 3000));
console.log('Finished waiting.');
console.log('Creating foods...');
await parallelLimit(Object.values(TBCA).map((food) => async () => {
console.log(`Creating ${food.name}...`);
try {
await upsertFood(food);
if (progressCallback) progressCallback(total);
} catch (e) {
console.error(e);
}
console.log(`Finished ${food.name}.`);
}), 10);
}