molly-db 1.0.9 → 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- package/main.js +3 -3
- package/module/_init_.js +3 -5
- package/module/_server_.js +56 -48
- package/module/_worker_.js +2 -6
- package/package.json +1 -1
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
@@ -58,12 +58,10 @@ function fillDB( _db, _table, _path ){
|
|
58
58
|
|
59
59
|
} catch(e) {
|
60
60
|
|
61
|
-
|
62
|
-
db._buff_ = fs.readFileSync( path );
|
61
|
+
db._init_ = { DB:[] };
|
63
62
|
db._path_ = query.path;
|
64
|
-
|
65
|
-
|
66
|
-
fs.writeFileSync( path,db._init_ );
|
63
|
+
const path = `${query.path}/_init_.json`;
|
64
|
+
fs.writeFileSync( path,JSON.stringify(db._init_) );
|
67
65
|
|
68
66
|
} response();
|
69
67
|
})();
|
package/module/_server_.js
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
const api = url.parse( req.url,true );
|
3
3
|
let params = api.query;
|
4
4
|
let body = undefined;
|
5
|
+
let parse = api;
|
5
6
|
|
6
7
|
/* --------------------------------------------------------------------------------------- */
|
7
8
|
|
@@ -18,21 +19,35 @@ function json( _data ){
|
|
18
19
|
|
19
20
|
/* --------------------------------------------------------------------------------------- */
|
20
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
|
+
|
21
41
|
function getBody(){
|
22
42
|
return new Promise((response,reject)=>{
|
23
43
|
try{
|
24
|
-
if( req.method == 'POST' ){
|
44
|
+
if( req.method == 'POST' || (/push|unshift|splice|update/).test(api.pathname) ){
|
25
45
|
const data = new Array();
|
26
46
|
req.on('data',(chunk)=>{ data.push(chunk); });
|
27
47
|
req.on('close',()=>{ try{
|
28
|
-
const buff = Buffer.concat(data)
|
29
|
-
const
|
30
|
-
|
31
|
-
|
32
|
-
if( !object.hash )
|
33
|
-
object.hash = crypto.hash( date,0 );
|
34
|
-
|
35
|
-
response(JSON.stringify(object));
|
48
|
+
const buff = Buffer.concat(data);
|
49
|
+
const json = JSON.parse(buff);
|
50
|
+
response( bodyParser(json) );
|
36
51
|
} catch(e) { response(false) } });
|
37
52
|
} else { response(true) }
|
38
53
|
} catch(e) { response(false) }
|
@@ -66,8 +81,14 @@ function validate( _params ){
|
|
66
81
|
|
67
82
|
let validator = false;
|
68
83
|
|
69
|
-
const vtb = (key)=>{ return db._init_.DB.some(x=>{ return x.tables.join().match(key); }) }
|
70
84
|
const vdb = (key)=>{ return db._init_.DB.some(x=>{ return x.name == key; }) }
|
85
|
+
const vtb = (key)=>{ return db._init_.DB.some(x=>{ return x.tables.join().match(key); }) }
|
86
|
+
|
87
|
+
validator = [
|
88
|
+
[ !_params?.offset, '_params.offset = 0' ],
|
89
|
+
[ !_params?.target, '_params.target = ""' ],
|
90
|
+
[ !_params?.length, '_params.length = 100' ],
|
91
|
+
].every(x=>{ if(x[0]) eval(x[1]); return true; });
|
71
92
|
|
72
93
|
validator = [
|
73
94
|
[!body, {status:'error',message:'invalid data'}],
|
@@ -75,16 +96,12 @@ function validate( _params ){
|
|
75
96
|
[!_params?.table, {status:'error',message:'no table name added'}]
|
76
97
|
].some(x=>{ if(x[0]) reject(x[1]); return x[0];}); if(validator) return 0;
|
77
98
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
[ !_params.offset, '_params.offset = 0' ],
|
85
|
-
[ !_params.target, '_params.target = ""' ],
|
86
|
-
[ !_params.length, '_params.length = 100' ],
|
87
|
-
].map(x=>{ if(x[0]) eval(x[1]) });
|
99
|
+
if( !(/table|db/gi).test(parse.pathname) ){
|
100
|
+
validator = [
|
101
|
+
[!vdb(_params?.db), {status:'error',message:`no db called ${_params.db} exist`}],
|
102
|
+
[!vtb(_params?.table), {status:'error',message:`no table called ${_params.table} exist`}]
|
103
|
+
].some(x=>{ if(x[0]) reject(x[1]); return x[0];}); if(validator) return 0;
|
104
|
+
}
|
88
105
|
|
89
106
|
response(_params);
|
90
107
|
|
@@ -136,7 +153,7 @@ function pop( _params ){
|
|
136
153
|
|
137
154
|
async function push( _params ){
|
138
155
|
|
139
|
-
db[_params.db][_params.table].push( body );
|
156
|
+
db[_params.db][_params.table].push( ...body );
|
140
157
|
|
141
158
|
save( _params ); return [{
|
142
159
|
database: _params.db,
|
@@ -149,7 +166,7 @@ async function push( _params ){
|
|
149
166
|
async function splice( _params ){
|
150
167
|
|
151
168
|
db[_params.db][_params.table].splice(
|
152
|
-
_params.offset,_params.length
|
169
|
+
_params.offset,_params.length,...body
|
153
170
|
);
|
154
171
|
|
155
172
|
save( _params ); return [{
|
@@ -161,7 +178,7 @@ async function splice( _params ){
|
|
161
178
|
|
162
179
|
async function unshift( _params ){
|
163
180
|
|
164
|
-
db[_params.db][_params.table].unshift( body );
|
181
|
+
db[_params.db][_params.table].unshift( ...body );
|
165
182
|
|
166
183
|
save( _params ); return [{
|
167
184
|
database: _params.db,
|
@@ -180,7 +197,7 @@ async function update( _params ){
|
|
180
197
|
});
|
181
198
|
|
182
199
|
if( !(index<0) )
|
183
|
-
db[_params.db][_params.table].splice( index,1
|
200
|
+
db[_params.db][_params.table].splice( index,1,...body );
|
184
201
|
|
185
202
|
save( _params ); return [{
|
186
203
|
database: _params.db,
|
@@ -206,32 +223,17 @@ async function remove( _params ){
|
|
206
223
|
}];
|
207
224
|
}
|
208
225
|
|
209
|
-
async function save( _params ){
|
210
|
-
|
211
|
-
modifyDB( _params.db,_params.table );
|
212
|
-
return [{
|
213
|
-
database: _params.db,
|
214
|
-
table: _params.table,
|
215
|
-
status: 'saved'
|
216
|
-
}];
|
217
|
-
}
|
218
|
-
|
219
226
|
/* --------------------------------------------------------------------------------------- */
|
220
227
|
|
221
228
|
function addDB( _params ){
|
222
229
|
try{
|
223
230
|
|
224
|
-
const init = `${query.path}/_init_.json`;
|
225
|
-
|
226
231
|
db._init_.DB.push({
|
232
|
+
tables: [],
|
227
233
|
name: _params.db,
|
228
|
-
|
229
|
-
});
|
230
|
-
|
231
|
-
db[_params.db] = new Array();
|
232
|
-
fs.writeFileSync( init,JSON.stringify(db._init_) );
|
234
|
+
}); db[_params.db] = new Array();
|
233
235
|
|
234
|
-
return [{
|
236
|
+
save( _params ); return [{
|
235
237
|
database: _params.db,
|
236
238
|
status: 'DB added'
|
237
239
|
}];
|
@@ -242,7 +244,6 @@ function addDB( _params ){
|
|
242
244
|
function removeDB( _params ){
|
243
245
|
try{
|
244
246
|
|
245
|
-
const init = `${query.path}/_init_.json`;
|
246
247
|
const i = db._init_.DB.findIndex(x=>{
|
247
248
|
return x.name == _params.db
|
248
249
|
});
|
@@ -251,11 +252,9 @@ function removeDB( _params ){
|
|
251
252
|
const path = `${query.path}/${x}.json`;
|
252
253
|
fs.unlinkSync(path);
|
253
254
|
}); db._init_.DB.splice(i,1);
|
254
|
-
|
255
255
|
db[_params.db] = new Array();
|
256
|
-
fs.writeFileSync( init,JSON.stringify(db._init_) );
|
257
256
|
|
258
|
-
return [{
|
257
|
+
save( _params ); return [{
|
259
258
|
database: _params.db,
|
260
259
|
table: _params.table,
|
261
260
|
status: 'DB deleted'
|
@@ -300,7 +299,7 @@ function addTable( _params ){
|
|
300
299
|
|
301
300
|
db[_params.db][_params.table] = new Array();
|
302
301
|
|
303
|
-
return [{
|
302
|
+
save( _params ); return [{
|
304
303
|
database: _params.db,
|
305
304
|
table: _params.table,
|
306
305
|
status: 'table added'
|
@@ -321,7 +320,7 @@ function removeTable( _params ){
|
|
321
320
|
db._init_.DB[i].tables.splice(j,1);
|
322
321
|
delete db[_params.db][_params.table];
|
323
322
|
|
324
|
-
return [{
|
323
|
+
save( _params ); return [{
|
325
324
|
database: _params.db,
|
326
325
|
table: _params.table,
|
327
326
|
status: 'table removed'
|
@@ -338,6 +337,15 @@ function refresh( _params ){
|
|
338
337
|
});
|
339
338
|
}
|
340
339
|
|
340
|
+
function save( _params ){
|
341
|
+
modifyDB( _params.db,_params.table );
|
342
|
+
return [{
|
343
|
+
database: _params.db,
|
344
|
+
table: _params.table,
|
345
|
+
status: 'saved'
|
346
|
+
}];
|
347
|
+
}
|
348
|
+
|
341
349
|
/* --------------------------------------------------------------------------------------- */
|
342
350
|
|
343
351
|
(async ()=>{
|
package/module/_worker_.js
CHANGED
@@ -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
|
-
|
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
|
});
|