-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongoProvider.js
78 lines (68 loc) · 2.16 KB
/
mongoProvider.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const mongo = require('mongodb').MongoClient;
const url = "mongodb://localhost:27017";
//const url = "mongodb+srv://user:[email protected]/test?retryWrites=true";
const database = 'test';
let db = null;
async function getDb() {
if(db !== null) {
return db;
}
const client = await mongo.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
sslValidate: false
}).catch(e => { console.log(e) });
let newDb = client.db(database);
if(db === null) {
db = newDb;
}
let close_conn = client.close;
newDb.close = function () {
close_conn.apply(client, arguments);
}
return newDb;
}
module.exports.getDb = getDb;
module.exports.find = async function (searchData, collectionName) {
if(db === null) {
await getDb();
}
const cursor = await db.collection(collectionName).find(searchData);
return cursor.toArray();
}
module.exports.update = async function (searchData, updateData, collectionName) {
await db.collection(collectionName).updateOne(searchData, updateData);
}
module.exports.updateMany = async function (searchData, updateData, collectionName) {
await db.collection(collectionName).updateMany(searchData, updateData, {
writeConcern: 1,
ordered: false
});
}
module.exports.insert = async function (data, collectionName) {
if(db === null) {
await getDb();
}
const res = await db.collection(collectionName).insertOne(data);
return res;
}
module.exports.insertMany = async function (data, collectionName) {
if(db === null) {
await getDb();
}
const res = await db.collection(collectionName).insertMany(data, {
writeConcern: 1,
ordered: false
});
return res;
}
module.exports.createIndexes = async function (indexData, collectionName) {
await db.collection(collectionName).createIndexes(
indexData,
function(err, result){
}
)
};
module.exports.drop = async function (collectionName) {
await db.collection(collectionName).drop().catch(e => { console.log(e) });
};