-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
251 lines (233 loc) · 8.99 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* This MongoDB Handler was written by depascaldc
* Copyright (C) 2020 | depascaldc - All Rights Reserved
*
* You may use, distribute and modify this code under the
* terms of the GPL-3.0 license, which unfortunately won't be
* written for another century.
*
* You should have received a copy of the GPL-3.0 license with
* this file. If not, please write to: , or visit : https://www.gnu.org/licenses/gpl-3.0.txt
*/
"use strict";
const MongoClient = require('mongodb').MongoClient;
const DRIVER_OPTIONS = {
useNewUrlParser: true,
useUnifiedTopology: true
}
module.exports.NodeJSMongoDriver = class NodeJSMongoDriver {
/**
* Get the instance of the driver by giving a connection String
* @param {String} connectionstring
*/
constructor(connectionstring) {
this.connectionstring = connectionstring
}
/**
* Get the instance of the driver by giving simply the following parameter ( wthout connection String )
* @param {String} username
* @param {String} password
* @param {String} host
* @param {Integer} port
* @param {String} authenticationDatabase
*/
static simple(username, password, host, port, authenticationDatabase) {
if (!username || !password || !host) {
throw new Error("Username Password and Host MUST be given.");
}
return new NodejsMongoDriver(`mongodb://${username}:${password}@${host}:${port != null ? port : "27017"}/${authenticationDatabase != null ? authenticationDatabase : "admin"}`)
}
/**
* Create a database at your MongoDB instance
* @param {String} databaseName
* @param {CallableFunction} callbackFunction
*/
createDatabase = (databaseName, callbackFunction) => {
MongoClient.connect(this.connectionstring + databaseName, DRIVER_OPTIONS, function (error, db) {
if (error) return callbackFunction(false, error);
db.close();
if (callbackFunction) callbackFunction(true);
});
}
/**
* Async creation of a database at your MongoDB instance
* @param {String} databaseName
* @returns {Promise<Object>}
*/
createDatabaseAsync = async (databaseName) => {
return new Promise((resolve, reject) => {
this.createDatabase(this.connectionstring, databaseName, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
/**
* Create a collection in your given database
* @param {String} databaseName
* @param {String} collectionName
* @param {CallableFunction} callbackFunction
*/
createCollection = (databaseName, collectionName, callbackFunction) => {
MongoClient.connect(this.connectionstring, DRIVER_OPTIONS, (error, db) => {
if (error) return callbackFunction(false, error);
var dbf = db.db(databaseName);
dbf.createCollection(collectionName, function (err, result) {
if (err) return callbackFunction(false, err);
db.close();
if (callbackFunction) callbackFunction(true)
});
})
}
/**
* Async creation of a collection in your given database
* @param {String} databaseName
* @param {String} collectionName
* @returns {Promise<Object>}
*/
createCollectionAsync = async (databaseName, collectionName) => {
return new Promise((resolve, reject) => {
this.createCollection(this.connectionstring, databaseName, collectionName, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
/**
* Search for an Object by give em an object to match
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} queryObject
* @param {CallableFunction} callbackFunction
*/
query = (databaseName, collectionName, queryObject, callbackFunction) => {
MongoClient.connect(this.connectionstring, DRIVER_OPTIONS, (error, db) => {
if (error) return callbackFunction(false, error);
var dbf = db.db(databaseName);
dbf.collection(collectionName).find(queryObject).toArray(function (err, result) {
if (err) return callbackFunction(false, err);
db.close();
if (callbackFunction) callbackFunction(result);
});
})
}
/**
* async search for an Object by give em an object to match
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} queryObject
* @returns {Promise<Object>}
*/
queryAsync = async (databaseName, collectionName, queryObject) => {
return new Promise((resolve, reject) => {
this.query(databaseName, collectionName, queryObject, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
/**
* Insert a new Object to the given Collection
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} onject
* @param {CallableFunction} callbackFunction
*/
insertObject = (databaseName, collectionName, object, callbackFunction) => {
MongoClient.connect(this.connectionstring, DRIVER_OPTIONS, (error, db) => {
if (error) return callbackFunction(false, error);
var dbf = db.db(databaseName);
dbf.collection(collectionName).insertOne(object, (err, result) => {
if (err) return callbackFunction(false, err);
db.close();
if (callbackFunction) callbackFunction(true);
})
})
}
/**
* Async insert a new Object to the given Collection
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} object
* @returns {Promise<Object>}
*/
insertObjectAsync = async (databaseName, collectionName, object) => {
return new Promise((resolve, reject) => {
this.insertObject(databaseName, collectionName, object, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
/**
* Delete a queried object
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} queryObject
* @param {CallableFunction} callbackFunction
*/
deleteObject = (databaseName, collectionName, queryObject, callbackFunction) => {
MongoClient.connect(this.connectionstring, DRIVER_OPTIONS, (error, db) => {
if (error) return callbackFunction(false, error);
var dbf = db.db(databaseName);
dbf.collection(collectionName).deleteOne(queryObject, function (err, result) {
if (err) return callbackFunction(false, err);
db.close();
if (callbackFunction) callbackFunction(true)
});
})
}
/**
* Async delete a queried object
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} queryObject
* @returns {Promise<Object>}
*/
deleteObjectAsync = async (databaseName, collectionName, queryObject) => {
return new Promise((resolve, reject) => {
this.deleteObject(databaseName, collectionName, queryObject, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
/**
* Update a queried object.. it inserts the given object key value pairs
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} queryObject
* @param {Object} newValues
* @param {CallableFunction} callbackFunction
*/
updateObject = (databaseName, collectionName, queryObject, newValues, callbackFunction) => {
MongoClient.connect(this.connectionstring, DRIVER_OPTIONS, (error, db) => {
if (error) return callbackFunction(false, error);
var dbf = db.db(databaseName);
let obj = {
$set: newValues
}
dbf.collection(collectionName).updateOne(queryObject, obj, function (err, result) {
if (err) return callbackFunction(false, err);
db.close();
if (callbackFunction) callbackFunction(true);
});
})
}
/**
* Async Update a queried object.. it inserts the given object key value pairs
* @param {String} databaseName
* @param {String} collectionName
* @param {Object} queryObject
* @param {Object} newValues
* @returns {Promise<Object>}
*/
updateObjectAsync = async (databaseName, collectionName, queryObject, newValues) => {
return new Promise((resolve, reject) => {
this.updateObject(databaseName, collectionName, queryObject, newValues, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
}