This repository was archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
61 lines (48 loc) · 1.57 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
const MongoClient = require('mongodb').MongoClient
const config = require('./config.json')
const connect = async function() {
const uri = `mongodb+srv://modos:${config.db.password}@chatnodesocket.mdxoy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`
const client = new MongoClient(uri)
try {
await client.connect()
} catch (error) {
console.log(error)
}
return client
}
const findUserPhoto = async function(sessionID) {
const client = await connect()
let image = null
try {
image = (await client.db(config.db.name).collection('users').findOne({sessionID: sessionID}, {image: 1})).image
} catch (error) {
image = "null"
}
return image
}
const existedUser = async function (client, username, email) {
return await client.db(config.db.name).collection('users').find({ $or: [ { username: username }, { email: email } ] }).toArray()
}
const allUser = async function (client) {
return await client.db(config.db.name).collection('users').find({}).toArray()
}
const user = async function (client, username, password) {
return await client.db(config.db.name).collection('users').find({ $and: [ { username: username }, { password: password } ] }).toArray()
}
const uploadAvatar = async function (client, username, path) {
await client.db(config.db.name).collection('users').updateOne({
username: username
}, {
$set: {
image: path
}
})
}
module.exports = {
findUserPhoto,
connect,
existedUser,
allUser,
user,
uploadAvatar
}