-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
80 lines (70 loc) · 1.96 KB
/
db.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
const { DB_NAME, DB_URL } = require("./config");
const { isArray } = require("util");
const R = require("ramda");
const MongoClient = require("mongodb").MongoClient;
let client = new MongoClient(DB_URL);
let db;
const initDB = async () => {
try {
// CREATE DB
await client.connect();
console.log("[db] Connected to server");
db = client.db(DB_NAME);
const existingColls = (await db
.listCollections({}, { nameOnly: true })
.toArray()).map(collObj => collObj.name);
// check if exist and create index
if (!R.includes("history", existingColls)) {
const collection = await db.createCollection("history");
await collection.createIndex({
"location.name": 1,
"forecast.forecastday.date_epoch": -1
});
console.log("[db] Created _history_ collection");
} else {
console.log("[db] _history_ collection was created before");
}
console.log("[db] Db and all collections initialised sucessfully");
// await client.close();
} catch (err) {
console.error(err);
}
};
// RETURN BOOL WHETHER DOCUMENT EXISTS
const isDocExists = (location, date) => {
return db
.collection("history")
.find({
"location.name": location,
"forecast.forecastday.date": date
})
.limit(1)
.toArray()
.then(res => res.length > 0);
};
// IN EITHER ARRAY OR SINGLE DOC
const writeDoc = data => {
if (isArray(data)) {
return db
.collection("history")
.insertMany(data)
.then(console.log(`[db] insterted ${data.length} records`));
} else {
const { location, forecast } = data;
return db
.collection("history")
.insertOne(data)
.then(
console.log(
`[db] Inserted ${location.name} ${
forecast.forecastday[0].date
} and resting`
)
)
.catch(err => console.error(err));
}
};
const closeDB = () => {
client.close();
};
module.exports = { initDB, isDocExists, writeDoc, closeDB };