-
Notifications
You must be signed in to change notification settings - Fork 37
/
database.js
44 lines (40 loc) · 1.1 KB
/
database.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
const MongoClient = require("mongodb").MongoClient;
const { getDay } = require("./utils/day");
const uri = process.env.collectionString;
let collection = null;
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
client.connect((err, data) => {
collection = client.db("91algo").collection("my-solution");
});
const findSolution = function (name) {
return new Promise((resolve, reject) => {
collection.find({ name }, function (err, result) {
if (err) reject(err);
resolve(result);
});
});
};
const dailyCheck = async function (name, data, callback) {
try {
const solution = await findSolution();
console.log(solution);
solution[getDay() - 1] = data;
collection.updateOne({ name }, { $set: solution });
return data;
} catch (_) {
const solution = Array(91);
solution[getDay() - 1] = data;
return new Promise((resolve, reject) => {
collection.insert({ name: solution }, function (err, result) {
if (err) reject(err);
resolve(result);
});
});
}
};
module.exports = {
dailyCheck,
};