-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
47 lines (38 loc) · 1.42 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
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const urlencodedParser = bodyParser.urlencoded({extended: false});
const url = "mongodb://aayush:[email protected]:42888/dreammy";
let dataSchema, data;
module.exports = {
connect :(collection, obj)=> {
mongoose.connect(url, { useNewUrlParser: true }).catch((err) => {console.log(err)});
dataSchema = new mongoose.Schema(obj);
data = mongoose.model(collection, dataSchema);
console.log("Connected!")
},
insert: (obj)=>{
let temp = new data(obj);
temp.save((err)=>{ if(err)console.log(err); console.log("Saved!") }).catch((err) => {console.log(err)});
},
find: (obj)=>{
data.find(obj).then((items)=> {
console.log(items);
return items;
}) .catch((err) => {console.log(err)});
},
count: async (obj)=>{
let size = 0;
await data.countDocuments(obj).then((count) => {
size = count;
}).catch((err) => {console.log(err)});
return size;
},
findSomeFields: async (obj, fields)=>{
return await data.find(obj, fields)
.catch((err) => {console.log(err)});
},
update: (obj, newValue)=>{
data.updateOne(obj, {$set: newValue}, {upsert: true})
.catch((err) => {console.log(err)});
}
}