fkg 1.0.8 → 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
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
  const {
3
- isDir,
4
3
  link,
4
+ isDir,
5
+ isFile,
5
6
  addDir,
6
7
  addJson,
7
- readJson,
8
8
  delFile,
9
+ readJson,
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 getTime = ()=>{ return new Date().getTime();}
13
+ const {str,obj,arr,number} = require('./utils/type');
14
14
  class fkg {
15
15
  constructor(arg) {
16
16
  this.close();
@@ -21,42 +21,18 @@ class fkg {
21
21
  }
22
22
  if(arg.key&&str(arg.key)) this.key = md5(arg.key+'fkg');
23
23
  if(arg.dbName&&arr(arg.dbName)&&arr.length>0){
24
- this.setList = {};
25
- setInterval(() => {
26
- for (const keys in this.setList) {
27
- const {n,id} = this.setList[keys];
28
- let d = this.data[n][id];
29
- if(this.key) d = ekey(JSON.stringify(d),this.key+id);
30
- addJson(link(this.dirs[n],'./'+id),d);
31
- delete this.setList[keys];
32
- }
33
- }, 3000);
34
24
  for (let i = 0;i < arg.dbName.length;i++) {
35
25
  const n = arg.dbName[i];
36
26
  this.data[n] = {};
37
27
  if(this.dir) {
38
28
  this.dirs[n] = link(this.dir,'./'+n);
39
- if(!isDir(this.dirs[n])) {
40
- addDir(this.dirs[n]);
41
- }else{
42
- let list = readdirSync(this.dirs[n]);
43
- for (let i = 0;i < list.length;i++) {
44
- const id = list[i];
45
- let value = readJson(link(this.dirs[n],'./'+id));
46
- if(value) {
47
- if(this.key) value = JSON.parse(dkey(value,this.key+id));
48
- this.data[n][id] = value;
49
- }
50
- };
51
- list = null;
52
- }
29
+ if(!isDir(this.dirs[n])) addDir(this.dirs[n]);
53
30
  }
54
31
  };
55
32
  }
56
33
  }
57
34
  }
58
35
  close(){
59
- this.lock = {};
60
36
  this.data = {};
61
37
  this.dirs = {};
62
38
  this.dir = null;
@@ -65,17 +41,35 @@ class fkg {
65
41
  const that = this;
66
42
  if(!n||!str(n)) return;
67
43
  if(!that.data[n]) that.data[n] = {};
68
- if(!that.lock[n]) that.lock[n] = {};
69
44
  return {
70
- list(page){
71
- function getOBJ(obj, n) {
72
- const keys = Object.keys(obj);
73
- return keys.slice(0, n).reduce((result, key) => {
74
- result[key] = obj[key];
75
- return result;
76
- },{});
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]);
77
53
  }
78
- 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
+
79
73
  },
80
74
  clear(){
81
75
  if(that.dirs[n]){
@@ -86,31 +80,35 @@ class fkg {
86
80
  list = null;
87
81
  }
88
82
  delete that.data[n];
89
- delete that.lock[n];
90
83
  return true;
91
84
  },
92
85
  get(id){
93
- if(that.lock[n][id]){
94
- const time = getTime();
95
- while (that.lock[n][id]) {
96
- // 等待解锁
97
- if(getTime()-time>3000) {
98
- delete that.lock[n][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;
99
94
  }
100
- }
101
- }
95
+ }
96
+ }
102
97
  return that.data[n][id];
103
98
  },
104
99
  set(id,d){
105
- that.lock[n][id] = 1;
106
100
  that.data[n][id] = d;
107
- if(that.dirs[n]) that.setList[n+id] = {n,id};
108
- delete that.lock[n][id];
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
+ }
109
105
  return true;
110
106
  },
111
107
  del(id){
108
+ if(that.dirs[n]&&that.data[n][id]){
109
+ delFile(link(that.dirs[n],'./'+id));
110
+ }
112
111
  delete that.data[n][id];
113
- if(that.dirs[n]) delFile(link(that.dirs[n],'./'+id));
114
112
  return true;
115
113
  }
116
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.8",
3
+ "version": "1.1.0",
4
4
  "description": "Read and set memory key!",
5
5
  "main": "lib/index.js",
6
6
  "keywords": [