molly-db 1.0.1

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.
@@ -0,0 +1,224 @@
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;
@@ -0,0 +1,221 @@
1
+
2
+ const readline = require('readline');
3
+ const crypto = require('crypto-js');
4
+ const axios = require('axios');
5
+ const fs = require('fs');
6
+
7
+ //TODO: Function ---------------------------------------------------------------------------------//
8
+ function slugify(str) {
9
+ const map = {
10
+ 'o' : 'ó|ò|ô|õ|ö|Ó|Ò|Ô|Õ|Ö',
11
+ 'a' : 'á|à|ã|â|ä|À|Á|Ã|Â|Ä',
12
+ 'e' : 'é|è|ê|ë|É|È|Ê|Ë',
13
+ 'i' : 'í|ì|î|ï|Í|Ì|Î|Ï',
14
+ 'u' : 'ú|ù|û|ü|Ú|Ù|Û|Ü',
15
+ 'c' : 'ç|Ç','n':'ñ|Ñ',
16
+ '' : /\s+|\W+/,
17
+ };
18
+ for (var pattern in map) {
19
+ str=str.replace( new RegExp(map[pattern],'gi' ), pattern);
20
+ } return str.toLowerCase();
21
+ }
22
+
23
+ const JsonFormatter = {
24
+ 'stringify': function(cipherParams) {
25
+ var jsonObj = { ct: cipherParams.ciphertext.toString(crypto.enc.Base64) };
26
+ if (cipherParams.salt) jsonObj.s = cipherParams.salt.toString();
27
+ if (cipherParams.iv) jsonObj.iv = cipherParams.iv.toString();
28
+ return new Buffer(JSON.stringify(jsonObj)).toString('base64');
29
+ },
30
+
31
+ 'parse': function(jsonStr) {
32
+ var jsonObj = JSON.parse( new Buffer(jsonStr,'base64').toString('UTF-8'));
33
+ var cipherParams = crypto.lib.CipherParams.create({
34
+ ciphertext: crypto.enc.Base64.parse(jsonObj.ct)
35
+ });
36
+ if (jsonObj.iv) cipherParams.iv = crypto.enc.Hex.parse(jsonObj.iv);
37
+ if (jsonObj.s) cipherParams.salt = crypto.enc.Hex.parse(jsonObj.s);
38
+ return cipherParams;
39
+ }
40
+ };
41
+
42
+ //TODO: Optimization FUnctions -------------------------------------------------------------------//
43
+
44
+ const init = function( _table,_config,_self ){
45
+ return new Promise( (res,rej)=>{
46
+ axios.get(`${_self.path}/${_table}.json`,{responseType:'stream'})
47
+ .then( ({data})=>{
48
+ res({
49
+ _itr: readline.createInterface({ input: data }),
50
+ _cfg: !_config ? _self.default : _config,
51
+ _res: new Array(),
52
+ _i: 0,
53
+ });
54
+ }) .catch( e=>rej() );
55
+ });
56
+ };
57
+
58
+ const lineConstrain = function( _i,_config ){
59
+ if( _i >= parseInt(_config.length)+parseInt(_config.offset) ) return 1;
60
+ else if ( _i < parseInt(_config.offset) ) return -1
61
+ else return 0;
62
+ }
63
+
64
+ //TODO: localDB Class ----------------------------------------------------------------------------//
65
+
66
+ class streamDB{
67
+
68
+ encrypted = false
69
+ events = new Object()
70
+ default = { offset: 0, length: 100 }
71
+
72
+ constructor( _path, _password ){
73
+ if( _password ){
74
+ this.password = _password;
75
+ this.encrypted = true;
76
+ } this.path = _path;
77
+ }
78
+
79
+ decrypt( _message,_password=this.password,_encrypted=this.encrypted ){
80
+ try{
81
+ if( _encrypted )
82
+ return crypto.AES.decrypt( _message,_password,{
83
+ format: JsonFormatter
84
+ }).toString( crypto.enc.Utf8 );
85
+ return _message;
86
+ } catch(e) {
87
+ return _message;
88
+ }
89
+ }
90
+
91
+ // TODO: Searching functions //
92
+ list( _table, _config ){
93
+ return new Promise( async(res,rej)=>{ try{
94
+
95
+ let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
96
+
97
+ _itr.on( 'line',( encryptedLine )=>{ try{
98
+ const line = this.decrypt( encryptedLine );
99
+ const cns = lineConstrain( _i, _cfg );
100
+ if( cns == 0 ) { _res.push( JSON.parse( line ) );
101
+ } else if( cns == 1 ) _itr.close();
102
+ } catch(e) { rej(`the db can be decripted: ${e}`) }
103
+ _i++; });
104
+
105
+ _itr.on( 'close',()=>{ res({
106
+ table:_table,
107
+ data:_res,
108
+ }); });
109
+
110
+ } catch(e) { rej( e ); } });
111
+ }
112
+
113
+ on( _event, _callback ){ this.events[_event] = _callback; }
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 ){
134
+ return new Promise( async(res,rej)=>{ try{
135
+
136
+ let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
137
+
138
+ _itr.on( 'line',( encryptedLine )=>{
139
+ try{
140
+ const line = this.decrypt( encryptedLine );
141
+ const keys = Object.keys( _target );
142
+ const data = JSON.parse( line );
143
+
144
+ const regex = ( x )=>{
145
+ const target = slugify(_target[x].toString());
146
+ const info = slugify(data[x].toString());
147
+ const regex = new RegExp(target,'gi');
148
+ return regex.test(info);
149
+ }
150
+
151
+ const every = keys.every( (x)=>{return regex(x)} );
152
+ const some = keys.some( (x)=>{return regex(x)} );
153
+
154
+ if( ( (/AND/gi).test(_logic) && every ) || ( (/OR/gi).test(_logic) && some ) ){
155
+ const cns = lineConstrain( _i, _cfg );
156
+ if( cns == 0 ) _res.push( data );
157
+ else if( cns == 1 ) _itr.close();
158
+ _i++;}
159
+
160
+ } catch(e) { rej(`the db can be decripted: ${e}`) }
161
+ });
162
+
163
+ _itr.on( 'close',()=>{ res({
164
+ table:_table,
165
+ data:_res,
166
+ }); });
167
+
168
+ } catch(e) { rej( e ); } });
169
+ }
170
+
171
+ match( _table,_match,_config ){
172
+ return new Promise( async(res,rej)=>{ try{
173
+
174
+ let { _i,_cfg,_itr,_res } = await init( _table,_config,this );
175
+
176
+ _itr.on( 'line',( encryptedLine )=>{ try{
177
+ const regex = new RegExp(slugify(_match),'gi');
178
+ const line = this.decrypt( encryptedLine );
179
+ if( regex.test(slugify(line)) ){
180
+ const cns = lineConstrain( _i, _cfg );
181
+ if( cns == 0 ) _res.push( JSON.parse( line ) );
182
+ else if( cns == 1 ) _itr.close();
183
+ _i++;}
184
+ } catch(e) { rej(`the db can be decripted: ${e}`) }
185
+ });
186
+
187
+ _itr.on( 'close',()=>{ res({
188
+ table:_table,
189
+ data:_res,
190
+ }); });
191
+
192
+ } catch(e) { rej( e ); } });
193
+ }
194
+
195
+ findByHash( _table, _hash ){
196
+ return new Promise( async(res,rej)=>{ try{
197
+
198
+ let { _i,_cfg,_itr,_res } = await init( _table,null,this );
199
+
200
+ _itr.on( 'line',( encryptedLine )=>{ try{
201
+ const line = this.decrypt( encryptedLine );
202
+ const data = JSON.parse( line );
203
+ if( data.hash == _hash ){
204
+ _res.push( data );
205
+ _itr.close();
206
+ }
207
+ } catch(e) { rej(`the db can be decripted: ${e}`) }
208
+ });
209
+
210
+ _itr.on( 'close',()=>{ res({
211
+ table:_table,
212
+ data:_res,
213
+ }); });
214
+
215
+ } catch(e) { rej( e ); } });
216
+ }
217
+
218
+ }
219
+
220
+ //TODO: localDB Class ----------------------------------------------------------------------------//
221
+ module.exports = streamDB;