fkg 1.1.0 → 1.1.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.
- package/README.md +10 -10
- package/lib/index.js +45 -70
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,26 +15,26 @@ npm install fkg
|
|
|
15
15
|
|
|
16
16
|
```javascript
|
|
17
17
|
const fkg = require('fkg');
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
});
|
|
23
|
-
const user = db.
|
|
18
|
+
const dir = 'C:\\work'; //数据储存位置,为空时不进行本地存储
|
|
19
|
+
const key = 'f31d0353cdf'; //key不为空时,数据会被加密储存
|
|
20
|
+
// const db = new fkg();
|
|
21
|
+
// const db = new fkg(dir);
|
|
22
|
+
const db = new fkg({dir,key});
|
|
23
|
+
const user = db.io('user');
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
**3. Add user data**
|
|
27
27
|
|
|
28
28
|
```javascript
|
|
29
|
-
|
|
30
|
-
user.set('id_2',{account:"
|
|
29
|
+
uset.add('id_1',{account:"wabngwu",password:"******"}); //数据储存在本地
|
|
30
|
+
user.set('id_2',{account:"zhangsan",password:"******"}); //数据储存在缓存
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
**5. Get user data**
|
|
34
34
|
|
|
35
35
|
```javascript
|
|
36
|
-
user.list();
|
|
37
|
-
|
|
36
|
+
user.list(); //获取本地和缓存数据键值
|
|
37
|
+
```
|
|
38
38
|
|
|
39
39
|
**6. Get list data**
|
|
40
40
|
|
package/lib/index.js
CHANGED
|
@@ -10,26 +10,17 @@ const {
|
|
|
10
10
|
readdirSync
|
|
11
11
|
} = require('./utils/fs');
|
|
12
12
|
const {md5,ekey,dkey} = require('rrid');
|
|
13
|
-
const {str,obj
|
|
13
|
+
const {str,obj} = require('./utils/type');
|
|
14
14
|
class fkg {
|
|
15
15
|
constructor(arg) {
|
|
16
16
|
this.close();
|
|
17
|
-
if(arg
|
|
18
|
-
if(arg
|
|
19
|
-
|
|
20
|
-
if(
|
|
21
|
-
|
|
22
|
-
if(arg.key&&str(arg.key)) this.key = md5(arg.key+'fkg');
|
|
23
|
-
if(arg.dbName&&arr(arg.dbName)&&arr.length>0){
|
|
24
|
-
for (let i = 0;i < arg.dbName.length;i++) {
|
|
25
|
-
const n = arg.dbName[i];
|
|
26
|
-
this.data[n] = {};
|
|
27
|
-
if(this.dir) {
|
|
28
|
-
this.dirs[n] = link(this.dir,'./'+n);
|
|
29
|
-
if(!isDir(this.dirs[n])) addDir(this.dirs[n]);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
17
|
+
if(arg) {
|
|
18
|
+
if(str(arg)&&isDir(arg)) this.dir = link(arg,'./data');
|
|
19
|
+
if(obj(arg)){
|
|
20
|
+
if(arg.dir&&isDir(arg.dir)) this.dir = link(arg.dir,'./data');
|
|
21
|
+
if(arg.key&&str(arg.key)) this.key = md5(arg.key+'fkg');
|
|
32
22
|
}
|
|
23
|
+
if(this.dir&&!isDir(this.dir)) addDir(this.dir);
|
|
33
24
|
}
|
|
34
25
|
}
|
|
35
26
|
close(){
|
|
@@ -37,78 +28,62 @@ class fkg {
|
|
|
37
28
|
this.dirs = {};
|
|
38
29
|
this.dir = null;
|
|
39
30
|
}
|
|
40
|
-
|
|
31
|
+
io(n) {
|
|
41
32
|
const that = this;
|
|
42
33
|
if(!n||!str(n)) return;
|
|
43
34
|
if(!that.data[n]) that.data[n] = {};
|
|
44
35
|
return {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if(that.dirs[n]) {
|
|
50
|
-
arr = readdirSync(that.dirs[n]);
|
|
51
|
-
}else{
|
|
52
|
-
arr = Object.keys(that.data[n]);
|
|
53
|
-
}
|
|
54
|
-
if(arr.length==0) return null;
|
|
55
|
-
if(!size||!number(size)) size = 100;
|
|
56
|
-
if(arr.length>size) arr = arr.slice(0,size);
|
|
57
|
-
for (let i = 0;i < arr.length;i++) {
|
|
58
|
-
const id = arr[i];
|
|
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
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
obj[id] = that.data[n][id]
|
|
70
|
-
};
|
|
71
|
-
return obj
|
|
72
|
-
|
|
36
|
+
set(id,data){
|
|
37
|
+
if(!id||!str(id)||!data) return false;
|
|
38
|
+
that.data[n][id] = data;
|
|
39
|
+
return true;
|
|
73
40
|
},
|
|
74
|
-
|
|
75
|
-
if(that.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
};
|
|
80
|
-
list = null;
|
|
41
|
+
add(id,data){
|
|
42
|
+
if(!id||!str(id)||!data||!that.dir) return false;
|
|
43
|
+
if(!that.dirs[n]) {
|
|
44
|
+
that.dirs[n] = link(that.dir,'./'+n);
|
|
45
|
+
if(!isDir(that.dirs[n])) addDir(that.dirs[n]);
|
|
81
46
|
}
|
|
82
|
-
|
|
83
|
-
return
|
|
47
|
+
if(that.key) data = ekey(JSON.stringify(data),that.key+id);
|
|
48
|
+
return addJson(link(that.dirs[n],'./'+id),data);
|
|
84
49
|
},
|
|
85
50
|
get(id){
|
|
86
|
-
|
|
51
|
+
if(!id||!str(id)) return null;
|
|
52
|
+
let ids = that.data[n][id];
|
|
87
53
|
if(!ids&&that.dirs[n]){
|
|
88
54
|
const url = link(that.dirs[n],'./'+id);
|
|
89
55
|
if(isFile(url)){
|
|
90
|
-
|
|
91
|
-
if(
|
|
92
|
-
if(that.key) value = JSON.parse(dkey(value,that.key+id));
|
|
93
|
-
that.data[n][id] = value;
|
|
94
|
-
}
|
|
56
|
+
ids = readJson(url);
|
|
57
|
+
if(ids&&that.key) ids = JSON.parse(dkey(ids,that.key+id));
|
|
95
58
|
}
|
|
96
59
|
}
|
|
97
|
-
return
|
|
60
|
+
return ids;
|
|
98
61
|
},
|
|
99
|
-
|
|
100
|
-
that.data[n]
|
|
101
|
-
if(that.dirs[n])
|
|
102
|
-
|
|
103
|
-
addJson(link(that.dirs[n],'./'+id),d);
|
|
104
|
-
}
|
|
105
|
-
return true;
|
|
62
|
+
list(){
|
|
63
|
+
let arr = Object.keys(that.data[n]);
|
|
64
|
+
if(that.dirs[n]) arr = [...arr,...readdirSync(that.dirs[n])];
|
|
65
|
+
return arr;
|
|
106
66
|
},
|
|
107
67
|
del(id){
|
|
108
|
-
if(
|
|
109
|
-
|
|
68
|
+
if(!id||!str(id)) return false;
|
|
69
|
+
let res = true;
|
|
70
|
+
if(that.dirs[n]){
|
|
71
|
+
const url = link(that.dirs[n],'./'+id);
|
|
72
|
+
if(isFile(url)) res = delFile(url);
|
|
73
|
+
if(!res) return res;
|
|
110
74
|
}
|
|
111
75
|
delete that.data[n][id];
|
|
76
|
+
return res;
|
|
77
|
+
},
|
|
78
|
+
clear(){
|
|
79
|
+
if(that.dirs[n]){
|
|
80
|
+
let list = readdirSync(that.dirs[n]);
|
|
81
|
+
for (let i = 0;i < list.length;i++) {
|
|
82
|
+
delFile(link(that.dirs[n],'./'+list[i]));
|
|
83
|
+
};
|
|
84
|
+
list = null;
|
|
85
|
+
}
|
|
86
|
+
delete that.data[n];
|
|
112
87
|
return true;
|
|
113
88
|
}
|
|
114
89
|
}
|