fkg 1.0.2 → 1.0.4
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 +45 -6
- package/lib/utils/fs.js +7 -0
- 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
|
+
**8. Clear Data Table**
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
user.clear();
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
+
const {
|
|
3
|
+
isDir,
|
|
4
|
+
isFile,
|
|
5
|
+
link,
|
|
6
|
+
addDir,
|
|
7
|
+
addJson,
|
|
8
|
+
readJson,
|
|
9
|
+
delFile,
|
|
10
|
+
readdirSync
|
|
11
|
+
} = require('./utils/fs');
|
|
2
12
|
const {str,obj,arr} = require('./utils/type');
|
|
3
|
-
const {isDir,isFile,link,addDir,addJson,readJson,delFile} = require('./utils/fs');
|
|
4
13
|
class fkg {
|
|
5
14
|
constructor(arg) {
|
|
6
15
|
this.is = {};
|
|
@@ -18,19 +27,49 @@ class fkg {
|
|
|
18
27
|
this.data[n] = {};
|
|
19
28
|
if(this.dir) {
|
|
20
29
|
this.dirs[n] = link(this.dir,'./'+n);
|
|
21
|
-
if(!isDir(this.dirs[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
|
+
};
|
|
38
|
+
}
|
|
22
39
|
}
|
|
23
40
|
};
|
|
24
41
|
}
|
|
25
42
|
}
|
|
26
43
|
}
|
|
27
44
|
table(n) {
|
|
28
|
-
if(!n||!str(n)) return;
|
|
29
|
-
if(!this.data[n]) this.data[n] = {};
|
|
30
45
|
const that = this;
|
|
46
|
+
if(!n||!str(n)) return;
|
|
47
|
+
if(!that.data[n]) that.data[n] = {};
|
|
48
|
+
if(that.dir&&!that.dirs[n]) {
|
|
49
|
+
that.dirs[n] = link(that.dir,'./'+n);
|
|
50
|
+
addDir(that.dirs[n]);
|
|
51
|
+
}
|
|
31
52
|
return {
|
|
32
|
-
list(){
|
|
33
|
-
|
|
53
|
+
list(page){
|
|
54
|
+
function getOBJ(obj, n) {
|
|
55
|
+
const keys = Object.keys(obj);
|
|
56
|
+
return keys.slice(0, n).reduce((result, key) => {
|
|
57
|
+
result[key] = obj[key];
|
|
58
|
+
return result;
|
|
59
|
+
},{});
|
|
60
|
+
}
|
|
61
|
+
return getOBJ(that.data[n],page>0?page:100);
|
|
62
|
+
},
|
|
63
|
+
clear(){
|
|
64
|
+
if(that.dir){
|
|
65
|
+
let list = readdirSync(that.dirs[n]);
|
|
66
|
+
for (let i = 0;i < list.length;i++) {
|
|
67
|
+
delFile(link(that.dirs[n],'./'+list[i]));
|
|
68
|
+
};
|
|
69
|
+
list = null;
|
|
70
|
+
}
|
|
71
|
+
that.data[n] = {};
|
|
72
|
+
return true;
|
|
34
73
|
},
|
|
35
74
|
get(id){
|
|
36
75
|
if(that.dir&&!that.data[n][id]) {
|
package/lib/utils/fs.js
CHANGED
|
@@ -33,6 +33,13 @@ module.exports = {
|
|
|
33
33
|
addJson(url,data){
|
|
34
34
|
return addFile(url,JSON.stringify(data));
|
|
35
35
|
},
|
|
36
|
+
file(url){
|
|
37
|
+
try {
|
|
38
|
+
return fs.statSync(url)
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return false
|
|
41
|
+
}
|
|
42
|
+
},
|
|
36
43
|
readJson(url){
|
|
37
44
|
try {
|
|
38
45
|
return JSON.parse(fs.readFileSync(url));
|