-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
44 lines (39 loc) · 1.19 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
require('dotenv').config();
import User from './user';
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
let db;
let collection;
MongoClient.connect(process.env.DB_HOST, function(err, client){
assert.equal(null, err);
console.log("Connected.")
db = client.db(process.env.DB_NAME);
collection = db.collection('users');
})
export const saveUser = async (user) => {
if(user._id == null || user._id == undefined){
await collection.insertOne(user);
} else {
await collection.updateOne({"_id": user._id}, {$set:
{
firstName: user.firstName,
userId: user.userId,
socialCreditScore: user.socialCreditScore}},
{upsert: true}
)
}
}
export const getUsers = async () => {
let res = await collection.find({}).sort({socialCreditScore: -1});
return res;
}
export const getUserById = async (user_id) => {
let test = await collection.findOne({userId: user_id});
if(test != undefined){
let u = new User(test.userId, test.firstName, test.socialCreditScore);
u._id = test._id;
return u;
} else {
return undefined
}
}