fkg 1.0.9 → 1.1.0

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
@@ -28,7 +28,6 @@ const user = db.table('user');
28
28
  ```javascript
29
29
  user.set('id_1',{account:"zhangsan",emai:"1000@qq.com",phone:"13800138000",name:"张三"});
30
30
  user.set('id_2',{account:"wangwu",emai:"1001@qq.com",phone:"13800138002",name:"王五"});
31
- ...
32
31
  ```
33
32
 
34
33
  **5. Get user data**
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,7 +10,7 @@ const {
9
10
  readdirSync
10
11
  } = require('./utils/fs');
11
12
  const {md5,ekey,dkey} = require('rrid');
12
- const {str,obj,arr} = require('./utils/type');
13
+ const {str,obj,arr,number} = require('./utils/type');
13
14
  class fkg {
14
15
  constructor(arg) {
15
16
  this.close();
@@ -20,43 +21,12 @@ class fkg {
20
21
  }
21
22
  if(arg.key&&str(arg.key)) this.key = md5(arg.key+'fkg');
22
23
  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
24
  for (let i = 0;i < arg.dbName.length;i++) {
42
25
  const n = arg.dbName[i];
43
26
  this.data[n] = {};
44
27
  if(this.dir) {
45
28
  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
- }
29
+ if(!isDir(this.dirs[n])) addDir(this.dirs[n]);
60
30
  }
61
31
  };
62
32
  }
@@ -72,15 +42,34 @@ class fkg {
72
42
  if(!n||!str(n)) return;
73
43
  if(!that.data[n]) that.data[n] = {};
74
44
  return {
75
- list(page){
76
- function getOBJ(obj, n) {
77
- const keys = Object.keys(obj);
78
- return keys.slice(0, n).reduce((result, key) => {
79
- result[key] = obj[key];
80
- return result;
81
- },{});
45
+ list(size){
46
+ let arr;
47
+ let obj = {};
48
+
49
+ if(that.dirs[n]) {
50
+ arr = readdirSync(that.dirs[n]);
51
+ }else{
52
+ arr = Object.keys(that.data[n]);
82
53
  }
83
- return getOBJ(that.data[n],page>0?page:100);
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
+
84
73
  },
85
74
  clear(){
86
75
  if(that.dirs[n]){
@@ -94,16 +83,32 @@ class fkg {
94
83
  return true;
95
84
  },
96
85
  get(id){
86
+ const ids = that.data[n][id];
87
+ if(!ids&&that.dirs[n]){
88
+ const url = link(that.dirs[n],'./'+id);
89
+ if(isFile(url)){
90
+ let value = readJson(url);
91
+ if(value) {
92
+ if(that.key) value = JSON.parse(dkey(value,that.key+id));
93
+ that.data[n][id] = value;
94
+ }
95
+ }
96
+ }
97
97
  return that.data[n][id];
98
98
  },
99
99
  set(id,d){
100
- if(that.dirs[n]) that.setList[n+id] = {n,id,s:1};
101
100
  that.data[n][id] = d;
101
+ if(that.dirs[n]) {
102
+ if(that.key) d = ekey(JSON.stringify(d),that.key+id);
103
+ addJson(link(that.dirs[n],'./'+id),d);
104
+ }
102
105
  return true;
103
106
  },
104
107
  del(id){
108
+ if(that.dirs[n]&&that.data[n][id]){
109
+ delFile(link(that.dirs[n],'./'+id));
110
+ }
105
111
  delete that.data[n][id];
106
- if(that.dirs[n]) that.setList[n+id] = {n,id,s:0};
107
112
  return true;
108
113
  }
109
114
  }
package/lib/utils/fs.js CHANGED
@@ -59,7 +59,11 @@ module.exports = {
59
59
  })
60
60
  },
61
61
  readdirSync(url){
62
- return fs.readdirSync(url);
62
+ try {
63
+ return fs.readdirSync(url);
64
+ } catch (error) {
65
+ return []
66
+ }
63
67
  },
64
68
  dirFile(paths) {
65
69
  let obj = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fkg",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "Read and set memory key!",
5
5
  "main": "lib/index.js",
6
6
  "keywords": [