forked from ariabisri/ews_redmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
83 lines (74 loc) · 2.3 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
81
82
83
require('dotenv').config();
const { MongoClient } = require('mongodb');
const url = process.env.DB_URL;
const dbName = process.env.DB_NAME;
const client = new MongoClient(url);;
async function add(col, val) {
// Use connect method to connect to the server
try {
await client.connect();
console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection(col);
if (collection.insertOne(val)) console.log(`Insertion Successful`);
} catch (error) {
console.log(error)
}
// return 'done.';
}
async function find(col, tgl) {
// Use connect method to connect to the server
try {
// const client = new MongoClient(process.env.MONGOURL);
await client.connect();
// console.log('Connected successfully to server');
const db = client.db(dbName);
const collection = db.collection(col);
let result = collection.find({"payload.user_terkait.hp":{$exists:true}, "payload.due_date":tgl})
return result.toArray()
} catch (error) {
console.log(error)
}
// return 'done.';
}
async function update (col, id, data) {
try {
await client.connect();
// database and collection code goes here
const db = client.db(dbName);
const coll = db.collection(col);
// update code goes here
const filter = {"_id":id};
const updateDoc = {
$set:{"payload.status_notifikasi":data}
};
const result = await coll.updateOne(filter, updateDoc);
// display the results of your operation
console.log("Number of documents updated: " + result.modifiedCount);
}
catch (error) {
console.log(error)
}
}
// find('issue_logs', '2022-12-03')
// main()
// .then(console.log)
// .catch(console.error)
// .finally(() => client.close());
// module.exports = { add };
// async function find(col){
// await client.connect();
// console.log('Connected successfully to server');
// const db = client.db(dbName);
// const collection = db.collection(col);
// let result = collection.find({"mode":'10'});
// return result.toArray()
// result = collection.findOne()
// .then(function(result) {
// console.log(result); // Use this to debug
// callback(result);
// }
// callback(result)
// console.log(result)
// }
module.exports= { add, find, update }