molly-db 1.0.10 → 1.0.13

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 CHANGED
@@ -33,9 +33,9 @@ class molly_db{
33
33
  }
34
34
  );
35
35
 
36
- this.worker.on('exit',(err)=>{ console.log(err); reject() });
37
- this.worker.on('error',(err)=>{ console.log(err); reject() });
38
- this.worker.on('message',(msg)=>{ console.log(msg); response() });
36
+ this.worker.on('exit',(err)=>{ console.log(err); reject(err) });
37
+ this.worker.on('error',(err)=>{ console.log(err); reject(err) });
38
+ this.worker.on('message',(msg)=>{ console.log(msg); response(msg) });
39
39
  });
40
40
  }
41
41
 
package/module/_init_.js CHANGED
@@ -9,16 +9,18 @@ function fillDB( _db, _table, _path ){
9
9
  _itr = readline.createInterface({
10
10
  input: stream.data
11
11
  });
12
+ }
12
13
 
13
- } else
14
+ else if( fs.existsSync(_path) )
14
15
  _itr = readline.createInterface({
15
16
  input: fs.createReadStream(_path)
16
17
  });
18
+
19
+ else return response();
17
20
 
18
- _itr.on('close',()=>{ response() });
19
21
  _itr.on('line',(line)=>{
20
22
  db[_db][_table].push(crypto.decrypt( line,query.pass ));
21
- });
23
+ }); _itr.on('close',()=>{ response() });
22
24
 
23
25
  });
24
26
  }
@@ -50,20 +52,17 @@ function fillDB( _db, _table, _path ){
50
52
  for( var j in DB.tables ){
51
53
  const table = DB.tables[j];
52
54
  const path = `${db._path_}/${table}.json`;
53
- db[name][table] = new Array();
54
- await fillDB( name,table,path );
55
+ db[name][table] = new Array(); await fillDB( name,table,path );
55
56
  }
56
57
 
57
58
  }
58
59
 
59
60
  } catch(e) {
60
61
 
61
- const path = `${query.path}/_init_.json`;
62
- db._buff_ = fs.readFileSync( path );
62
+ db._init_ = { DB:[] };
63
63
  db._path_ = query.path;
64
- db._init_ = { DB:[] }
65
-
66
- fs.writeFileSync( path,db._init_ );
64
+ const path = `${query.path}/_init_.json`;
65
+ fs.writeFileSync( path,JSON.stringify(db._init_) );
67
66
 
68
67
  } response();
69
68
  })();
@@ -19,21 +19,35 @@ function json( _data ){
19
19
 
20
20
  /* --------------------------------------------------------------------------------------- */
21
21
 
22
+ function bodyParser( _data ){
23
+
24
+ const date = Date.now();
25
+ const result = new Array();
26
+
27
+ if( typeof _data.length == 'number' ){
28
+ for( var i in _data ){
29
+ if(!_data[i]?.hash)
30
+ _data[i].hash = crypto.hash( date,0 );
31
+ result.push(JSON.stringify(_data[i]));
32
+ }
33
+ } else {
34
+ if(!_data?.hash)
35
+ _data.hash = crypto.hash( date,0 );
36
+ result.push(JSON.stringify(_data));
37
+ } return result;
38
+
39
+ }
40
+
22
41
  function getBody(){
23
42
  return new Promise((response,reject)=>{
24
43
  try{
25
- if( req.method == 'POST' ){
44
+ if( req.method == 'POST' || (/push|unshift|splice|update/).test(api.pathname) ){
26
45
  const data = new Array();
27
46
  req.on('data',(chunk)=>{ data.push(chunk); });
28
47
  req.on('close',()=>{ try{
29
- const buff = Buffer.concat(data).toString();
30
- const object = JSON.parse(buff);
31
- const date = Date.now();
32
-
33
- if( !object.hash )
34
- object.hash = crypto.hash( date,0 );
35
-
36
- response(JSON.stringify(object));
48
+ const buff = Buffer.concat(data);
49
+ const json = JSON.parse(buff);
50
+ response( bodyParser(json) );
37
51
  } catch(e) { response(false) } });
38
52
  } else { response(true) }
39
53
  } catch(e) { response(false) }
@@ -71,9 +85,9 @@ function validate( _params ){
71
85
  const vtb = (key)=>{ return db._init_.DB.some(x=>{ return x.tables.join().match(key); }) }
72
86
 
73
87
  validator = [
74
- [ !_params.offset, '_params.offset = 0' ],
75
- [ !_params.target, '_params.target = ""' ],
76
- [ !_params.length, '_params.length = 100' ],
88
+ [ !_params?.offset, '_params.offset = 0' ],
89
+ [ !_params?.target, '_params.target = ""' ],
90
+ [ !_params?.length, '_params.length = 100' ],
77
91
  ].every(x=>{ if(x[0]) eval(x[1]); return true; });
78
92
 
79
93
  validator = [
@@ -137,9 +151,19 @@ function pop( _params ){
137
151
 
138
152
  /* --------------------------------------------------------------------------------------- */
139
153
 
154
+ function lengthOf( _params ){ return db[_params.db][_params.table].length; }
155
+ function indexOf( _params ){
156
+ return db[_params.db][_params.table].indexOf(x=>{
157
+ const regex = new RegExp(_params.target,'gi');
158
+ return regex.test(x);
159
+ });
160
+ }
161
+
162
+ /* --------------------------------------------------------------------------------------- */
163
+
140
164
  async function push( _params ){
141
165
 
142
- db[_params.db][_params.table].push( body );
166
+ db[_params.db][_params.table].push( ...body );
143
167
 
144
168
  save( _params ); return [{
145
169
  database: _params.db,
@@ -152,7 +176,7 @@ async function push( _params ){
152
176
  async function splice( _params ){
153
177
 
154
178
  db[_params.db][_params.table].splice(
155
- _params.offset,_params.length,body
179
+ _params.offset,_params.length,...body
156
180
  );
157
181
 
158
182
  save( _params ); return [{
@@ -164,7 +188,7 @@ async function splice( _params ){
164
188
 
165
189
  async function unshift( _params ){
166
190
 
167
- db[_params.db][_params.table].unshift( body );
191
+ db[_params.db][_params.table].unshift( ...body );
168
192
 
169
193
  save( _params ); return [{
170
194
  database: _params.db,
@@ -183,7 +207,7 @@ async function update( _params ){
183
207
  });
184
208
 
185
209
  if( !(index<0) )
186
- db[_params.db][_params.table].splice( index,1,body );
210
+ db[_params.db][_params.table].splice( index,1,...body );
187
211
 
188
212
  save( _params ); return [{
189
213
  database: _params.db,
@@ -209,32 +233,17 @@ async function remove( _params ){
209
233
  }];
210
234
  }
211
235
 
212
- async function save( _params ){
213
-
214
- modifyDB( _params.db,_params.table );
215
- return [{
216
- database: _params.db,
217
- table: _params.table,
218
- status: 'saved'
219
- }];
220
- }
221
-
222
236
  /* --------------------------------------------------------------------------------------- */
223
237
 
224
238
  function addDB( _params ){
225
239
  try{
226
240
 
227
- const init = `${query.path}/_init_.json`;
228
-
229
241
  db._init_.DB.push({
242
+ tables: [],
230
243
  name: _params.db,
231
- tables: []
232
- });
233
-
234
- db[_params.db] = new Array();
235
- fs.writeFileSync( init,JSON.stringify(db._init_) );
244
+ }); db[_params.db] = new Array();
236
245
 
237
- return [{
246
+ save( _params ); return [{
238
247
  database: _params.db,
239
248
  status: 'DB added'
240
249
  }];
@@ -245,7 +254,6 @@ function addDB( _params ){
245
254
  function removeDB( _params ){
246
255
  try{
247
256
 
248
- const init = `${query.path}/_init_.json`;
249
257
  const i = db._init_.DB.findIndex(x=>{
250
258
  return x.name == _params.db
251
259
  });
@@ -254,11 +262,9 @@ function removeDB( _params ){
254
262
  const path = `${query.path}/${x}.json`;
255
263
  fs.unlinkSync(path);
256
264
  }); db._init_.DB.splice(i,1);
257
-
258
265
  db[_params.db] = new Array();
259
- fs.writeFileSync( init,JSON.stringify(db._init_) );
260
266
 
261
- return [{
267
+ save( _params ); return [{
262
268
  database: _params.db,
263
269
  table: _params.table,
264
270
  status: 'DB deleted'
@@ -303,7 +309,7 @@ function addTable( _params ){
303
309
 
304
310
  db[_params.db][_params.table] = new Array();
305
311
 
306
- return [{
312
+ save( _params ); return [{
307
313
  database: _params.db,
308
314
  table: _params.table,
309
315
  status: 'table added'
@@ -324,7 +330,7 @@ function removeTable( _params ){
324
330
  db._init_.DB[i].tables.splice(j,1);
325
331
  delete db[_params.db][_params.table];
326
332
 
327
- return [{
333
+ save( _params ); return [{
328
334
  database: _params.db,
329
335
  table: _params.table,
330
336
  status: 'table removed'
@@ -341,6 +347,15 @@ function refresh( _params ){
341
347
  });
342
348
  }
343
349
 
350
+ function save( _params ){
351
+ modifyDB( _params.db,_params.table );
352
+ return [{
353
+ database: _params.db,
354
+ table: _params.table,
355
+ status: 'saved'
356
+ }];
357
+ }
358
+
344
359
  /* --------------------------------------------------------------------------------------- */
345
360
 
346
361
  (async ()=>{
@@ -355,6 +370,9 @@ function refresh( _params ){
355
370
  else if( api.pathname == '/match' ) json( await match(params) )
356
371
  else if( api.pathname == '/update' ) json( await update(params) )
357
372
 
373
+ else if( api.pathname == '/index' ) json( await indexOf(params) )
374
+ else if( api.pathname == '/lengt' ) json( await lengthOf(params) )
375
+
358
376
  /* Save Api */
359
377
  else if( api.pathname == '/save' ) json( await save(params) )
360
378
  else if( api.pathname == '/remove' ) json( await remove(params) )
@@ -32,14 +32,10 @@ function app(req,res){
32
32
  (()=>{
33
33
  try{
34
34
  http.createServer( app ).listen( query.port,()=>{
35
- console.log('molly-db cluster is running, please wait');
36
35
  _init_().then(()=>{
37
36
  worker.parentPort.postMessage({
38
- protocol: 'HTTP',
39
- status: 'started',
40
- workerID: process.pid,
41
- msg: 'molly-db is running',
42
- server: `http://localhost:${query.port}`,
37
+ protocol: 'HTTP', status: 'started',
38
+ workerID: process.pid, port: query.port,
43
39
  });
44
40
  }).catch(e=>{ process.exit(1); });
45
41
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "license": "MIT",
3
3
  "main": "main.js",
4
- "version": "1.0.10",
4
+ "version": "1.0.13",
5
5
  "name": "molly-db",
6
6
  "author": "bececrazy",
7
7
  "scripts": {