gmongo 2.0.8 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +8 -0
- package/README.md +74 -34
- package/examples/encryptDataAtRest.js +173 -0
- package/mongo.js +189 -125
- package/package.json +4 -2
- package/services/aes.js +11 -1
- package/services/connect.js +21 -35
- package/services/objectId.js +2 -2
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
{
|
|
2
|
+
"editor.defaultFormatter": "rvest.vs-code-prettier-eslint",
|
|
3
|
+
"editor.formatOnPaste": false, // required
|
|
4
|
+
"editor.formatOnType": false, // required
|
|
5
|
+
"editor.formatOnSave": true, // optional
|
|
6
|
+
"editor.formatOnSaveMode": "file", // required to format on save
|
|
7
|
+
"files.autoSave": "onFocusChange" // optional but recommended
|
|
8
|
+
}
|
package/README.md
CHANGED
|
@@ -24,18 +24,22 @@ Mongo is the preferred database format for NodeJS based systems. It supports mul
|
|
|
24
24
|
|
|
25
25
|
### To connect to a server use Mongo.start()
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
When using a x509 certificate, the path must be a local path on the device, username and password should be null, on the contrary, when using a username and password, the x509 should be null.
|
|
28
|
+
|
|
29
|
+
```node
|
|
30
|
+
const Mongo = require( 'gmongo' )
|
|
31
|
+
|
|
28
32
|
Mongo.start(
|
|
29
33
|
<Is Atlas DB>, //Boolean
|
|
30
|
-
<
|
|
31
|
-
<IP>, //String
|
|
34
|
+
<Collection Name>, //String
|
|
35
|
+
<IP or host>, //String
|
|
32
36
|
<Port>, //Number or NULL
|
|
33
37
|
<User>, //String or NULL
|
|
34
38
|
<Password>, //String or NULL
|
|
35
39
|
<x509 Certificate Path>, //String or NULL
|
|
36
40
|
<Timeout in milliseconds>, //Number or NULL
|
|
37
41
|
)
|
|
38
|
-
.then(
|
|
42
|
+
.then( ()=> console.log('Im Connected!'))
|
|
39
43
|
```
|
|
40
44
|
|
|
41
45
|
Example:
|
|
@@ -74,7 +78,7 @@ Mongo.start( false, 'MyDatabase', '127.0.0.1', 27017, null, null, null, 20000 )
|
|
|
74
78
|
|
|
75
79
|
### To INSERT a new row or a set of rows, use Mongo.insert()
|
|
76
80
|
|
|
77
|
-
```
|
|
81
|
+
```node
|
|
78
82
|
Mongo.insert(
|
|
79
83
|
<Database Name>, //String
|
|
80
84
|
<Collection Name>, //String
|
|
@@ -85,17 +89,20 @@ Mongo.insert(
|
|
|
85
89
|
Example:
|
|
86
90
|
|
|
87
91
|
```node
|
|
88
|
-
|
|
92
|
+
const data = {
|
|
89
93
|
username: 'jose',
|
|
90
94
|
pass: '123',
|
|
91
95
|
active: false,
|
|
92
|
-
added: new Date().getTime()
|
|
93
|
-
}
|
|
96
|
+
added: new Date().getTime(),
|
|
97
|
+
}
|
|
98
|
+
Mongo.insert('MyDatabase', 'users', data).then((result) =>
|
|
99
|
+
console.log('all done', data._id, result.insertedId)
|
|
100
|
+
)
|
|
94
101
|
```
|
|
95
102
|
|
|
96
103
|
### To DELETE rows from the collection, use Mongo.delete()
|
|
97
104
|
|
|
98
|
-
```
|
|
105
|
+
```node
|
|
99
106
|
Mongo.delete(
|
|
100
107
|
<Database Name>, //String
|
|
101
108
|
<Collection Name>, //String
|
|
@@ -107,13 +114,13 @@ Example:
|
|
|
107
114
|
|
|
108
115
|
```node
|
|
109
116
|
Mongo.delete('MyDatabase', 'users', {
|
|
110
|
-
myuser: Mongo.id(user._id)
|
|
111
|
-
}).then(result => console.log('all done'))
|
|
117
|
+
myuser: Mongo.id(user._id),
|
|
118
|
+
}).then((result) => console.log('all done'))
|
|
112
119
|
```
|
|
113
120
|
|
|
114
121
|
### To UPDATE rows in the collection use Mongo.update
|
|
115
122
|
|
|
116
|
-
```
|
|
123
|
+
```node
|
|
117
124
|
Mongo.update(
|
|
118
125
|
<Database Name>, //String
|
|
119
126
|
<Collection Name>, //String
|
|
@@ -129,17 +136,17 @@ Mongo.update(
|
|
|
129
136
|
'MyDatabase',
|
|
130
137
|
'users',
|
|
131
138
|
{
|
|
132
|
-
active: false
|
|
139
|
+
active: false,
|
|
133
140
|
},
|
|
134
141
|
{
|
|
135
|
-
active: true
|
|
142
|
+
active: true,
|
|
136
143
|
}
|
|
137
|
-
).then(result => console.log('all done'))
|
|
144
|
+
).then((result) => console.log('all done'))
|
|
138
145
|
```
|
|
139
146
|
|
|
140
147
|
### To QUERY the collection use Mongo.query()
|
|
141
148
|
|
|
142
|
-
```
|
|
149
|
+
```node
|
|
143
150
|
Mongo.query(
|
|
144
151
|
<Database Name>, //String
|
|
145
152
|
<Collection Name>, //String
|
|
@@ -151,13 +158,13 @@ Example:
|
|
|
151
158
|
|
|
152
159
|
```node
|
|
153
160
|
Mongo.query('MyDatabase', 'users', {
|
|
154
|
-
active: true
|
|
155
|
-
}).then(rowsFromQuery => console.log(rowsFromQuery))
|
|
161
|
+
active: true,
|
|
162
|
+
}).then((rowsFromQuery) => console.log(rowsFromQuery))
|
|
156
163
|
```
|
|
157
164
|
|
|
158
165
|
### To QUERY with a SORT use Mongo.querySort
|
|
159
166
|
|
|
160
|
-
```
|
|
167
|
+
```node
|
|
161
168
|
Mongo.querySort(
|
|
162
169
|
<Database Name>, //String
|
|
163
170
|
<Collection Name>, //String
|
|
@@ -169,17 +176,14 @@ Mongo.querySort(
|
|
|
169
176
|
Example:
|
|
170
177
|
|
|
171
178
|
```node
|
|
172
|
-
Mongo.querySort(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
{ added: 1 },
|
|
176
|
-
{ active: true }
|
|
177
|
-
).then(rowsFromQuery => console.log(rowsFromQuery))
|
|
179
|
+
Mongo.querySort('MyDatabase', 'users', { added: 1 }, { active: true }).then(
|
|
180
|
+
(rowsFromQuery) => console.log(rowsFromQuery)
|
|
181
|
+
)
|
|
178
182
|
```
|
|
179
183
|
|
|
180
184
|
### To QUERY with a SORT and LIMIT, use Mongo.queryLimitSort
|
|
181
185
|
|
|
182
|
-
```
|
|
186
|
+
```node
|
|
183
187
|
Mongo.queryLimitSort(
|
|
184
188
|
<Database Name>, //String
|
|
185
189
|
<Collection Name>, //String
|
|
@@ -198,14 +202,14 @@ Mongo.queryLimitSort(
|
|
|
198
202
|
100,
|
|
199
203
|
{ added: 1 },
|
|
200
204
|
{ active: true }
|
|
201
|
-
).then(rowsFromQuery => console.log(rowsFromQuery))
|
|
205
|
+
).then((rowsFromQuery) => console.log(rowsFromQuery))
|
|
202
206
|
```
|
|
203
207
|
|
|
204
208
|
### For a SINGLE QUERY the collection use Mongo.singleQuery()
|
|
205
209
|
|
|
206
210
|
Note: singleQuery should only ever query **ONE ROW**.
|
|
207
211
|
|
|
208
|
-
```
|
|
212
|
+
```node
|
|
209
213
|
Mongo.singleQuery(
|
|
210
214
|
<Database Name>, //String
|
|
211
215
|
<Collection Name>, //String
|
|
@@ -217,21 +221,21 @@ Example:
|
|
|
217
221
|
|
|
218
222
|
```node
|
|
219
223
|
Mongo.singleQuery('MyDatabase', 'users', {
|
|
220
|
-
myuser: Mongo.id(user._id)
|
|
221
|
-
}).then(rowFromQuery => console.log(rowFromQuery))
|
|
224
|
+
myuser: Mongo.id(user._id),
|
|
225
|
+
}).then((rowFromQuery) => console.log(rowFromQuery))
|
|
222
226
|
```
|
|
223
227
|
|
|
224
228
|
This **singleQuery** is very useful in the authentication methods, e.g.
|
|
225
229
|
|
|
226
230
|
```node
|
|
227
231
|
Mongo.singleQuery('MyDatabase', 'users', {
|
|
228
|
-
loginSessionKey: req.sessionKey
|
|
229
|
-
}).then(rowsFromQuery => res.json(rowFromQuery ? true : false))
|
|
232
|
+
loginSessionKey: req.sessionKey,
|
|
233
|
+
}).then((rowsFromQuery) => res.json(rowFromQuery ? true : false))
|
|
230
234
|
```
|
|
231
235
|
|
|
232
236
|
### To JOIN a SINGLE COLLECTION to another, use Mongo.join()
|
|
233
237
|
|
|
234
|
-
```
|
|
238
|
+
```node
|
|
235
239
|
Mongo.join(
|
|
236
240
|
<Database Name>, //String
|
|
237
241
|
<Collection Name>, //String
|
|
@@ -256,7 +260,43 @@ Mongo.join(
|
|
|
256
260
|
'photos',
|
|
257
261
|
{ added: 1 },
|
|
258
262
|
{ myuser: Mongo.id(user._id) }
|
|
259
|
-
).then(rowFromQuery => console.log(rowFromQuery))
|
|
263
|
+
).then((rowFromQuery) => console.log(rowFromQuery))
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Adding/Querying Encryption at rest to data queries
|
|
267
|
+
|
|
268
|
+
Many times applications require secure secure data at rest for safe storage of personal information. This package includes this automatically in the following functions:
|
|
269
|
+
|
|
270
|
+
| Function |
|
|
271
|
+
| -------------- |
|
|
272
|
+
| insert |
|
|
273
|
+
| update |
|
|
274
|
+
| query |
|
|
275
|
+
| singleQuery |
|
|
276
|
+
| querySort |
|
|
277
|
+
| queryLimitSort |
|
|
278
|
+
| join |
|
|
279
|
+
|
|
280
|
+
To use this feature you first need to define a Key and IV for the encrypted columns selected. Save these to a very safe and not public location for usage in the functions.
|
|
281
|
+
|
|
282
|
+
```node
|
|
283
|
+
const key = Mongo.aes.makeKey()
|
|
284
|
+
const iv = Mongo.aes.makeIv()
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
To make use of the automatic encryption and decryption, please add the following fields to the end of the function as shown in the following example:
|
|
288
|
+
|
|
289
|
+
```node
|
|
290
|
+
const key = Mongo.aes.makeKey()
|
|
291
|
+
const iv = Mongo.aes.makeIv()
|
|
292
|
+
Mongo.query(
|
|
293
|
+
'TestCollection',
|
|
294
|
+
'TestTable',
|
|
295
|
+
{},
|
|
296
|
+
['encryptedColumn'],
|
|
297
|
+
key,
|
|
298
|
+
iv
|
|
299
|
+
).then((rowFromQuery) => console.log(rowFromQuery))
|
|
260
300
|
```
|
|
261
301
|
|
|
262
302
|
---
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
const MGO = require('../mongo.js')
|
|
2
|
+
|
|
3
|
+
const keys = {
|
|
4
|
+
test: {
|
|
5
|
+
columns: ['name', 'dob'],
|
|
6
|
+
key: MGO.aes.format.key(
|
|
7
|
+
'6233616364623234393935616165333332633466376134663031633833643262'
|
|
8
|
+
),
|
|
9
|
+
iv: MGO.aes.format.iv('62323166666532663034393535346233'),
|
|
10
|
+
},
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const start = async (_) => {
|
|
14
|
+
console.log('test connect')
|
|
15
|
+
const DBName = 'Account'
|
|
16
|
+
const DBHost = 'test.mongodb.net'
|
|
17
|
+
const DBPort = null
|
|
18
|
+
const DBUser = null
|
|
19
|
+
const DBPass = null
|
|
20
|
+
const DBx509 = ''
|
|
21
|
+
await MGO.start(true, DBName, DBHost, DBPort, DBUser, DBPass, DBx509, 20000)
|
|
22
|
+
console.log('connected')
|
|
23
|
+
console.log(keys.test)
|
|
24
|
+
if (false) {
|
|
25
|
+
console.log('insert test')
|
|
26
|
+
const data = {
|
|
27
|
+
added: new Date(),
|
|
28
|
+
incrementer: 2,
|
|
29
|
+
test: 'TEST String',
|
|
30
|
+
}
|
|
31
|
+
console.log(
|
|
32
|
+
await MGO.insert(
|
|
33
|
+
DBName,
|
|
34
|
+
'TestTable',
|
|
35
|
+
data,
|
|
36
|
+
['test'],
|
|
37
|
+
keys.test.key,
|
|
38
|
+
keys.test.iv
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
console.log(data)
|
|
42
|
+
}
|
|
43
|
+
if (false) {
|
|
44
|
+
console.log('delete test')
|
|
45
|
+
console.log(
|
|
46
|
+
await MGO.delete(DBName, 'TestTable', {
|
|
47
|
+
_id: MGO.id('655003e5d765319d61037ceb'),
|
|
48
|
+
})
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
if (false) {
|
|
52
|
+
console.log('update test')
|
|
53
|
+
console.log(
|
|
54
|
+
await MGO.update(
|
|
55
|
+
DBName,
|
|
56
|
+
'TestTable',
|
|
57
|
+
{
|
|
58
|
+
_id: MGO.id('655003c9506eaf7ed924e885'),
|
|
59
|
+
},
|
|
60
|
+
{ test: 'My favourite data' },
|
|
61
|
+
['test'],
|
|
62
|
+
keys.test.key,
|
|
63
|
+
keys.test.iv
|
|
64
|
+
)
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
if (false) {
|
|
68
|
+
console.log('query test')
|
|
69
|
+
console.log(
|
|
70
|
+
await MGO.query(
|
|
71
|
+
DBName,
|
|
72
|
+
'TestTable',
|
|
73
|
+
{},
|
|
74
|
+
['test'],
|
|
75
|
+
keys.test.key,
|
|
76
|
+
keys.test.iv
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
if (false) {
|
|
81
|
+
console.log('single query test')
|
|
82
|
+
console.log(
|
|
83
|
+
await MGO.singleQuery(
|
|
84
|
+
DBName,
|
|
85
|
+
'TestTable',
|
|
86
|
+
{
|
|
87
|
+
_id: MGO.id('65500e6decab789298f5a32e'),
|
|
88
|
+
},
|
|
89
|
+
['test'],
|
|
90
|
+
keys.test.key,
|
|
91
|
+
keys.test.iv
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
if (false) {
|
|
96
|
+
console.log('query sort test')
|
|
97
|
+
console.log(
|
|
98
|
+
await MGO.querySort(
|
|
99
|
+
DBName,
|
|
100
|
+
'TestTable',
|
|
101
|
+
{ added: 1 },
|
|
102
|
+
{},
|
|
103
|
+
['test'],
|
|
104
|
+
keys.test.key,
|
|
105
|
+
keys.test.iv
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
if (false) {
|
|
110
|
+
console.log('query limit sort test')
|
|
111
|
+
console.log(
|
|
112
|
+
await MGO.queryLimitSort(
|
|
113
|
+
DBName,
|
|
114
|
+
'TestTable',
|
|
115
|
+
1,
|
|
116
|
+
{ added: -1 },
|
|
117
|
+
{},
|
|
118
|
+
['test'],
|
|
119
|
+
keys.test.key,
|
|
120
|
+
keys.test.iv
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
if (false) {
|
|
125
|
+
console.log('join test')
|
|
126
|
+
console.log(
|
|
127
|
+
await MGO.join(
|
|
128
|
+
DBName,
|
|
129
|
+
'TestTable',
|
|
130
|
+
'incrementer',
|
|
131
|
+
'TestTable1',
|
|
132
|
+
'incrementer',
|
|
133
|
+
'innerObject',
|
|
134
|
+
{ added: 1 },
|
|
135
|
+
{},
|
|
136
|
+
['test'],
|
|
137
|
+
keys.test.key,
|
|
138
|
+
keys.test.iv
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
console.log(MGO.aes.makeIv())
|
|
144
|
+
console.log(MGO.aes.makeKey())
|
|
145
|
+
process.exit()
|
|
146
|
+
/*
|
|
147
|
+
console.log(MGO.aes.makeIv())
|
|
148
|
+
console.log(MGO.aes.makeKey())
|
|
149
|
+
|
|
150
|
+
await MGO.insert(
|
|
151
|
+
'this',
|
|
152
|
+
'test',
|
|
153
|
+
[
|
|
154
|
+
{ name: 'James Encke', dob: '2021-01-01' },
|
|
155
|
+
{ name: 'Matthew Encke', dob: '2021-01-01' }
|
|
156
|
+
],
|
|
157
|
+
keys.test.columns,
|
|
158
|
+
keys.test.key,
|
|
159
|
+
keys.test.iv
|
|
160
|
+
)
|
|
161
|
+
await MGO.query(
|
|
162
|
+
'this',
|
|
163
|
+
'test',
|
|
164
|
+
{},
|
|
165
|
+
keys.test.columns,
|
|
166
|
+
keys.test.key,
|
|
167
|
+
keys.test.iv
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
*/
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
start()
|
package/mongo.js
CHANGED
|
@@ -32,22 +32,22 @@ const db = {
|
|
|
32
32
|
updateList: {},
|
|
33
33
|
databaseList: {},
|
|
34
34
|
aes: aes,
|
|
35
|
-
id: name =>
|
|
35
|
+
id: (name) =>
|
|
36
36
|
typeof name != 'string' || name.length % 12 == 0 ? objectId(name) : null,
|
|
37
37
|
start: (isAtlas, dbName, ip, port, user, pass, x509, timeoutInMS) =>
|
|
38
38
|
new Promise((resolve, reject) => {
|
|
39
39
|
if (!Array.isArray(dbName)) {
|
|
40
40
|
dbName = [dbName]
|
|
41
41
|
}
|
|
42
|
-
const connectDB = _ => {
|
|
42
|
+
const connectDB = (_) => {
|
|
43
43
|
const thisDB = dbName && dbName.length > 0 ? dbName.shift() : null
|
|
44
44
|
if (thisDB) {
|
|
45
45
|
connect(isAtlas, thisDB, ip, port, user, pass, x509, timeoutInMS)
|
|
46
|
-
.then(dbObject => {
|
|
46
|
+
.then((dbObject) => {
|
|
47
47
|
db.databaseList[thisDB] = dbObject
|
|
48
48
|
connectDB()
|
|
49
49
|
})
|
|
50
|
-
.catch(err => {
|
|
50
|
+
.catch((err) => {
|
|
51
51
|
console.log('Database Connection Error', err)
|
|
52
52
|
reject()
|
|
53
53
|
})
|
|
@@ -58,7 +58,7 @@ const db = {
|
|
|
58
58
|
connectDB()
|
|
59
59
|
}),
|
|
60
60
|
insert: (dbName, table, rowOrRows, columsOfRowOrRowsToEncrypt, key, iv) =>
|
|
61
|
-
new Promise((resolve, reject) => {
|
|
61
|
+
new Promise(async (resolve, reject) => {
|
|
62
62
|
if (db.databaseList[dbName]) {
|
|
63
63
|
if (columsOfRowOrRowsToEncrypt && key && iv) {
|
|
64
64
|
if (Array.isArray(rowOrRows)) {
|
|
@@ -81,98 +81,113 @@ const db = {
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
|
-
db.databaseList[dbName].collection(table
|
|
85
|
-
|
|
84
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
85
|
+
resFmt(
|
|
86
|
+
null,
|
|
87
|
+
await collection[
|
|
86
88
|
Array.isArray(rowOrRows) ? 'insertMany' : 'insertOne'
|
|
87
|
-
](rowOrRows
|
|
89
|
+
](rowOrRows),
|
|
90
|
+
resolve,
|
|
91
|
+
reject
|
|
88
92
|
)
|
|
89
93
|
} else {
|
|
90
94
|
reject('No Connection')
|
|
91
95
|
}
|
|
92
96
|
}),
|
|
93
97
|
delete: (dbName, table, dataToRemove) =>
|
|
94
|
-
new Promise((resolve, reject) => {
|
|
98
|
+
new Promise(async (resolve, reject) => {
|
|
95
99
|
if (db.databaseList[dbName]) {
|
|
96
|
-
db.databaseList[dbName].collection(table
|
|
97
|
-
|
|
98
|
-
resFmt(err, result, resolve, reject)
|
|
99
|
-
)
|
|
100
|
-
)
|
|
100
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
101
|
+
resFmt(null, await collection.deleteMany(dataToRemove), resolve, reject)
|
|
101
102
|
} else {
|
|
102
103
|
reject('No Connection')
|
|
103
104
|
}
|
|
104
105
|
}),
|
|
105
|
-
update: (
|
|
106
|
-
|
|
106
|
+
update: (
|
|
107
|
+
dbName,
|
|
108
|
+
table,
|
|
109
|
+
dataToUpdate,
|
|
110
|
+
newData,
|
|
111
|
+
columsOfRowOrRowsToEncrypt,
|
|
112
|
+
key,
|
|
113
|
+
iv
|
|
114
|
+
) =>
|
|
115
|
+
new Promise(async (resolve, reject) => {
|
|
107
116
|
if (db.databaseList[dbName]) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
117
|
+
if (columsOfRowOrRowsToEncrypt && key && iv) {
|
|
118
|
+
for (let n = 0; n < columsOfRowOrRowsToEncrypt.length; n++) {
|
|
119
|
+
newData[columsOfRowOrRowsToEncrypt[n]] = aes.encrypt(
|
|
120
|
+
key,
|
|
121
|
+
iv,
|
|
122
|
+
newData[columsOfRowOrRowsToEncrypt[n]]
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
127
|
+
resFmt(
|
|
128
|
+
null,
|
|
129
|
+
await collection.updateMany(dataToUpdate, { $set: newData }),
|
|
130
|
+
resolve,
|
|
131
|
+
reject
|
|
114
132
|
)
|
|
115
133
|
} else {
|
|
116
134
|
reject('No Connection')
|
|
117
135
|
}
|
|
118
136
|
}),
|
|
119
137
|
singleQuery: (dbName, table, query, columnsToDecrypt, key, iv) =>
|
|
120
|
-
new Promise((resolve, reject) => {
|
|
138
|
+
new Promise(async (resolve, reject) => {
|
|
121
139
|
if (db.databaseList[dbName]) {
|
|
122
|
-
db.databaseList[dbName].collection(table
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
)
|
|
132
|
-
)
|
|
140
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
141
|
+
const result = await collection.findOne(query)
|
|
142
|
+
resFmt(
|
|
143
|
+
null,
|
|
144
|
+
result && columnsToDecrypt && key && iv
|
|
145
|
+
? decrypt([result], columnsToDecrypt, key, iv)[0]
|
|
146
|
+
: result,
|
|
147
|
+
resolve,
|
|
148
|
+
reject
|
|
133
149
|
)
|
|
134
150
|
} else {
|
|
135
151
|
reject('No Connection')
|
|
136
152
|
}
|
|
137
153
|
}),
|
|
138
154
|
query: (dbName, table, query, columnsToDecrypt, key, iv) =>
|
|
139
|
-
new Promise((resolve, reject) => {
|
|
155
|
+
new Promise(async (resolve, reject) => {
|
|
140
156
|
if (db.databaseList[dbName]) {
|
|
141
|
-
db.databaseList[dbName].collection(table
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
158
|
+
const results = await collection.find(query)
|
|
159
|
+
const result = []
|
|
160
|
+
while (await results.hasNext()) {
|
|
161
|
+
result.push(await results.next())
|
|
162
|
+
}
|
|
163
|
+
resFmt(
|
|
164
|
+
null,
|
|
165
|
+
result && result.length > 0 && columnsToDecrypt && key && iv
|
|
166
|
+
? decrypt(result, columnsToDecrypt, key, iv)
|
|
167
|
+
: result,
|
|
168
|
+
resolve,
|
|
169
|
+
reject
|
|
154
170
|
)
|
|
155
171
|
} else {
|
|
156
172
|
reject('No Connection')
|
|
157
173
|
}
|
|
158
174
|
}),
|
|
159
175
|
querySort: (dbName, table, sort, query, columnsToDecrypt, key, iv) =>
|
|
160
|
-
new Promise((resolve, reject) => {
|
|
176
|
+
new Promise(async (resolve, reject) => {
|
|
161
177
|
if (db.databaseList[dbName]) {
|
|
162
|
-
db.databaseList[dbName].collection(table
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
)
|
|
178
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
179
|
+
const results = await (await collection.find(query)).sort(sort)
|
|
180
|
+
const result = []
|
|
181
|
+
while (await results.hasNext()) {
|
|
182
|
+
result.push(await results.next())
|
|
183
|
+
}
|
|
184
|
+
resFmt(
|
|
185
|
+
null,
|
|
186
|
+
result && result.length > 0 && columnsToDecrypt && key && iv
|
|
187
|
+
? decrypt(result, columnsToDecrypt, key, iv)
|
|
188
|
+
: result,
|
|
189
|
+
resolve,
|
|
190
|
+
reject
|
|
176
191
|
)
|
|
177
192
|
} else {
|
|
178
193
|
reject('No Connection')
|
|
@@ -188,23 +203,23 @@ const db = {
|
|
|
188
203
|
key,
|
|
189
204
|
iv
|
|
190
205
|
) =>
|
|
191
|
-
new Promise((resolve, reject) => {
|
|
206
|
+
new Promise(async (resolve, reject) => {
|
|
192
207
|
if (db.databaseList[dbName]) {
|
|
193
|
-
db.databaseList[dbName].collection(table
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
209
|
+
const results = await (
|
|
210
|
+
await (await collection.find(query)).limit(max)
|
|
211
|
+
).sort(sort)
|
|
212
|
+
const result = []
|
|
213
|
+
while (await results.hasNext()) {
|
|
214
|
+
result.push(await results.next())
|
|
215
|
+
}
|
|
216
|
+
resFmt(
|
|
217
|
+
null,
|
|
218
|
+
result && result.length > 0 && columnsToDecrypt && key && iv
|
|
219
|
+
? decrypt(result, columnsToDecrypt, key, iv)
|
|
220
|
+
: result,
|
|
221
|
+
resolve,
|
|
222
|
+
reject
|
|
208
223
|
)
|
|
209
224
|
} else {
|
|
210
225
|
reject('No Connection')
|
|
@@ -218,67 +233,116 @@ const db = {
|
|
|
218
233
|
joinToIDField,
|
|
219
234
|
joinedToElement,
|
|
220
235
|
sort,
|
|
221
|
-
query
|
|
236
|
+
query,
|
|
237
|
+
columnsToDecrypt,
|
|
238
|
+
key,
|
|
239
|
+
iv
|
|
222
240
|
) =>
|
|
223
|
-
new Promise((resolve, reject) => {
|
|
241
|
+
new Promise(async (resolve, reject) => {
|
|
224
242
|
if (db.databaseList[dbName]) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
243
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
244
|
+
resolve(
|
|
245
|
+
await db.aggregate(
|
|
246
|
+
dbName,
|
|
247
|
+
table,
|
|
248
|
+
query,
|
|
249
|
+
sort,
|
|
250
|
+
[
|
|
251
|
+
{
|
|
252
|
+
$lookup: {
|
|
253
|
+
from: joinTo,
|
|
254
|
+
localField: tableIDField,
|
|
255
|
+
foreignField: joinToIDField,
|
|
256
|
+
as: joinedToElement,
|
|
257
|
+
},
|
|
258
|
+
},
|
|
259
|
+
],
|
|
260
|
+
columnsToDecrypt,
|
|
261
|
+
key,
|
|
262
|
+
iv
|
|
263
|
+
)
|
|
264
|
+
)
|
|
237
265
|
} else {
|
|
238
266
|
reject('No Connection')
|
|
239
267
|
}
|
|
240
268
|
}),
|
|
241
|
-
joins: (
|
|
242
|
-
|
|
269
|
+
joins: (
|
|
270
|
+
dbName,
|
|
271
|
+
table,
|
|
272
|
+
joinedToList,
|
|
273
|
+
sort,
|
|
274
|
+
query,
|
|
275
|
+
columnsToDecrypt,
|
|
276
|
+
key,
|
|
277
|
+
iv
|
|
278
|
+
) =>
|
|
279
|
+
new Promise(async (resolve, reject) => {
|
|
243
280
|
if (db.databaseList[dbName]) {
|
|
244
|
-
db.databaseList[dbName].collection(table
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
281
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
282
|
+
let aggregrateList = []
|
|
283
|
+
for (let i = 0; i < joinedToList.length; i++) {
|
|
284
|
+
aggregrateList.push({
|
|
285
|
+
$lookup: {
|
|
286
|
+
from: joinedToList[i].to.name,
|
|
287
|
+
localField: joinedToList[i].from,
|
|
288
|
+
foreignField: joinedToList[i].to.id,
|
|
289
|
+
as: joinedToList[i].name,
|
|
290
|
+
},
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
resolve(
|
|
294
|
+
await db.aggregate(
|
|
295
|
+
dbName,
|
|
296
|
+
table,
|
|
297
|
+
query,
|
|
298
|
+
sort,
|
|
299
|
+
aggregrateList,
|
|
300
|
+
columnsToDecrypt,
|
|
301
|
+
key,
|
|
302
|
+
iv
|
|
303
|
+
)
|
|
304
|
+
)
|
|
260
305
|
} else {
|
|
261
306
|
reject('No Connection')
|
|
262
307
|
}
|
|
263
308
|
}),
|
|
264
|
-
aggregate: (
|
|
265
|
-
|
|
309
|
+
aggregate: (
|
|
310
|
+
dbName,
|
|
311
|
+
table,
|
|
312
|
+
query,
|
|
313
|
+
sort,
|
|
314
|
+
aggregates,
|
|
315
|
+
columnsToDecrypt,
|
|
316
|
+
key,
|
|
317
|
+
iv
|
|
318
|
+
) =>
|
|
319
|
+
new Promise(async (resolve, reject) => {
|
|
266
320
|
if (db.databaseList[dbName]) {
|
|
267
|
-
db.databaseList[dbName].collection(table
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
321
|
+
const collection = await db.databaseList[dbName].collection(table)
|
|
322
|
+
if (query) {
|
|
323
|
+
aggregates.unshift({ $match: query })
|
|
324
|
+
}
|
|
325
|
+
if (sort) {
|
|
326
|
+
aggregates.push({ $sort: sort })
|
|
327
|
+
}
|
|
328
|
+
const result = []
|
|
329
|
+
const results = collection.aggregate(aggregates)
|
|
330
|
+
for await (const row of results) {
|
|
331
|
+
result.push(row)
|
|
332
|
+
}
|
|
333
|
+
console.log({ columnsToDecrypt, key, iv })
|
|
334
|
+
resFmt(
|
|
335
|
+
null,
|
|
336
|
+
columnsToDecrypt && columnsToDecrypt.length > 0 && key && iv
|
|
337
|
+
? decrypt(result, columnsToDecrypt, key, iv)
|
|
338
|
+
: result,
|
|
339
|
+
resolve,
|
|
340
|
+
reject
|
|
341
|
+
)
|
|
278
342
|
} else {
|
|
279
343
|
reject('No Connection')
|
|
280
344
|
}
|
|
281
|
-
})
|
|
345
|
+
}),
|
|
282
346
|
}
|
|
283
347
|
|
|
284
348
|
module.exports = db
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gmongo",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "MongoDB Connection Class",
|
|
5
5
|
"main": "mongo.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,7 +17,9 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/GnomeGroup/gmongo#readme",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"
|
|
20
|
+
"eslint": "^8.39.0",
|
|
21
|
+
"mongodb": "^6.2.0",
|
|
22
|
+
"prettier": "^2.8.8"
|
|
21
23
|
},
|
|
22
24
|
"prettier": {
|
|
23
25
|
"singleQuote": true,
|
package/services/aes.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
const crypto = require('crypto')
|
|
2
2
|
|
|
3
|
+
const getBufferFromData = (bytes, data) => {
|
|
4
|
+
const newData = Buffer.alloc(bytes)
|
|
5
|
+
newData.write(data, 'utf-8')
|
|
6
|
+
return newData.toString('hex')
|
|
7
|
+
}
|
|
8
|
+
|
|
3
9
|
module.exports = {
|
|
4
10
|
encrypt: (key, iv, plainData) => {
|
|
5
11
|
const encipher = crypto.createCipheriv(
|
|
@@ -32,5 +38,9 @@ module.exports = {
|
|
|
32
38
|
.createHash('sha256')
|
|
33
39
|
.update(crypto.randomBytes(256))
|
|
34
40
|
.digest()
|
|
35
|
-
.toString('hex')
|
|
41
|
+
.toString('hex'),
|
|
42
|
+
format: {
|
|
43
|
+
iv: value => getBufferFromData(16, value),
|
|
44
|
+
key: value => getBufferFromData(32, value)
|
|
45
|
+
}
|
|
36
46
|
}
|
package/services/connect.js
CHANGED
|
@@ -1,51 +1,37 @@
|
|
|
1
1
|
const mongo = require('mongodb').MongoClient
|
|
2
2
|
|
|
3
|
-
module.exports = (
|
|
4
|
-
|
|
5
|
-
dbName,
|
|
6
|
-
ip,
|
|
7
|
-
port,
|
|
8
|
-
user,
|
|
9
|
-
pass,
|
|
10
|
-
x509,
|
|
11
|
-
timeoutInMS,
|
|
12
|
-
callback
|
|
13
|
-
) =>
|
|
14
|
-
new Promise(function(resolve, reject) {
|
|
3
|
+
module.exports = (isAtlas, dbName, ip, port, user, pass, x509, timeoutInMS) =>
|
|
4
|
+
new Promise(function (resolve, reject) {
|
|
15
5
|
let connectOptions = {
|
|
16
6
|
serverSelectionTimeoutMS: timeoutInMS
|
|
17
7
|
? parseInt(timeoutInMS)
|
|
18
8
|
: CFG.defaults.timeout,
|
|
19
|
-
useNewUrlParser: true,
|
|
20
|
-
useUnifiedTopology: true
|
|
21
9
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
mongo.connect(
|
|
27
|
-
'mongodb' +
|
|
10
|
+
try {
|
|
11
|
+
const url =
|
|
12
|
+
'mongodb' +
|
|
28
13
|
(isAtlas ? '+srv' : '') +
|
|
29
14
|
'://' +
|
|
30
|
-
(user ?
|
|
15
|
+
(user ? encodeURIComponent(user) : '') +
|
|
31
16
|
(user && pass ? ':' : '') +
|
|
32
|
-
(pass ?
|
|
17
|
+
(pass ? encodeURIComponent(pass) : '') +
|
|
33
18
|
(user || pass ? '@' : '') +
|
|
34
|
-
|
|
19
|
+
encodeURIComponent(ip) +
|
|
35
20
|
(isAtlas ? '' : ':' + parseInt(port).toString()) +
|
|
36
21
|
'/' +
|
|
37
|
-
|
|
22
|
+
encodeURIComponent(dbName) +
|
|
38
23
|
'?retryWrites=true&w=majority' +
|
|
39
|
-
(x509
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
24
|
+
(x509
|
|
25
|
+
? '&authMechanism=MONGODB-X509&tls=true&tlsCertificateKeyFile=' +
|
|
26
|
+
encodeURIComponent(x509)
|
|
27
|
+
: '&authMechanism=DEFAULT')
|
|
28
|
+
const dataBase = new mongo(url, connectOptions)
|
|
29
|
+
if (dataBase) {
|
|
30
|
+
const dbObject = dataBase.db(dbName)
|
|
31
|
+
dbObject.collection(dbName)
|
|
32
|
+
resolve(dbObject)
|
|
33
|
+
return
|
|
49
34
|
}
|
|
50
|
-
)
|
|
35
|
+
} catch (err) {}
|
|
36
|
+
reject()
|
|
51
37
|
})
|
package/services/objectId.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
const objectid = require('mongodb').
|
|
1
|
+
const objectid = require('mongodb').ObjectId
|
|
2
2
|
|
|
3
|
-
module.exports = id => objectid(id)
|
|
3
|
+
module.exports = (id) => new objectid(id)
|