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