fkg 1.0.7 → 1.0.9

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 CHANGED
@@ -16,8 +16,9 @@ npm install fkg
16
16
  ```javascript
17
17
  const fkg = require('fkg');
18
18
  const db = new fkg({
19
- dir: 'C:\\work', //数据储存位置,为空时不进行存储
20
- dbName: ['user'] //需要数据储存时,数据表名不可为空
19
+ key:'f31d0353cdf', //key不为空时,储存的数据会被加密
20
+ dir: 'C:\\work', //数据储存位置,为空时不进行存储
21
+ dbName: ['user'] //需要数据储存时,数据表名不可为空
21
22
  });
22
23
  const user = db.table('user');
23
24
  ```
package/lib/index.js CHANGED
@@ -1,23 +1,43 @@
1
1
  'use strict';
2
2
  const {
3
- isDir,
4
3
  link,
4
+ isDir,
5
5
  addDir,
6
6
  addJson,
7
- readJson,
8
7
  delFile,
8
+ readJson,
9
9
  readdirSync
10
10
  } = require('./utils/fs');
11
+ const {md5,ekey,dkey} = require('rrid');
11
12
  const {str,obj,arr} = require('./utils/type');
12
13
  class fkg {
13
14
  constructor(arg) {
14
15
  this.close();
15
- if(arg&&obj(arg)) {
16
+ if(arg&&obj(arg)) {
16
17
  if(arg.dir&&isDir(arg.dir)){
17
18
  this.dir = link(arg.dir,'./data');
18
19
  if(!isDir(this.dir)) addDir(this.dir);
19
20
  }
21
+ if(arg.key&&str(arg.key)) this.key = md5(arg.key+'fkg');
20
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);
21
41
  for (let i = 0;i < arg.dbName.length;i++) {
22
42
  const n = arg.dbName[i];
23
43
  this.data[n] = {};
@@ -28,8 +48,12 @@ class fkg {
28
48
  }else{
29
49
  let list = readdirSync(this.dirs[n]);
30
50
  for (let i = 0;i < list.length;i++) {
31
- const value = readJson(link(this.dirs[n],'./'+list[i]));
32
- if(value!==null) this.data[n][list[i]] = value;
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
+ }
33
57
  };
34
58
  list = null;
35
59
  }
@@ -59,27 +83,27 @@ class fkg {
59
83
  return getOBJ(that.data[n],page>0?page:100);
60
84
  },
61
85
  clear(){
62
- if(that.dir&&that.dirs[n]){
86
+ if(that.dirs[n]){
63
87
  let list = readdirSync(that.dirs[n]);
64
88
  for (let i = 0;i < list.length;i++) {
65
89
  delFile(link(that.dirs[n],'./'+list[i]));
66
90
  };
67
91
  list = null;
68
92
  }
69
- that.data[n] = {};
93
+ delete that.data[n];
70
94
  return true;
71
95
  },
72
96
  get(id){
73
97
  return that.data[n][id];
74
98
  },
75
99
  set(id,d){
100
+ if(that.dirs[n]) that.setList[n+id] = {n,id,s:1};
76
101
  that.data[n][id] = d;
77
- if(that.dir&&that.dirs[n]) addJson(link(that.dirs[n],'./'+id),d);
78
102
  return true;
79
103
  },
80
104
  del(id){
81
105
  delete that.data[n][id];
82
- if(that.dir&&that.dirs[n]) delFile(link(that.dirs[n],'./'+id));
106
+ if(that.dirs[n]) that.setList[n+id] = {n,id,s:0};
83
107
  return true;
84
108
  }
85
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fkg",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Read and set memory key!",
5
5
  "main": "lib/index.js",
6
6
  "keywords": [
@@ -8,5 +8,8 @@
8
8
  "cache"
9
9
  ],
10
10
  "author": "weduu",
11
- "license": "MIT"
11
+ "license": "MIT",
12
+ "dependencies": {
13
+ "rrid": "^1.0.6"
14
+ }
12
15
  }