-
Notifications
You must be signed in to change notification settings - Fork 0
/
items-base.js
57 lines (53 loc) · 1.33 KB
/
items-base.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
import _ from "lodash";
import axios from "axios";
import { default as mongodb } from "mongodb";
import dotenv from "dotenv";
dotenv.config();
const uri = process.env.MONGODB;
const client = new mongodb.MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const main = async () => {
const responseItems = await axios.get(
"https://5e.tools/data/items-base.json"
);
try {
const items = responseItems.data.baseitem;
console.log(`count ${items.length}`);
const newItems = await Promise.all(
items.map((item) => {
return {
id: `${item.name.replace(/\W/g, "")}-${item.source}-${
item.page
}`.toLowerCase(),
base: true,
...item,
};
})
);
return newItems;
} catch (error) {
throw new Error(error);
}
};
(async () => {
try {
await client.connect();
const dbo = client.db(process.env.DBNAME);
var text = await main();
await Promise.all(
text.map(async (item) => {
const query = { id: item.id };
const update = { $set: item };
const options = { upsert: true };
await dbo.collection("items").updateOne(query, update, options);
})
);
} catch (e) {
// Deal with the fact the chain failed
console.log(e);
} finally {
await client.close();
}
})();