fkg 1.0.1 → 1.0.3
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 +62 -0
- package/lib/index.js +64 -40
- package/lib/utils/fs.js +34 -40
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
## Quickstart
|
|
2
|
+
=============
|
|
3
|
+
|
|
4
|
+
### To Create cache service...
|
|
5
|
+
======================================
|
|
6
|
+
|
|
7
|
+
**1. Install**
|
|
8
|
+
|
|
9
|
+
```shell
|
|
10
|
+
npm install fkg
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
**2. Create a server**
|
|
15
|
+
|
|
16
|
+
```javascript
|
|
17
|
+
const fkg = require('fkg');
|
|
18
|
+
const db = new fkg({
|
|
19
|
+
dir: 'C:\\work', //数据储存位置,为空时不进行存储
|
|
20
|
+
dbName: ['user'] //需要数据储存时,数据表名不可为空
|
|
21
|
+
});
|
|
22
|
+
const user = db.table('user');
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**3. Add user data**
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
user.set('id_1',{account:"zhangsan",emai:"1000@qq.com",phone:"13800138000",name:"张三"});
|
|
29
|
+
user.set('id_2',{account:"wangwu",emai:"1001@qq.com",phone:"13800138002",name:"王五"});
|
|
30
|
+
...
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**5. Get user data**
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
user.list(10); //默认100条数据
|
|
37
|
+
user.list(50); //默认100条数据
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**6. Get list data**
|
|
41
|
+
|
|
42
|
+
```javascript
|
|
43
|
+
user.get('id_1');
|
|
44
|
+
user.get('id_2');
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**7. Delete user data**
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
user.del('id_1');
|
|
51
|
+
user.del('id_2');
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**7. Clear Data Table**
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
user.clear();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
package/lib/index.js
CHANGED
|
@@ -1,66 +1,90 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
const {
|
|
3
|
-
|
|
2
|
+
const {
|
|
3
|
+
isDir,
|
|
4
|
+
isFile,
|
|
5
|
+
link,
|
|
6
|
+
addDir,
|
|
7
|
+
addJson,
|
|
8
|
+
readJson,
|
|
9
|
+
delFile,
|
|
10
|
+
readdirSync
|
|
11
|
+
} = require('./utils/fs');
|
|
12
|
+
const {str,obj,arr} = require('./utils/type');
|
|
4
13
|
class fkg {
|
|
5
|
-
constructor(
|
|
14
|
+
constructor(arg) {
|
|
6
15
|
this.is = {};
|
|
7
16
|
this.data = {};
|
|
8
17
|
this.dirs = {};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.dir = link(dir,'./data');
|
|
13
|
-
if(!isDir(this.dir))
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
this.dir = null;
|
|
19
|
+
if(arg&&obj(arg)) {
|
|
20
|
+
if(arg.dir&&isDir(arg.dir)){
|
|
21
|
+
this.dir = link(arg.dir,'./data');
|
|
22
|
+
if(!isDir(this.dir)) addDir(this.dir);
|
|
23
|
+
}
|
|
24
|
+
if(arg.dbName&&arr(arg.dbName)&&arr.length>0){
|
|
25
|
+
for (let i = 0;i < arg.dbName.length;i++) {
|
|
26
|
+
const n = arg.dbName[i];
|
|
27
|
+
this.data[n] = {};
|
|
28
|
+
if(this.dir) {
|
|
29
|
+
this.dirs[n] = link(this.dir,'./'+n);
|
|
30
|
+
if(!isDir(this.dirs[n])) {
|
|
31
|
+
addDir(this.dirs[n]);
|
|
32
|
+
}else{
|
|
33
|
+
let list = readdirSync(this.dirs[n]);
|
|
34
|
+
for (let i = 0;i < list.length;i++) {
|
|
35
|
+
const value = readJson(link(this.dirs[n],'./'+list[i]));
|
|
36
|
+
if(value!==null) this.data[n][list[i]] = value;
|
|
37
|
+
};
|
|
19
38
|
}
|
|
20
39
|
}
|
|
21
|
-
}
|
|
22
|
-
}else{
|
|
23
|
-
this.dir = null;
|
|
40
|
+
};
|
|
24
41
|
}
|
|
25
42
|
}
|
|
26
|
-
deal();
|
|
27
43
|
}
|
|
28
|
-
|
|
44
|
+
table(n) {
|
|
29
45
|
if(!n||!str(n)) return;
|
|
30
|
-
if(this.
|
|
31
|
-
this.is[n] = 0;
|
|
32
|
-
if(!this.data[n]) {
|
|
33
|
-
this.dirs[n] = link(this.dir,'./'+n+'.fkg');
|
|
34
|
-
if(isFile(this.dirs[n])){
|
|
35
|
-
const res = await readJson(this.dirs[n]);
|
|
36
|
-
if(res) {
|
|
37
|
-
this.data[n] = res;
|
|
38
|
-
}else{
|
|
39
|
-
this.data[n] = {};
|
|
40
|
-
}
|
|
41
|
-
}else{
|
|
42
|
-
this.data[n] = {};
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}else{
|
|
46
|
-
if(!this.data[n]) this.data[n] = {};
|
|
47
|
-
}
|
|
46
|
+
if(!this.data[n]) this.data[n] = {};
|
|
48
47
|
const that = this;
|
|
49
48
|
return {
|
|
50
|
-
list(){
|
|
51
|
-
|
|
49
|
+
list(page){
|
|
50
|
+
function getOBJ(obj, n) {
|
|
51
|
+
const keys = Object.keys(obj);
|
|
52
|
+
return keys.slice(0, n).reduce((result, key) => {
|
|
53
|
+
result[key] = obj[key];
|
|
54
|
+
return result;
|
|
55
|
+
},{});
|
|
56
|
+
}
|
|
57
|
+
return getOBJ(that.data[n],page>0?page:100);
|
|
58
|
+
},
|
|
59
|
+
clear(){
|
|
60
|
+
if(that.dir){
|
|
61
|
+
let list = readdirSync(that.dirs[n]);
|
|
62
|
+
for (let i = 0;i < list.length;i++) {
|
|
63
|
+
delFile(link(that.dirs[n],'./'+list[i]));
|
|
64
|
+
};
|
|
65
|
+
list = null;
|
|
66
|
+
}
|
|
67
|
+
that.data[n] = {};
|
|
68
|
+
return true;
|
|
52
69
|
},
|
|
53
70
|
get(id){
|
|
71
|
+
if(that.dir&&!that.data[n][id]) {
|
|
72
|
+
const url = link(that.dirs[n],'./'+id);
|
|
73
|
+
if(isFile(url)){
|
|
74
|
+
const res = readJson(url);
|
|
75
|
+
if(res) that.data[n][id] = res;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
54
78
|
return that.data[n][id];
|
|
55
79
|
},
|
|
56
80
|
set(id,d){
|
|
57
|
-
that.is[n]++;
|
|
58
81
|
that.data[n][id] = d;
|
|
82
|
+
if(that.dir) addJson(link(that.dirs[n],'./'+id),d);
|
|
59
83
|
return true;
|
|
60
84
|
},
|
|
61
85
|
del(id){
|
|
62
|
-
that.is[n]++;
|
|
63
86
|
delete that.data[n][id];
|
|
87
|
+
if(that.dir) delFile(link(that.dirs[n],'./'+id));
|
|
64
88
|
return true;
|
|
65
89
|
}
|
|
66
90
|
}
|
package/lib/utils/fs.js
CHANGED
|
@@ -9,14 +9,12 @@ const isDir = (url) => {
|
|
|
9
9
|
}
|
|
10
10
|
}
|
|
11
11
|
const addFile = (url, data)=>{
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
})
|
|
12
|
+
try {
|
|
13
|
+
fs.writeFileSync(url, data,{encoding:'utf8'});
|
|
14
|
+
return true;
|
|
15
|
+
} catch (err) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
20
18
|
}
|
|
21
19
|
module.exports = {
|
|
22
20
|
isDir,
|
|
@@ -35,20 +33,19 @@ module.exports = {
|
|
|
35
33
|
addJson(url,data){
|
|
36
34
|
return addFile(url,JSON.stringify(data));
|
|
37
35
|
},
|
|
36
|
+
file(url){
|
|
37
|
+
try {
|
|
38
|
+
return fs.statSync(url)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
},
|
|
38
43
|
readJson(url){
|
|
39
|
-
|
|
40
|
-
fs.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if(data) {
|
|
45
|
-
resolve(JSON.parse(data))
|
|
46
|
-
}else{
|
|
47
|
-
resolve(null);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
})
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(fs.readFileSync(url));
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
52
49
|
},
|
|
53
50
|
readFile(url) {
|
|
54
51
|
return new Promise((resolve) => {
|
|
@@ -61,7 +58,7 @@ module.exports = {
|
|
|
61
58
|
});
|
|
62
59
|
})
|
|
63
60
|
},
|
|
64
|
-
|
|
61
|
+
readdirSync(url){
|
|
65
62
|
return fs.readdirSync(url);
|
|
66
63
|
},
|
|
67
64
|
dirFile(paths) {
|
|
@@ -89,26 +86,23 @@ module.exports = {
|
|
|
89
86
|
return obj;
|
|
90
87
|
},
|
|
91
88
|
addDir(url) {
|
|
92
|
-
|
|
93
|
-
fs.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
89
|
+
try {
|
|
90
|
+
fs.mkdirSync(url);
|
|
91
|
+
return true;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
return false
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
mkdirSync(url) {
|
|
97
|
+
return fs.mkdirSync(url);
|
|
101
98
|
},
|
|
102
99
|
delFile(url) {
|
|
103
|
-
|
|
104
|
-
fs.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
})
|
|
111
|
-
})
|
|
100
|
+
try {
|
|
101
|
+
fs.unlinkSync(url);
|
|
102
|
+
return true
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return false
|
|
105
|
+
}
|
|
112
106
|
},
|
|
113
107
|
getSize(obj) {
|
|
114
108
|
let size = 0;
|