-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
112 lines (103 loc) · 2.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
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
// const { MongoClient } = require("mongodb");
import { MongoClient } from "mongodb";
const uri =
"mongodb://appuser:apppasword@localhost:27017/assignment?authSource=test_db&readPreference=primary&directConnection=true&ssl=false";
const client = new MongoClient(uri);
const doLogin = async (loginCreds) => {
try {
await client.connect();
const database = client.db("assignment");
const users = database.collection("users");
const query = { email: loginCreds.email };
console.log("Query", query);
const user = await users.findOne(query);
if (user) {
if (user.password === loginCreds.password) {
delete user.password;
return user;
} else {
return "Incorrect User/Password";
}
} else {
return "Incorrect User/Password";
}
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
};
const getUser = async (uid) => {
try {
await client.connect();
const database = client.db("assignment");
const users = database.collection("users");
// Query for a movie that has the title 'Back to the Future'
const query = { uid: uid };
const user = await users.findOne(query);
console.log(user);
return user;
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
};
const createUser = async (userObj) => {
try {
await client.connect();
const database = client.db("assignment");
const users = database.collection("users");
const query = { email: userObj.email };
const user = await users.findOne(query);
if (!user) {
const result = await users.insertOne(userObj);
console.log(result);
delete userObj.password;
return userObj;
} else {
console.log(user);
return "User already exist.";
}
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
};
const userExists = async (userObj) => {
try {
await client.connect();
const database = client.db("assignment");
const users = database.collection("users");
// Query for a movie that has the title 'Back to the Future'
const query = { email: userObj.email };
const user = await users.findOne(query);
if (!user) {
return false;
} else {
return true;
}
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
};
const getProducts = async () => {
try {
await client.connect();
const database = client.db("assignment");
const products = database.collection("products");
const items = await products.find().toArray();
console.log("Items", items);
return items;
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
};
const db = {
getUser: getUser,
createUser: createUser,
doLogin: doLogin,
getProducts: getProducts,
userExists: userExists,
};
export default db;