-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
116 lines (88 loc) · 3.18 KB
/
Copy pathdatabase.js
File metadata and controls
116 lines (88 loc) · 3.18 KB
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
import { } from 'dotenv/config.js'
import Mongo from 'mongodb'
const MongoClient = Mongo.MongoClient
class Database {
constructor() {
(async () => {
this.client = new MongoClient(process.env.ATLAS_URI, { useUnifiedTopology: true })
if (!this.client.isConnected()) await this.connect()
})()
}
async connect() {
/*
* This function is called for every time the new Database() is called
* In the start, I thought this was a bad habit, since the connection only had to be done once
* But I realized in order to connect a database call to a certain file ( E.g passport-config.js )
* The connect() has to be called, it gives the file the permission to call the database methods
*/
try {
await this.client.connect()
this.database = this.client.db(process.env.DATABASE_NAME)
this.users = this.database.collection(process.env.USERS_COLLECTION)
this.todos = this.database.collection(process.env.TODOS_COLLECTION)
} catch (error) {
throw error
}
}
closeConnection() {
if (!this.client.isConnected()) this.client.close()
}
async insert(object = {}) {
/*
* User: name, email, password
* ToDo: user_id, title
*/
const keys = Object.keys(object)
const addUserKeys = ['name', 'email', 'password']
const addTodoKeys = ['user_id', 'title']
try {
/*
* insert() is used by both addUser and addTodo.
* It then has to check which one it is.
* Once checked, it executes the proper function.
*/
if (keys.every((key, index) => key === addUserKeys[index])) {
const { name, email, password } = object
await this.users.insertOne({ name, email, password })
return true
}
if (keys.every((key, index) => key === addTodoKeys[index])) {
const { user_id, title } = object
await this.todos.insertOne({ user_id, title })
return true
}
} catch (error) {
throw error
}
}
getUsers() { return this.users.find({}) }
async getUserByEmail(email = '') {
try {
if (typeof (email) === 'string') {
if (email.includes('@') && email.includes('.')) {
return (await this.users.findOne({ email }))
}
}
} catch (error) {
throw error
}
}
async getUserById(_id = '') {
try {
if (typeof (_id) === 'string') return (await this.users.findOne({ '_id': Mongo.ObjectID(_id) }))
} catch (error) {
throw error
}
}
getToDosByUserID(user_id = '') {
if (typeof (user_id) === 'string') return this.todos.find({ user_id })
}
async removeToDoByTitle(title = '') {
try {
if (typeof (title) === 'string') await this.todos.deleteOne({ title })
} catch (error) {
throw error
}
}
}
export default Database