fkg 1.0.2 → 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 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,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,7 +27,15 @@ 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])) addDir(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
  }
@@ -29,8 +46,26 @@ class fkg {
29
46
  if(!this.data[n]) this.data[n] = {};
30
47
  const that = this;
31
48
  return {
32
- list(){
33
- return that.data[n];
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;
34
69
  },
35
70
  get(id){
36
71
  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));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fkg",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Read and set memory key!",
5
5
  "main": "lib/index.js",
6
6
  "keywords": [