-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
134 lines (111 loc) · 3.64 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const mongoose = require('mongoose');
require('dotenv').config();
const subscriptionSchema = require('./subscriptionSchema');
const noticeSchema = require('./noticeSchema')
const uri = process.env.MONGODB_ATLAS_URI;
async function saveUser(userSubscriptionObj) {
try {
await mongoose.connect(uri);
//compile model from schema
const Subscription = mongoose.model('Subscription', subscriptionSchema); //.model(nameOfCollection, Schema)
const user = new Subscription(userSubscriptionObj);
//save to DB
await user.save();
//if connection to DB and saving to DB is OK:
return 0;
} catch(e) {
console.error(e);
}
}
async function getAllUsers() {
try {
await mongoose.connect(uri);
//compile model from schema
const Subscription = mongoose.model('Subscription', subscriptionSchema); //.model(nameOfCollection, Schema)
const subscriptions = await Subscription.find({});
console.log(subscriptions);
return subscriptions;
} catch (err) {
console.error(err);
}
}
async function getMine() {
try {
await mongoose.connect(uri);
const Subscription = mongoose.model('Subscription', subscriptionSchema);
const mySub = await Subscription.find({"_id": "65657a891a043b38e4f50013"});
console.log(mySub[0]);
return mySub[0];
} catch(err) {
console.error(err);
}
}
// async function lookupNoticeEntry(elem) {
// try {
// await mongoose.connect(uri);
// const Notice = mongoose.model('noticeBoard', noticeSchema);
// const promise = await Notice.find({"uid" : elem.uid}).exec();
// console.log(promise.length)
// return promise;
// } catch(err) {
// console.error(err);
// }
// }
function lookupNoticeEntry(elem) {
return new Promise(async (resolve, reject) => {
try {
await mongoose.connect(uri);
const Notice = mongoose.model('noticeBoard', noticeSchema);
const matchingEntry = await Notice.findOne({"uid": elem.uid});
if (matchingEntry) {
reject('Notice entry exists');
} else {
// 이제는 matchingEntry가 없을경우 elem을 보내준다.
resolve(elem);
}
} catch(err) {
console.error(err);
reject(err); // Reject the promise in case of an error
}
});
}
async function saveNoticeItem(collectionName='noticeBoard', noticeObj) {
try {
await mongoose.connect(uri);
//compile model from schema
const Notice = mongoose.model('noticeBoard', noticeSchema, collectionName); //.model(nameOfCollection, Schema)
const notice = new Notice(noticeObj);
//save to DB
await notice.save();
} catch(e) {
console.error(e);
}
}
async function packSandwich(id) {
try {
await mongoose.connect(uri);
const Subscription = mongoose.model('Subscription', subscriptionSchema);
const promise = Subscription.findOne({"_id": id}).exec();
return promise;
} catch(e) {
console.error(e, "packSandwich has an error fix it");
}
}
// Close the MongoDB connection when the application is terminating
process.on('SIGINT', async () => {
try {
await mongoose.connection.close();
console.log('MongoDB connection closed');
process.exit(0);
} catch (err) {
console.error('Error closing MongoDB connection:', err);
process.exit(1);
}
});
module.exports = {
saveUser,
getAllUsers,
getMine,
lookupNoticeEntry,
saveNoticeItem
};