molly-db 1.0.1 → 1.0.4
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/main.js +0 -1
- package/module/localDB.js +8 -30
- package/module/readline.js +208 -0
- package/module/streamDB.js +8 -29
- package/module/webDB.js +1 -19
- package/package.json +3 -3
- package/module/mongoDB.js +0 -224
package/main.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
const output = new Object();
|
2
2
|
|
3
3
|
try{ output.streamDB = require('./module/streamDB'); } catch(e) { /*console.log(e)*/ }
|
4
|
-
try{ output.mongoDB = require('./module/mongoDB'); } catch(e) { /*console.log(e)*/ }
|
5
4
|
try{ output.localDB = require('./module/localDB'); } catch(e) { /*console.log(e)*/ }
|
6
5
|
try{ output.webDB = require('./module/webDB'); } catch(e) { /*console.log(e)*/ }
|
7
6
|
|
package/module/localDB.js
CHANGED
@@ -78,11 +78,11 @@ class localDB{
|
|
78
78
|
events = new Object()
|
79
79
|
default = { offset: 0, length: 100 }
|
80
80
|
|
81
|
-
constructor(
|
82
|
-
if(
|
83
|
-
this.password =
|
81
|
+
constructor( object ){
|
82
|
+
if( object.pass ){
|
83
|
+
this.password = object.pass;
|
84
84
|
this.encrypted = true;
|
85
|
-
} this.path =
|
85
|
+
} this.path = object.path;
|
86
86
|
}
|
87
87
|
|
88
88
|
// TODO: Encription & Decription DATA //
|
@@ -127,27 +127,7 @@ class localDB{
|
|
127
127
|
} catch(e) { rej( e ); } });
|
128
128
|
}
|
129
129
|
|
130
|
-
|
131
|
-
loop( _table ){
|
132
|
-
return new Promise( (res,rej)=>{ try{
|
133
|
-
|
134
|
-
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
135
|
-
if( this.events.start ) this.events.start( _itr );
|
136
|
-
|
137
|
-
_itr.on( 'line',( encryptedLine )=>{ try{
|
138
|
-
const line = this.decrypt( encryptedLine );
|
139
|
-
if( this.events.data ) this.events.data( line,_i,_itr );
|
140
|
-
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
141
|
-
_i++; });
|
142
|
-
|
143
|
-
_itr.on( 'close',()=>{
|
144
|
-
if( this.events.close ) this.events.close( _itr );
|
145
|
-
});
|
146
|
-
|
147
|
-
} catch(e) { rej( e ); } });
|
148
|
-
}
|
149
|
-
|
150
|
-
find( _table, _target, _logic='AND', _config ){
|
130
|
+
find( _table, _target, _config ){
|
151
131
|
return new Promise( async(res,rej)=>{ try{
|
152
132
|
|
153
133
|
let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
|
@@ -166,9 +146,7 @@ class localDB{
|
|
166
146
|
}
|
167
147
|
|
168
148
|
const every = keys.every( (x)=>{return regex(x)} );
|
169
|
-
|
170
|
-
|
171
|
-
if( ( (/AND/gi).test(_logic) && every ) || ( (/OR/gi).test(_logic) && some ) ){
|
149
|
+
if( ( every ) ){
|
172
150
|
const cns = lineConstrain( _i, _cfg );
|
173
151
|
if( cns == 0 ) _res.push( data );
|
174
152
|
else if( cns == 1 ) _itr.close();
|
@@ -209,7 +187,7 @@ class localDB{
|
|
209
187
|
} catch(e) { rej( e ); } });
|
210
188
|
}
|
211
189
|
|
212
|
-
|
190
|
+
hash( _table, _hash ){
|
213
191
|
return new Promise( (res,rej)=>{ try{
|
214
192
|
|
215
193
|
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
@@ -434,4 +412,4 @@ class localDB{
|
|
434
412
|
}
|
435
413
|
|
436
414
|
//TODO: localDB Class ----------------------------------------------------------------------------//
|
437
|
-
module.exports = localDB;
|
415
|
+
module.exports = localDB;
|
@@ -0,0 +1,208 @@
|
|
1
|
+
const crypto = require('crypto-js');
|
2
|
+
const {Buffer} = require('buffer');
|
3
|
+
const output = new Object();
|
4
|
+
|
5
|
+
/* -------------------------------------------------------------------------------------------------------- */
|
6
|
+
|
7
|
+
const JsonFormatter = {
|
8
|
+
|
9
|
+
'stringify': function(cipherParams) {
|
10
|
+
var jsonObj = { ct: cipherParams.ciphertext.toString(crypto.enc.Base64) };
|
11
|
+
if (cipherParams.salt) jsonObj.s = cipherParams.salt.toString();
|
12
|
+
if (cipherParams.iv) jsonObj.iv = cipherParams.iv.toString();
|
13
|
+
return new Buffer.from(JSON.stringify(jsonObj)).toString('base64');
|
14
|
+
},
|
15
|
+
|
16
|
+
'parse': function(jsonStr) {
|
17
|
+
var jsonObj = JSON.parse( new Buffer.from(jsonStr,'base64').toString());
|
18
|
+
var cipherParams = crypto.lib.CipherParams.create({
|
19
|
+
ciphertext: crypto.enc.Base64.parse(jsonObj.ct)
|
20
|
+
});
|
21
|
+
if (jsonObj.iv) cipherParams.iv = crypto.enc.Hex.parse(jsonObj.iv);
|
22
|
+
if (jsonObj.s) cipherParams.salt = crypto.enc.Hex.parse(jsonObj.s);
|
23
|
+
return cipherParams;
|
24
|
+
}
|
25
|
+
|
26
|
+
};
|
27
|
+
|
28
|
+
/* -------------------------------------------------------------------------------------------------------- */
|
29
|
+
|
30
|
+
function slugify(str){
|
31
|
+
const map = {
|
32
|
+
'c' : 'ç',
|
33
|
+
'n' : 'ñ',
|
34
|
+
'e' : 'é|è|ê|ë',
|
35
|
+
'i' : 'í|ì|î|ï',
|
36
|
+
'u' : 'ú|ù|û|ü',
|
37
|
+
'o' : 'ó|ò|ô|õ|ö',
|
38
|
+
'a' : 'á|à|ã|â|ä',
|
39
|
+
'' : /\s+|\W+/,
|
40
|
+
};
|
41
|
+
for (var pattern in map) {
|
42
|
+
str=str.replace( new RegExp(map[pattern],'gi' ), pattern);
|
43
|
+
} return str.toLowerCase();
|
44
|
+
}
|
45
|
+
|
46
|
+
function encrypt( _message,_password ){
|
47
|
+
try{if( _password )
|
48
|
+
return crypto.AES.encrypt( _message,_password,{
|
49
|
+
format: JsonFormatter
|
50
|
+
}).toString(); return _message;
|
51
|
+
} catch(e) { return _message; }
|
52
|
+
}
|
53
|
+
|
54
|
+
function decrypt( _message,_password ){
|
55
|
+
try{if( _password )
|
56
|
+
return crypto.AES.decrypt( _message,_password,{
|
57
|
+
format: JsonFormatter
|
58
|
+
}).toString( crypto.enc.Utf8 );
|
59
|
+
return _message;
|
60
|
+
} catch(e) { return _message; }
|
61
|
+
}
|
62
|
+
|
63
|
+
function processData( _chunk ){
|
64
|
+
for( var i=_chunk.length; i--; ){
|
65
|
+
if( _chunk[i]==10 || _chunk[i]==13 )
|
66
|
+
return [
|
67
|
+
_chunk.slice(0,i),
|
68
|
+
_chunk.slice(i),
|
69
|
+
];
|
70
|
+
} return [ null, _chunk ];
|
71
|
+
}
|
72
|
+
|
73
|
+
/* -------------------------------------------------------------------------------------------------------- */
|
74
|
+
|
75
|
+
function dbDecrypt( _list,_self ){ return _list.map((x)=>{ return decrypt(x,_self?.password) }); }
|
76
|
+
|
77
|
+
function dbEncrypt( _list,_self ){ return _list.map((x)=>{ return encrypt(x,_self?.password) }); }
|
78
|
+
|
79
|
+
/* -------------------------------------------------------------------------------------------------------- */
|
80
|
+
|
81
|
+
output.readLine = ( _self,_stream,_config,_data )=>{
|
82
|
+
return new Promise((response,reject)=>{
|
83
|
+
|
84
|
+
let offset = 0;
|
85
|
+
const db = new Array();
|
86
|
+
let prcd = new Array();
|
87
|
+
let data = new Buffer(0);
|
88
|
+
|
89
|
+
_stream.on('data',async(chunk)=>{
|
90
|
+
|
91
|
+
data = Buffer.concat([ data,chunk ]);
|
92
|
+
prcd = processData( data );
|
93
|
+
data = prcd[1];
|
94
|
+
|
95
|
+
if( prcd[0] ){
|
96
|
+
|
97
|
+
const encrypted = prcd[0].toString().split('\n');
|
98
|
+
const decrypted = dbDecrypt(encrypted,_self);
|
99
|
+
|
100
|
+
const done = await _data( db,decrypted,_config );
|
101
|
+
offset += done.length;
|
102
|
+
|
103
|
+
if( db.length >= _config.length )
|
104
|
+
_stream.destroy();
|
105
|
+
else if ( offset > _config.offset && db.length != 0 )
|
106
|
+
db.push( ...done );
|
107
|
+
else if ( offset > _config.offset && db.length == 0 )
|
108
|
+
db.push( ...done.slice( offset - done.length - _config.offset - 1 ) );
|
109
|
+
|
110
|
+
}
|
111
|
+
|
112
|
+
});
|
113
|
+
|
114
|
+
_stream.on('close',async()=>{ response( db.slice( 0,_config.length ) ); });
|
115
|
+
|
116
|
+
});
|
117
|
+
}
|
118
|
+
|
119
|
+
output.list = ( _self,_stream,_config )=>{
|
120
|
+
|
121
|
+
const onData = ( db,data,_config )=>{
|
122
|
+
return new Promise((response,reject)=>{
|
123
|
+
try{
|
124
|
+
const index = data.findIndex((x)=>x=='');
|
125
|
+
data.splice( index,1 );
|
126
|
+
} catch(e) {} response(data);
|
127
|
+
});
|
128
|
+
}
|
129
|
+
|
130
|
+
return new Promise((response,reject)=>{
|
131
|
+
response( output.readLine( _self,_stream,_config,onData ));
|
132
|
+
});
|
133
|
+
|
134
|
+
}
|
135
|
+
|
136
|
+
output.match = ( _self,_stream,_target,_config )=>{
|
137
|
+
|
138
|
+
const onData = ( db,data,_config )=>{
|
139
|
+
return new Promise((response,reject)=>{
|
140
|
+
const done = new Array();
|
141
|
+
data.map(x=>{
|
142
|
+
try{
|
143
|
+
if( (new RegExp(slugify(_target),'gi')).test(slugify(x)) )
|
144
|
+
done.push(x);
|
145
|
+
} catch(e) {} });
|
146
|
+
response(done);
|
147
|
+
});
|
148
|
+
}
|
149
|
+
|
150
|
+
return new Promise((response,reject)=>{
|
151
|
+
response(output.readLine( _self,_stream,_config,onData ));
|
152
|
+
});
|
153
|
+
|
154
|
+
}
|
155
|
+
|
156
|
+
output.find = ( _self,_stream,_target,_config )=>{
|
157
|
+
|
158
|
+
const onData = ( db,data,_config )=>{
|
159
|
+
return new Promise((response,reject)=>{
|
160
|
+
const done = new Array();
|
161
|
+
data.map(x=>{
|
162
|
+
try{
|
163
|
+
const data = JSON.parse(x);
|
164
|
+
const cond = Object.keys(_target)
|
165
|
+
.every(y=>{
|
166
|
+
return (_target[y]).test(data[y]);
|
167
|
+
}); if( cond ) done.push(x);
|
168
|
+
} catch(e) {}
|
169
|
+
}); response(done);
|
170
|
+
});
|
171
|
+
}
|
172
|
+
|
173
|
+
return new Promise((response,reject)=>{
|
174
|
+
response(output.readLine( _self,_stream,_config,onData ));
|
175
|
+
});
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
output.hash = ( _self,_stream,_target )=>{
|
180
|
+
|
181
|
+
const onData = ( db,data,_config )=>{
|
182
|
+
return new Promise((response,reject)=>{
|
183
|
+
data.some((x)=>{
|
184
|
+
try{
|
185
|
+
const y = JSON.parse(x);
|
186
|
+
if( y.hash == _target )
|
187
|
+
response([x]);
|
188
|
+
} catch(e) {}
|
189
|
+
});
|
190
|
+
});
|
191
|
+
}
|
192
|
+
|
193
|
+
const _config = { offset:0, length:1 };
|
194
|
+
return new Promise((response,reject)=>{
|
195
|
+
response(output.readLine( _self,_stream,_config,onData ));
|
196
|
+
});
|
197
|
+
|
198
|
+
}
|
199
|
+
|
200
|
+
output.stream = ( _self,_stream,_config,_onData )=>{
|
201
|
+
return new Promise((response,reject)=>{
|
202
|
+
response(output.readLine( _self,_stream,_config,_onData ));
|
203
|
+
});
|
204
|
+
}
|
205
|
+
|
206
|
+
/* -------------------------------------------------------------------------------------------------------- */
|
207
|
+
|
208
|
+
module.exports = output;
|
package/module/streamDB.js
CHANGED
@@ -69,11 +69,11 @@ class streamDB{
|
|
69
69
|
events = new Object()
|
70
70
|
default = { offset: 0, length: 100 }
|
71
71
|
|
72
|
-
constructor(
|
73
|
-
if(
|
74
|
-
this.password =
|
72
|
+
constructor( object ){
|
73
|
+
if( object.pass ){
|
74
|
+
this.password = object.pass;
|
75
75
|
this.encrypted = true;
|
76
|
-
} this.path =
|
76
|
+
} this.path = object.path;
|
77
77
|
}
|
78
78
|
|
79
79
|
decrypt( _message,_password=this.password,_encrypted=this.encrypted ){
|
@@ -110,27 +110,7 @@ class streamDB{
|
|
110
110
|
} catch(e) { rej( e ); } });
|
111
111
|
}
|
112
112
|
|
113
|
-
|
114
|
-
loop( _table ){
|
115
|
-
return new Promise( (res,rej)=>{ try{
|
116
|
-
|
117
|
-
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
118
|
-
if( this.events.start ) this.events.start( _itr );
|
119
|
-
|
120
|
-
_itr.on( 'line',( encryptedLine )=>{ try{
|
121
|
-
const line = this.decrypt( encryptedLine );
|
122
|
-
if( this.events.data ) this.events.data( line,_i,_itr );
|
123
|
-
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
124
|
-
_i++; });
|
125
|
-
|
126
|
-
_itr.on( 'close',()=>{
|
127
|
-
if( this.events.close ) this.events.close( _itr );
|
128
|
-
});
|
129
|
-
|
130
|
-
} catch(e) { rej( e ); } });
|
131
|
-
}
|
132
|
-
|
133
|
-
find( _table, _target, _logic='AND', _config ){
|
113
|
+
find( _table, _target, _config ){
|
134
114
|
return new Promise( async(res,rej)=>{ try{
|
135
115
|
|
136
116
|
let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
|
@@ -149,9 +129,8 @@ class streamDB{
|
|
149
129
|
}
|
150
130
|
|
151
131
|
const every = keys.every( (x)=>{return regex(x)} );
|
152
|
-
const some = keys.some( (x)=>{return regex(x)} );
|
153
132
|
|
154
|
-
if(
|
133
|
+
if( every ){
|
155
134
|
const cns = lineConstrain( _i, _cfg );
|
156
135
|
if( cns == 0 ) _res.push( data );
|
157
136
|
else if( cns == 1 ) _itr.close();
|
@@ -192,7 +171,7 @@ class streamDB{
|
|
192
171
|
} catch(e) { rej( e ); } });
|
193
172
|
}
|
194
173
|
|
195
|
-
|
174
|
+
hash( _table, _hash ){
|
196
175
|
return new Promise( async(res,rej)=>{ try{
|
197
176
|
|
198
177
|
let { _i,_cfg,_itr,_res } = await init( _table,null,this );
|
@@ -218,4 +197,4 @@ class streamDB{
|
|
218
197
|
}
|
219
198
|
|
220
199
|
//TODO: localDB Class ----------------------------------------------------------------------------//
|
221
|
-
module.exports = streamDB;
|
200
|
+
module.exports = streamDB;
|
package/module/webDB.js
CHANGED
@@ -150,24 +150,6 @@ class createWebDB{
|
|
150
150
|
} catch(e) { rej( e ); } });
|
151
151
|
}
|
152
152
|
|
153
|
-
on( _event, _callback ){ this.events[_event] = _callback; }
|
154
|
-
loop( _table ){
|
155
|
-
return new Promise( (res,rej)=>{ try{
|
156
|
-
|
157
|
-
let { _i,_cfg,_itr,_res,_tmp,_path } = init( _table,null,this );
|
158
|
-
if( this.events.start ) this.events.start( _itr );
|
159
|
-
|
160
|
-
_itr.every( ( encryptedLine )=>{ try{
|
161
|
-
const line = this.decrypt( encryptedLine );
|
162
|
-
if( this.events.data ) this.events.data( line,_i,_itr );
|
163
|
-
} catch(e) { rej(`the db can be decripted: ${e}`) }
|
164
|
-
_i++; });
|
165
|
-
|
166
|
-
if( this.events.close ) this.events.close( _itr );
|
167
|
-
|
168
|
-
} catch(e) { rej( e ); } });
|
169
|
-
}
|
170
|
-
|
171
153
|
find( _table, _target, _logic='AND', _config ){
|
172
154
|
return new Promise( (res,rej)=>{ try{
|
173
155
|
|
@@ -392,4 +374,4 @@ class createWebDB{
|
|
392
374
|
}
|
393
375
|
|
394
376
|
// TODO: export --------------------------------------------------------------------------------------//
|
395
|
-
module.exports = createWebDB;
|
377
|
+
module.exports = createWebDB;
|
package/package.json
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
{
|
2
2
|
"license": "MIT",
|
3
3
|
"main": "main.js",
|
4
|
-
"version": "1.0.
|
4
|
+
"version": "1.0.4",
|
5
5
|
"name": "molly-db",
|
6
6
|
"author": "bececrazy",
|
7
7
|
"scripts": { "start": "node main" },
|
8
8
|
"repository": "https://github.com/EDBC-REPO-NPM/Molly-db",
|
9
|
-
"dependencies": { "axios": "^0.27.2", "
|
10
|
-
"description": "
|
9
|
+
"dependencies": { "axios": "^0.27.2", "crypto-js": "^4.1.1", "fs": "^0.0.1-security" },
|
10
|
+
"description": "Molly-db is a free and open source library for nodejs that allow you create a lightweight encrypted database using Json files",
|
11
11
|
"keywords": [ "json", "storage", "database", "datastorage", "web-database", "json-database", "encrypted-data", "web-local-storage", "encrypted-database", "web-encrypted-database" ]
|
12
12
|
}
|
package/module/mongoDB.js
DELETED
@@ -1,224 +0,0 @@
|
|
1
|
-
const mongodb = require('mongodb').MongoClient;
|
2
|
-
const crypto = require('crypto-js');
|
3
|
-
|
4
|
-
//TODO: mongoDB Class ----------------------------------------------------------------------------//
|
5
|
-
class mongoDB{
|
6
|
-
|
7
|
-
default = { offset: 0, length: 100, sort:{} }
|
8
|
-
|
9
|
-
constructor( _name,_URL ){
|
10
|
-
this.name = _name;
|
11
|
-
this.path = _URL;
|
12
|
-
}
|
13
|
-
|
14
|
-
// TODO: Searching functions //
|
15
|
-
list( _table, _config ){
|
16
|
-
return new Promise( (res,rej)=>{
|
17
|
-
mongodb.connect(this.path, (err, db)=>{
|
18
|
-
|
19
|
-
if (err) rej( err );
|
20
|
-
if( !_config ) _config = this.default;
|
21
|
-
|
22
|
-
db.db(this.name).collection(_table)
|
23
|
-
.find({})
|
24
|
-
.sort(_config.sort)
|
25
|
-
.skip( _config.offset )
|
26
|
-
.limit( _config.length )
|
27
|
-
.toArray((err,data)=>{
|
28
|
-
if (err) rej( err );
|
29
|
-
db.close(); res({
|
30
|
-
table: _table,
|
31
|
-
data: data
|
32
|
-
});
|
33
|
-
});
|
34
|
-
|
35
|
-
});
|
36
|
-
});
|
37
|
-
}
|
38
|
-
|
39
|
-
match( _table, _target, _index, _config ){
|
40
|
-
return new Promise( (res,rej)=>{
|
41
|
-
mongodb.connect(this.path, (err, db)=>{
|
42
|
-
|
43
|
-
if (err) rej( err );
|
44
|
-
if( !_config ) _config = this.default;
|
45
|
-
|
46
|
-
db.db(this.name).collection(_table)
|
47
|
-
.find({ $text: { $search:_target } })
|
48
|
-
.sort(_config.sort)
|
49
|
-
.skip( _config.offset )
|
50
|
-
.limit( _config.length )
|
51
|
-
.toArray((err,data)=>{
|
52
|
-
if (err) rej( err );
|
53
|
-
db.close(); res({
|
54
|
-
table: _table,
|
55
|
-
data: data
|
56
|
-
});
|
57
|
-
});
|
58
|
-
|
59
|
-
});
|
60
|
-
});
|
61
|
-
}
|
62
|
-
|
63
|
-
find( _table, _object, _config ){
|
64
|
-
return new Promise( (res,rej)=>{
|
65
|
-
mongodb.connect(this.path, (err, db)=>{
|
66
|
-
|
67
|
-
if (err) rej( err );
|
68
|
-
if( !_config ) _config = this.default;
|
69
|
-
|
70
|
-
db.db(this.name).collection(_table)
|
71
|
-
.find( _object )
|
72
|
-
.sort(_config.sort)
|
73
|
-
.skip( _config.offset )
|
74
|
-
.limit( _config.length )
|
75
|
-
.toArray((err,data)=>{
|
76
|
-
if (err) rej( err );
|
77
|
-
db.close(); res({
|
78
|
-
table: _table,
|
79
|
-
data: data,
|
80
|
-
});
|
81
|
-
});
|
82
|
-
|
83
|
-
});
|
84
|
-
});
|
85
|
-
}
|
86
|
-
|
87
|
-
findByHash( _table, _hash, _config ){
|
88
|
-
return new Promise( (res,rej)=>{
|
89
|
-
mongodb.connect(this.path, (err, db)=>{
|
90
|
-
|
91
|
-
if (err) rej( err );
|
92
|
-
if( !_config ) _config = this.default;
|
93
|
-
|
94
|
-
db.db(this.name).collection(_table)
|
95
|
-
.createIndex( _index )
|
96
|
-
.find( {hash:_hash} )
|
97
|
-
.sort(_config.sort)
|
98
|
-
.skip( _config.offset )
|
99
|
-
.limit( _config.length )
|
100
|
-
.toArray((err,data)=>{
|
101
|
-
if (err) rej( err );
|
102
|
-
db.close(); res({
|
103
|
-
table: _table,
|
104
|
-
data: data,
|
105
|
-
});
|
106
|
-
});
|
107
|
-
|
108
|
-
});
|
109
|
-
});
|
110
|
-
}
|
111
|
-
|
112
|
-
createNewHash( _object ){
|
113
|
-
try{
|
114
|
-
return _object.map( (item)=>{
|
115
|
-
item['_stamp'] = Date.now();
|
116
|
-
const _base = JSON.stringify( item );
|
117
|
-
if( !item.hash )
|
118
|
-
item.hash = crypto.SHA256( _base ).toString();
|
119
|
-
return item;
|
120
|
-
});
|
121
|
-
} catch(e) {
|
122
|
-
_object['_stamp'] = Date.now();
|
123
|
-
const _base = JSON.stringify( _object );
|
124
|
-
if( !_object.hash )
|
125
|
-
_object.hash = crypto.SHA256( _base ).toString();
|
126
|
-
return _object;
|
127
|
-
}
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
// TODO: Saving functions //
|
132
|
-
|
133
|
-
push( _table, ..._object ){
|
134
|
-
return new Promise( (res,rej)=>{
|
135
|
-
mongodb.connect(this.path, (err, db)=>{
|
136
|
-
|
137
|
-
if (err) rej( err );
|
138
|
-
_object = this.createNewHash( _object.flat() );
|
139
|
-
db.db(this.name).collection(_table)
|
140
|
-
.insertMany( _object.flat(),(err,data)=>{
|
141
|
-
if (err) rej( err );
|
142
|
-
db.close(); res({
|
143
|
-
table: _table,
|
144
|
-
data: data,
|
145
|
-
});
|
146
|
-
});
|
147
|
-
|
148
|
-
});
|
149
|
-
});
|
150
|
-
}
|
151
|
-
|
152
|
-
update( _table, _hash, _object ){
|
153
|
-
return new Promise( (res,rej)=>{
|
154
|
-
mongodb.connect(this.path, (err, db)=>{
|
155
|
-
|
156
|
-
if (err) rej( err );
|
157
|
-
_object = this.createNewHash( _object );
|
158
|
-
db.db(this.name).collection(_table)
|
159
|
-
.updateOne({ hash:_hash },{$set:_object},(err,data)=>{
|
160
|
-
if (err) rej( err );
|
161
|
-
db.close(); res({
|
162
|
-
table: _table,
|
163
|
-
data: data,
|
164
|
-
});
|
165
|
-
});
|
166
|
-
|
167
|
-
});
|
168
|
-
});
|
169
|
-
}
|
170
|
-
|
171
|
-
// TODO: Removing functions //
|
172
|
-
remove( _table, _hash ){
|
173
|
-
return new Promise( (res,rej)=>{
|
174
|
-
mongodb.connect(this.path, (err, db)=>{
|
175
|
-
|
176
|
-
if (err) rej( err );
|
177
|
-
db.db(this.name).collection(_table)
|
178
|
-
.deleteOne({ hash:_hash },(err,data)=>{
|
179
|
-
if (err) rej( err );
|
180
|
-
db.close(); res({
|
181
|
-
table: _table,
|
182
|
-
data: data,
|
183
|
-
});
|
184
|
-
});
|
185
|
-
|
186
|
-
});
|
187
|
-
});
|
188
|
-
}
|
189
|
-
|
190
|
-
addTable( _table ){
|
191
|
-
return new Promise( (res,rej)=>{
|
192
|
-
mongodb.connect(this.path, (err, db)=>{
|
193
|
-
if (err) rej( err );
|
194
|
-
db.db(this.name).createCollection(_table);
|
195
|
-
res({
|
196
|
-
table: _table,
|
197
|
-
data: data,
|
198
|
-
});
|
199
|
-
});
|
200
|
-
});
|
201
|
-
}
|
202
|
-
|
203
|
-
removeTable( _table ){
|
204
|
-
return new Promise( (res,rej)=>{
|
205
|
-
mongodb.connect(this.path, (err, db)=>{
|
206
|
-
|
207
|
-
if (err) rej( err );
|
208
|
-
db.db(this.name).collection(_table)
|
209
|
-
.drop((err, delOK)=>{
|
210
|
-
if (err) rej( err );
|
211
|
-
db.close(); res({
|
212
|
-
table: _table,
|
213
|
-
data: data,
|
214
|
-
});
|
215
|
-
});
|
216
|
-
|
217
|
-
});
|
218
|
-
});
|
219
|
-
}
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
//TODO: localDB Class ----------------------------------------------------------------------------//
|
224
|
-
module.exports = mongoDB;
|