fkg 1.1.0 → 1.1.2
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/README.md +15 -25
- package/lib/index.js +96 -105
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -15,52 +15,42 @@ npm install fkg
|
|
|
15
15
|
|
|
16
16
|
```javascript
|
|
17
17
|
const fkg = require('fkg');
|
|
18
|
-
const db =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const db = fkg({
|
|
19
|
+
dir:'D:\\work', //数据储存位置,为空时不进行存储
|
|
20
|
+
key:'exgeg2233', //key不为空时,储存的数据会被加密
|
|
21
|
+
db:['user','name'] //db必须存在,且必须为数组
|
|
22
22
|
});
|
|
23
|
-
const user = db.table('user');
|
|
24
23
|
```
|
|
25
24
|
|
|
26
|
-
**3.
|
|
27
|
-
|
|
25
|
+
**3. Create user data**
|
|
28
26
|
```javascript
|
|
29
|
-
|
|
30
|
-
user.
|
|
27
|
+
const data = {name:'xxx'};
|
|
28
|
+
db.user.add('id_1',data); //数据储存在本地
|
|
29
|
+
db.user.set('id_2',data); //数据储存在缓存
|
|
31
30
|
```
|
|
32
31
|
|
|
33
32
|
**5. Get user data**
|
|
34
33
|
|
|
35
34
|
```javascript
|
|
36
|
-
user.list();
|
|
37
|
-
|
|
35
|
+
db.user.list(); //获取user表缓存和本地数据键值
|
|
36
|
+
```
|
|
38
37
|
|
|
39
38
|
**6. Get list data**
|
|
40
39
|
|
|
41
40
|
```javascript
|
|
42
|
-
user.get('id_1');
|
|
43
|
-
user.get('id_2');
|
|
41
|
+
db.user.get('id_1'); //获取user表中指定缓存
|
|
42
|
+
db.user.get('id_2'); //获取不到缓存,会获取本地数据
|
|
44
43
|
```
|
|
45
44
|
|
|
46
45
|
**7. Delete user data**
|
|
47
46
|
|
|
48
47
|
```javascript
|
|
49
|
-
user.
|
|
50
|
-
user.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
**8. Clear Data Table**
|
|
54
|
-
|
|
55
|
-
```javascript
|
|
56
|
-
user.clear();
|
|
48
|
+
db.user.clear(); //清空user表中所有的缓存
|
|
49
|
+
db.user.kill(); //清空user表中所有的本地数据
|
|
50
|
+
db.user.del('id_1'); //删除user表中指定缓存和本地数据
|
|
57
51
|
```
|
|
58
52
|
|
|
59
|
-
**9. Close server**
|
|
60
53
|
|
|
61
|
-
```javascript
|
|
62
|
-
db.close();
|
|
63
|
-
```
|
|
64
54
|
|
|
65
55
|
|
|
66
56
|
|
package/lib/index.js
CHANGED
|
@@ -1,117 +1,108 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if(this.dir) {
|
|
28
|
-
this.dirs[n] = link(this.dir,'./'+n);
|
|
29
|
-
if(!isDir(this.dirs[n])) addDir(this.dirs[n]);
|
|
2
|
+
module.exports = (arg)=>{
|
|
3
|
+
const fs = require('./utils/fs');
|
|
4
|
+
const {md5,ekey,dkey} = require('rrid');
|
|
5
|
+
const {str,obj,arr} = require('./utils/type');
|
|
6
|
+
if(arg&&obj(arg)) {
|
|
7
|
+
let io = {};
|
|
8
|
+
let dirs = {};
|
|
9
|
+
let datas = {};
|
|
10
|
+
if(arg.dir&&fs.isDir(arg.dir)) {
|
|
11
|
+
arg.dir = fs.link(arg.dir,'./data');
|
|
12
|
+
if(!fs.isDir(arg.dir)) fs.addDir(arg.dir);
|
|
13
|
+
}else{
|
|
14
|
+
delete arg.dir;
|
|
15
|
+
}
|
|
16
|
+
if(arg.db&&arr(arg.db)){
|
|
17
|
+
if(arg.db.length>0){
|
|
18
|
+
for (let i = 0;i < arg.db.length;i++) {
|
|
19
|
+
const n = arg.db[i];
|
|
20
|
+
if(!str(n)){
|
|
21
|
+
console.log(`The db.${n} is not string`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if(arg.dir) {
|
|
25
|
+
dirs[n] = fs.link(arg.dir,'./'+n);
|
|
26
|
+
if(!fs.isDir(dirs[n])) fs.addDir(dirs[n]);
|
|
30
27
|
}
|
|
31
28
|
};
|
|
29
|
+
}else{
|
|
30
|
+
console.log("The length of 'db' is not 0");
|
|
31
|
+
return;
|
|
32
32
|
}
|
|
33
|
+
}else{
|
|
34
|
+
console.log('The db is array');
|
|
35
|
+
return;
|
|
33
36
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if(that.dirs[n]){
|
|
60
|
-
const url = link(that.dirs[n],'./'+id);
|
|
61
|
-
if(!that.data[n][id]&&isFile(url)) {
|
|
62
|
-
let value = readJson(url);
|
|
63
|
-
if(value) {
|
|
64
|
-
if(that.key) value = JSON.parse(dkey(value,that.key+id));
|
|
65
|
-
that.data[n][id] = value;
|
|
66
|
-
}
|
|
37
|
+
if(arg.key&&str(arg.key)) {
|
|
38
|
+
arg.key = md5(arg.key+'fkg');
|
|
39
|
+
}else{
|
|
40
|
+
delete arg.key;
|
|
41
|
+
}
|
|
42
|
+
const def = (n) => {
|
|
43
|
+
return {
|
|
44
|
+
set(id,data){
|
|
45
|
+
if(!id||!str(id)||!data) return false;
|
|
46
|
+
datas[n][id] = data;
|
|
47
|
+
return true;
|
|
48
|
+
},
|
|
49
|
+
add(id,data){
|
|
50
|
+
if(!id||!str(id)||!data||!arg.dir) return false;
|
|
51
|
+
if(arg.key) data = ekey(JSON.stringify(data),arg.key+id);
|
|
52
|
+
return fs.addJson(fs.link(dirs[n],'./'+id),data);
|
|
53
|
+
},
|
|
54
|
+
get(id){
|
|
55
|
+
if(!id||!str(id)) return null;
|
|
56
|
+
let ids = datas[n][id];
|
|
57
|
+
if(!ids&&arg.dir){
|
|
58
|
+
const url = fs.link(dirs[n],'./'+id);
|
|
59
|
+
if(fs.isFile(url)){
|
|
60
|
+
ids = fs.readJson(url);
|
|
61
|
+
if(ids&&arg.key) ids = JSON.parse(dkey(ids,arg.key+id));
|
|
67
62
|
}
|
|
63
|
+
}
|
|
64
|
+
return ids;
|
|
65
|
+
},
|
|
66
|
+
list(){
|
|
67
|
+
let arr = Object.keys(datas[n]);
|
|
68
|
+
if(arg.dir) arr = [...arr,...fs.readdirSync(dirs[n])];
|
|
69
|
+
return arr;
|
|
70
|
+
},
|
|
71
|
+
del(id){
|
|
72
|
+
let res = true;
|
|
73
|
+
if(!id||!str(id)) return false;
|
|
74
|
+
if(datas[n][id]) delete datas[n][id];
|
|
75
|
+
if(arg.dir){
|
|
76
|
+
const url = fs.link(dirs[n],'./'+id);
|
|
77
|
+
if(fs.isFile(url)) res = fs.delFile(url);
|
|
78
|
+
if(!res) return res;
|
|
68
79
|
}
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
},
|
|
85
|
-
get(id){
|
|
86
|
-
const ids = that.data[n][id];
|
|
87
|
-
if(!ids&&that.dirs[n]){
|
|
88
|
-
const url = link(that.dirs[n],'./'+id);
|
|
89
|
-
if(isFile(url)){
|
|
90
|
-
let value = readJson(url);
|
|
91
|
-
if(value) {
|
|
92
|
-
if(that.key) value = JSON.parse(dkey(value,that.key+id));
|
|
93
|
-
that.data[n][id] = value;
|
|
94
|
-
}
|
|
80
|
+
return res;
|
|
81
|
+
},
|
|
82
|
+
clear(){
|
|
83
|
+
datas[n] = {}
|
|
84
|
+
return true;
|
|
85
|
+
},
|
|
86
|
+
kill(){
|
|
87
|
+
if(arg.dir){
|
|
88
|
+
const list = fs.readdirSync(dirs[n]);
|
|
89
|
+
for (let i = 0;i < list.length;i++) {
|
|
90
|
+
fs.delFile(fs.link(dirs[n],'./'+list[i]));
|
|
91
|
+
};
|
|
92
|
+
return true;
|
|
93
|
+
}else{
|
|
94
|
+
return false;
|
|
95
95
|
}
|
|
96
|
-
}
|
|
97
|
-
return that.data[n][id];
|
|
98
|
-
},
|
|
99
|
-
set(id,d){
|
|
100
|
-
that.data[n][id] = d;
|
|
101
|
-
if(that.dirs[n]) {
|
|
102
|
-
if(that.key) d = ekey(JSON.stringify(d),that.key+id);
|
|
103
|
-
addJson(link(that.dirs[n],'./'+id),d);
|
|
104
|
-
}
|
|
105
|
-
return true;
|
|
106
|
-
},
|
|
107
|
-
del(id){
|
|
108
|
-
if(that.dirs[n]&&that.data[n][id]){
|
|
109
|
-
delFile(link(that.dirs[n],'./'+id));
|
|
110
96
|
}
|
|
111
|
-
delete that.data[n][id];
|
|
112
|
-
return true;
|
|
113
97
|
}
|
|
114
98
|
}
|
|
99
|
+
for (let i = 0;i < arg.db.length;i++) {
|
|
100
|
+
const n = arg.db[i];
|
|
101
|
+
datas[n] = {};
|
|
102
|
+
io[n] = def(n);
|
|
103
|
+
};
|
|
104
|
+
return io;
|
|
105
|
+
}else{
|
|
106
|
+
console.log('Missing parameters');
|
|
115
107
|
}
|
|
116
|
-
}
|
|
117
|
-
module.exports = fkg;
|
|
108
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fkg",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Read and set memory key!",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"keywords": [
|
|
7
|
+
"db",
|
|
7
8
|
"key",
|
|
8
|
-
"
|
|
9
|
+
"disk",
|
|
10
|
+
"data",
|
|
11
|
+
"cache",
|
|
12
|
+
"database"
|
|
9
13
|
],
|
|
10
14
|
"author": "weduu",
|
|
11
15
|
"license": "MIT",
|