-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
46 lines (41 loc) · 1.15 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
const database = require('mongoose');
const { userModel } = require('./model');
require('dotenv').config("../.env");
const DB_CONNECTION = process.env.DB_CONNECTION;
console.log(`userModel`);
const init = () => {
if (DB_CONNECTION === undefined) return;
database
.connect(DB_CONNECTION)
.then((v) => {
console.log('MongoDB connected');
})
.catch((e) => {
console.error(`Connection Error ${e}`);
})
};
const getAllInfo = async () => {
let result = await userModel.find();
return result;
}
const addUser = async (id, address, num) => {
try {
while (num) {
const newUser = new userModel({
discordId: id,
walletAddress: address
})
await newUser.save((err) => {
if (err) return console.log(err);
console.log(newUser, "Saved Successfully");
})
num--;
}
let result = await getAllInfo();
return result;
} catch (error) {
console.log(`${error} occured`);
return;
}
};
module.exports = { init, addUser, getAllInfo };