ming_node 2.9.2 → 2.9.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ FileDb=require("../module/FileDb")
2
+
3
+ db= new FileDb("a.json",false)
4
+
5
+ db.add({
6
+ name:"zs"
7
+ })
8
+
9
+ console.log(db.listByPage({caseObj:{
10
+ "$name":"s"
11
+ }}))
12
+
13
+
14
+ db.add({
15
+ name:"ls"
16
+ })
17
+
18
+ console.log(db.listByPage({}))
@@ -6,11 +6,13 @@ db.add({
6
6
  name:"zs"
7
7
  })
8
8
 
9
- console.log(db.listByPage())
9
+ console.log(db.listByPage({caseObj:{
10
+ "$name":"s"
11
+ }}))
10
12
 
11
13
 
12
14
  db.add({
13
15
  name:"ls"
14
16
  })
15
17
 
16
- console.log(db.listByPage())
18
+ console.log(db.listByPage({}))
package/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * By : Minglie
4
4
  * QQ: 934031452
5
5
  * Date :2021.12.01
6
- * version :2.9.2
6
+ * version :2.9.3
7
7
  */
8
8
  var http = require('http');
9
9
  var https = require('https');
@@ -0,0 +1,185 @@
1
+ /**
2
+ * File : MemoryDb.js
3
+ * By : Minglie
4
+ * QQ: 934031452
5
+ * Date :2021.09.14
6
+ * rem : 文件数据库
7
+ */
8
+
9
+ const M=require("../index");
10
+
11
+ class FileDb{
12
+
13
+ constructor(tableName,readFromMemory=true) {
14
+ this.tableName=tableName;
15
+ this.readFromMemory=readFromMemory;
16
+ this.dataList=M.getObjByFile(this.tableName)
17
+ }
18
+
19
+ readFromFile(){
20
+ if(this.readFromMemory){
21
+ return
22
+ }else {
23
+ this.dataList=M.getObjByFile(this.tableName);
24
+ }
25
+ }
26
+
27
+ /**
28
+ * 单个加
29
+ * @param obj
30
+ * @returns {Promise<*>}
31
+ */
32
+ async add(obj){
33
+ this.readFromFile();
34
+ obj.id = M.randomStr();
35
+ this.dataList=[...this.dataList,obj];
36
+ M.writeObjToFile(this.tableName,this.dataList)
37
+ return obj;
38
+ }
39
+
40
+ async update(obj){
41
+ this.readFromFile();
42
+ for (let i = 0; i < this.dataList.length; i++) {
43
+ if (this.dataList[i].id == obj.id) {
44
+ this.dataList[i]=obj;
45
+ return
46
+ }
47
+ }
48
+ M.writeObjToFile(this.tableName,this.dataList)
49
+ }
50
+
51
+ listAll(caseObj){
52
+ this.readFromFile();
53
+ let o_keys=[];
54
+ if(caseObj){
55
+ o_keys = Object.keys(caseObj);
56
+ }
57
+ if (caseObj && o_keys.length>0) {
58
+ let r_list = [];
59
+ let o_vals = Object.values(caseObj);
60
+ var d = this.dataList;
61
+ for (let i = 0; i < d.length; i++) {
62
+ let s=0;
63
+ for (let j=0;j<o_keys.length;j++){
64
+ if(o_keys[j][0]=="$"){
65
+ let realKey=o_keys[j].replace("$","");
66
+ if (d[i][realKey].indexOf(o_vals[j])==-1) {
67
+ break
68
+ }
69
+ }else {
70
+ if (d[i][o_keys[j]] != o_vals[j]) {
71
+ break
72
+ }
73
+ }
74
+ s++;
75
+ }
76
+ if(s==o_keys.length){
77
+ r_list.push(d[i]);
78
+ }
79
+ }
80
+ return r_list;
81
+ } else {
82
+ return this.dataList;
83
+ }
84
+ }
85
+
86
+
87
+ listByPage({page=0, num=10, caseObj,order}) {
88
+ page=Number.parseInt(page);
89
+ num=Number.parseInt(num);
90
+ if (page <= 0) page = 1;
91
+ let rows;
92
+ if (caseObj) {
93
+ rows = this.listAll(caseObj);
94
+ } else {
95
+ rows = this.listAll();
96
+ }
97
+ if(order){
98
+ let orderArr= order.split(" ");
99
+ let orderField=orderArr[0];
100
+ let orderType=orderArr[1];
101
+ rows.sort((a,b)=>{
102
+ let ret=0;
103
+ if (a[orderField]>b[orderField]) {
104
+ ret= 1;
105
+ }else if(a<b){
106
+ ret= -1
107
+ }else{
108
+ ret= 0;
109
+ }
110
+ if(orderType=="asc"){
111
+ return ret;
112
+ }else {
113
+ return -1*ret;
114
+ }
115
+ })
116
+ }
117
+ let total = rows.length;
118
+ rows=JSON.parse(JSON.stringify(rows))
119
+ rows = rows.splice((page - 1) * num, num)
120
+ return {rows, total}
121
+ }
122
+
123
+
124
+ deleteAll(o) {
125
+ this.readFromFile();
126
+ if (o) {
127
+ let r_list = [];
128
+ let o_keys = Object.keys(o);
129
+ let o_vals = Object.values(o)
130
+ var d = this.dataList;
131
+ let delete_index=[]
132
+ for (let i = 0; i < d.length; i++) {
133
+ let s=0;
134
+ for (let j=0;j<o_keys.length;j++){
135
+ if (d[i][o_keys[j]] != o_vals[j]) {
136
+ break
137
+ }
138
+ s++;
139
+ }
140
+ if(s==o_keys.length){
141
+ delete_index.push(i)
142
+ }
143
+ }
144
+ for (let i = 0; i < d.length; i++) {
145
+ if(!delete_index.includes(i)){
146
+ r_list.push(d[i])
147
+ }
148
+ }
149
+ this.dataList=r_list;
150
+ return delete_index.length;
151
+ } else {
152
+ let length=this.dataList.length;
153
+ this.dataList=[];
154
+ return length;
155
+ }
156
+ M.writeObjToFile(this.tableName,this.dataList)
157
+ }
158
+
159
+ getById(id) {
160
+ this.readFromFile();
161
+ var d = this.dataList;
162
+ for (let i = 0; i < d.length; i++) {
163
+ if(d[i].id==id){
164
+ return d[i];
165
+ }
166
+ }
167
+ return null;
168
+ }
169
+
170
+ deleteById(id) {
171
+ this.readFromFile();
172
+ var d = this.dataList;
173
+ for (let i = 0; i < d.length; i++) {
174
+ if(d[i].id==id){
175
+ this.dataList.splice(i, 1);
176
+ return id;
177
+ }
178
+ }
179
+ M.writeObjToFile(this.tableName,this.dataList)
180
+ return 0;
181
+ }
182
+ }
183
+
184
+
185
+ module.exports = FileDb;
@@ -36,7 +36,10 @@ class MemoryDb{
36
36
  }
37
37
 
38
38
  listAll(caseObj){
39
- let o_keys = Object.keys(caseObj);
39
+ let o_keys=[];
40
+ if(caseObj){
41
+ o_keys = Object.keys(caseObj);
42
+ }
40
43
  if (caseObj && o_keys.length>0) {
41
44
  let r_list = [];
42
45
  let o_vals = Object.values(caseObj);
@@ -44,8 +47,15 @@ class MemoryDb{
44
47
  for (let i = 0; i < d.length; i++) {
45
48
  let s=0;
46
49
  for (let j=0;j<o_keys.length;j++){
47
- if (d[i][o_keys[j]] != o_vals[j]) {
48
- break
50
+ if(o_keys[j][0]=="$"){
51
+ let realKey=o_keys[j].replace("$","");
52
+ if (d[i][realKey].indexOf(o_vals[j])==-1) {
53
+ break
54
+ }
55
+ }else {
56
+ if (d[i][o_keys[j]] != o_vals[j]) {
57
+ break
58
+ }
49
59
  }
50
60
  s++;
51
61
  }
@@ -60,19 +70,39 @@ class MemoryDb{
60
70
  }
61
71
 
62
72
 
63
- listByPage(startPage=0, limit=10, caseObj) {
64
- startPage=Number.parseInt(startPage);
65
- limit=Number.parseInt(limit);
66
- if (startPage <= 0) startPage = 1;
73
+ listByPage({page=0, num=10, caseObj,order}) {
74
+ page=Number.parseInt(page);
75
+ num=Number.parseInt(num);
76
+ if (page <= 0) page = 1;
67
77
  let rows;
68
78
  if (caseObj) {
69
79
  rows = this.listAll(caseObj);
70
80
  } else {
71
81
  rows = this.listAll();
72
82
  }
83
+ if(order){
84
+ let orderArr= order.split(" ");
85
+ let orderField=orderArr[0];
86
+ let orderType=orderArr[1];
87
+ rows.sort((a,b)=>{
88
+ let ret=0;
89
+ if (a[orderField]>b[orderField]) {
90
+ ret= 1;
91
+ }else if(a<b){
92
+ ret= -1
93
+ }else{
94
+ ret= 0;
95
+ }
96
+ if(orderType=="desc"){
97
+ return ret;
98
+ }else {
99
+ return -1*ret;
100
+ }
101
+ })
102
+ }
73
103
  let total = rows.length;
74
104
  rows=JSON.parse(JSON.stringify(rows))
75
- rows = rows.splice((startPage - 1) * limit, limit)
105
+ rows = rows.splice((page - 1) * num, num)
76
106
  return {rows, total}
77
107
  }
78
108
 
package/package.json CHANGED
@@ -46,5 +46,5 @@
46
46
  "scripts": {
47
47
  "test": "echo \"Error: no test specified\" && exit 1"
48
48
  },
49
- "version": "2.9.2"
49
+ "version": "2.9.3"
50
50
  }
@@ -26,7 +26,7 @@ class MemoryBaseRestApi extends AbstractBaseRestApi{
26
26
  }
27
27
 
28
28
  async list({page,num,order,queryCase}){
29
- let r= this.memoryDb.listByPage(page,num,queryCase);
29
+ let r= this.memoryDb.listByPage({page,num,caseObj:queryCase,order});
30
30
  return r;
31
31
  }
32
32
 
@@ -1,59 +1,50 @@
1
1
  /**
2
2
  * 数据源为内存的rpc风格接口
3
3
  */
4
- const M=require("../../index")
4
+ const FileDb=require("../../module/FileDb");
5
5
  const AbstractBaseRpcApi=require("./AbstractBaseRpcApi");
6
6
 
7
7
  class FileBaseRpcApi extends AbstractBaseRpcApi{
8
+
8
9
  constructor(props) {
9
10
  super(props);
11
+ if(props.readFromMemory==null){
12
+ props.readFromMemory=true;
13
+ }
14
+ this.fileDb=new FileDb(props.tableName,props.readFromMemory);
10
15
  }
11
16
  async add(obj){
12
- obj.id = M.randomStr();
13
- M.addObjToFile(this.tableName, obj);
14
- return obj;
17
+ let r= this.fileDb.add(obj);
18
+ return r;
15
19
  }
16
20
 
17
21
  async delete(obj){
18
- let r=0;
19
- if (obj) {
20
- r= M.deleteObjByPropFile(this.tableName, obj);
21
- } else {
22
- r=M.writeObjToFile(this.tableName,[]);
23
- }
22
+ let r= this.fileDb.deleteAll(obj);
24
23
  return r;
25
24
  }
26
25
 
27
26
  async getById(id){
28
- let r=M.getById(this.tableName,id);
27
+ let r=this.fileDb.getById(id);
29
28
  return r;
30
29
  }
31
30
 
32
- async list({page=1,num=10,order, queryCase}){
33
- page=Number.parseInt(page);
34
- num=Number.parseInt(num);
35
- if (page <= 0) page = 1;
36
- let rows =await this.listAll(queryCase);
37
- let total = rows.length;
38
- rows = rows.splice((page - 1) * num, num)
39
- return {rows, total}
31
+ async list({page,num,order,queryCase}){
32
+ let r= this.fileDb.listByPage({page,num,caseObj:queryCase,order});
33
+ return r;
40
34
  }
41
35
 
42
36
  async listAll(obj){
43
- if (obj) {
44
- return M.listAllObjByPropFile(this.tableName, obj);
45
- } else {
46
- return M.getObjByFile(this.tableName);
47
- }
37
+ let r= this.fileDb.listAll(obj);
38
+ return r;
48
39
  }
49
40
 
50
41
  async update(obj){
51
- let r= M.updateObjByIdFile(this.tableName,obj);
42
+ let r= this.fileDb.update(obj);
52
43
  return r;
53
44
  }
54
45
 
55
46
  async getChildenList(id,caseObj){
56
- let r= this.listAll({parent_id:id,...caseObj});
47
+ let r= this.fileDb.listAll({parent_id:id,...caseObj});
57
48
  return r;
58
49
  }
59
50
  }
@@ -26,7 +26,7 @@ class MemoryBaseRpcApi extends AbstractBaseRpcApi{
26
26
  }
27
27
 
28
28
  async list({page,num,order,queryCase}){
29
- let r= this.memoryDb.listByPage(page,num,queryCase);
29
+ let r= this.memoryDb.listByPage({page,num,caseObj: queryCase,order});
30
30
  return r;
31
31
  }
32
32