-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
161 lines (145 loc) · 4.42 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Load required Modules
const MongoClient = require('mongodb').MongoClient;
/**
* Async mongodb class
*/
class asyncmongodb {
/**
* Create new object
* @param {Object} _config
*/
constructor(_config = {}) {
this.config = _config;
}
/**
* Connect to mongodb
*/
connect() {
return MongoClient.connect(this.config.uri)
.then((client) => { this.client = client; this.db = this.client.db(this.config.dbName); return true; })
.catch((e) => e);
}
/**
* Insert one row to mongodb
* @param {Object} param0
*/
insert({ collection, row }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).insertOne(row)
.then(_ => resolve(true))
.catch((e) => reject(e));
});
}
/**
* Insert many rows to mongodb
* @param {Object} param0
*/
insertMany({ collection, rows, ignoreErrors = true }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).insertMany(rows, { ordered: !ignoreErrors })
.then(_ => resolve(true))
.catch((e) => reject(e));
});
}
/**
* Update one row in mongodb
* @param {Object} param0
*/
update({ collection, row, where = {} }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).updateOne(where, { $set: row })
.then(_ => resolve(true))
.catch((e) => reject(e));
});
}
/**
* Update many rows in mongodb
* @param {Object} param0
*/
updateMany({ collection, rows, where = {}, ignoreErrors = true }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).updateMany(where, rows, { ordered: !ignoreErrors })
.then(_ => resolve(true))
.catch((e) => reject(e));
});
}
/**
* Delete one row in mongodb
* @param {Object} param0
*/
delete({ collection, where = {} }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).deleteOne(where)
.then(_ => resolve(true))
.catch((e) => reject(e));
});
}
/**
* Delete many rows in mongodb
* @param {Object} param0
*/
deleteMany({ collection, where = {}, ignoreErrors = true }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).deleteMany(where, { ordered: !ignoreErrors })
.then(_ => resolve(true))
.catch((e) => reject(e));
});
}
/**
* Find one row in mongodb
* @param {Object} param0
*/
findOne({ collection, where = {}, fields = {}, sort = {} }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).findOne(where, { sort: sort }, function (err, result) {
if (err) reject(err);
resolve(result);
});
});
}
/**
* Find rows in mongodb
* @param {Object} param0
*/
find({ collection, where = {}, limit = 0, skip = 0, sort = {}, fields = {} }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).find(where, fields).sort(sort).skip(skip).limit(limit).toArray(function (err, result) {
if (err) reject(err);
resolve(result);
});
});
}
/**
* Get count of documents
* @param {Object} param0
*/
count({ collection, where = {}, limit = 0, skip = 0, fields = {} }) {
return new Promise((resolve, reject) => {
this.db.collection(collection).find(where, fields).skip(skip).limit(limit).count(function (err, result) {
if (err) reject(err);
resolve(result);
});
});
}
/**
* Drop Collection
* @param {Object} param0
*/
dropCollection(collection) {
return new Promise((resolve, reject) => {
this.db.dropCollection(collection, function (err, result) {
if (err) reject(err);
resolve(result);
});
});
}
/**
* Disconnect Mongodb connection
*/
disconnect() {
if (this.client) {
return this.client.close();
}
}
}
module.exports = asyncmongodb;