-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoDBMethods.js
87 lines (78 loc) · 1.82 KB
/
mongoDBMethods.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
import mongoose from "mongoose";
import { CodeAuthData, CodeUserData } from "./mongoDB.js";
const uri =
"mongodb+srv://ishu:[email protected]/?retryWrites=true&w=majority";
async function connect() {
try {
mongoose.connect(uri);
console.log("Connected to Database");
} catch (err) {
console.error(`Error in connecting to Database${err}`);
}
}
async function closeConnection() {
try {
mongoose.connection.close();
console.log("Disconnected");
} catch (err) {
console.error(`Error while disconnecting ${err}`);
}
}
async function addUser(data) {
try {
const user = new CodeAuthData(data);
await user.save();
console.log(data);
} catch (err) {
console.error(`Error while adding new user ${err}`);
}
}
async function findUser(data) {
try {
const user = await CodeAuthData.findOne({ email: data.email });
if (user) return user;
return null;
} catch (err) {
console.error(`Error while finding user ${err}`);
}
}
async function addUserData(email, data, url) {
try {
const userData = new CodeUserData({
email,
data,
url,
});
await userData.save();
console.log(data);
} catch (err) {
console.error(`Error while adding the user's data`);
}
}
async function findUrl(email) {
try {
const user = await CodeUserData.findOne({ email: email });
if (user) return user.url;
return null;
} catch (err) {
console.log(`Error while finding error ${err}`);
}
}
async function getUserData(email) {
try {
const user = await CodeAuthData.findOne({ email: email });
if (user) return user.data;
return null;
} catch (err) {
console.log(`Error while finding user data`);
}
}
export {
connect,
closeConnection,
addUser,
findUser,
addUserData,
findUrl,
getUserData,
};