ming_node 2.8.0 → 2.9.3

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -55,7 +55,7 @@ app.get("/getSession",(req,res)=>{
55
55
  ```javascript
56
56
  +async function(){
57
57
  M =await new Promise((v)=>require('https').get("https://minglie.github.io/js/ming_node.js",(q)=>{d='';q.on('data',(a)=>d+=a);q.on('end',()=>v(eval(d)))}))
58
- var app=M.server();
58
+ var app=M.server();
59
59
  app.listen(8888);
60
60
  app.get("/",async (req,res)=>{
61
61
  app.redirect("/index.html",req,res)
@@ -110,13 +110,21 @@ app.listen(8888);
110
110
  app.get("/baidu",(req,res)=>{
111
111
  console.log(req.params);
112
112
  //响应html文本
113
- // res.renderHtml("hello woed")
113
+ res.renderHtml("hello woed")
114
114
  //响应js文本
115
- //res.renderJs("alert(5)")
115
+ res.renderJs("alert(5)")
116
116
  //响应本地文件
117
- //res.renderUrl("file:D:/G/ming_node/test/test.html");
117
+ res.renderUrl("file:D:/G/ming_node/test/test.html");
118
118
  //响应百度首页
119
119
  res.renderUrl("https://www.baidu.com/index.html");
120
+ //响应一个网络文件,支持二进制
121
+ res.sendFile("https://ming/a.txt");
122
+ //响应一个本地文件,支持二进制
123
+ res.sendFile("file:C:/Users/a.txt");
124
+ //响应一个网络文件,支持二进制,浏览器直接下载
125
+ res.sendFile("https://ming/a.txt",true);
126
+ //响应一个本地文件,支持二进制,浏览器直接下载
127
+ res.sendFile("file:C:/Users/a.txt",true);
120
128
  })
121
129
 
122
130
  ```
@@ -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({}))
@@ -0,0 +1,3 @@
1
+ let restart= require("../utils/nodemon/restart");
2
+ restart("test.js","test.js")
3
+
@@ -0,0 +1,8 @@
1
+ var M=require("../index");
2
+
3
+ app=M.server()
4
+ app.listen(8888)
5
+
6
+ app.get(["/h15","/h16","/h18888"],(req,res)=>{
7
+ res.send("BBBBXXXEEEEE")
8
+ })
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.8.0
6
+ * version :2.9.3
7
7
  */
8
8
  var http = require('http');
9
9
  var https = require('https');
@@ -437,23 +437,22 @@ M.import=async function (url,callback){
437
437
  /**
438
438
  *下载图片
439
439
  */
440
- M.download = function (url, file, callback) {
440
+ M.download =async function (url, file) {
441
441
  var func = http;
442
442
  if (url.indexOf("https") >= 0) {
443
443
  func = https;
444
444
  }
445
- func.get(url, function (res) {
446
- res.setEncoding('binary');//转成二进制
447
- var content = '';
448
- res.on('data', function (data) {
449
- content += data;
450
- }).on('end', function () {
451
- if (callback) callback();
452
- fs.writeFile(file, content, 'binary', function (err) {
453
- if (err) throw err;
454
- });
445
+ return new Promise(((resolve, reject) => {
446
+ func.get(url, function (res) {
447
+ let writeStream = fs.createWriteStream(file);
448
+ res.on("end",d=>{
449
+ resolve(file);
450
+ }).on("error",e=>{
451
+ reject(e);
452
+ }).pipe(writeStream);
455
453
  });
456
- });
454
+ }))
455
+
457
456
  }
458
457
  /**
459
458
  *下载所有图片
@@ -823,10 +822,8 @@ M.readLine =async function (file, callback) {
823
822
  }
824
823
  });
825
824
  input.on('end', function () {
826
- if (remaining.length > 0) {
827
- resolve(lineCount);
828
- callback(remaining);
829
- }
825
+ resolve(lineCount);
826
+ callback(remaining);
830
827
  });
831
828
  } )
832
829
  }
@@ -1579,6 +1576,47 @@ M.server = function () {
1579
1576
  G._end(req,text);
1580
1577
  }
1581
1578
 
1579
+
1580
+ res.sendFile= async function (url,isDownLoad=false) {
1581
+ let extname = path.extname(url);
1582
+ let fileName=M.getFileNameByUrl(url);
1583
+ if(url.startsWith("file")){
1584
+ url= url.substring(5);
1585
+ res.setHeader("Access-Control-Allow-Origin", "*");
1586
+ res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
1587
+ res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
1588
+ res.setHeader("X-Powered-By", ' 3.2.1');
1589
+ if(isDownLoad){
1590
+ res.setHeader("Content-Disposition", "attachment;filename="+encodeURIComponent(fileName,"utf-8"));
1591
+ }
1592
+ res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
1593
+ fs.createReadStream(url).on("end",d=>{})
1594
+ .on("error",e=>{
1595
+ console.error(e);
1596
+ }).pipe(res);
1597
+ }else {
1598
+ var func = http;
1599
+ if (url.indexOf("https") >= 0) {
1600
+ func = https;
1601
+ }
1602
+ func.get(url, function (res1) {
1603
+ res.setHeader("Access-Control-Allow-Origin", "*");
1604
+ res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
1605
+ res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
1606
+ res.setHeader("X-Powered-By", ' 3.2.1');
1607
+ if(isDownLoad){
1608
+ res.setHeader("Content-Disposition", "attachment;filename="+encodeURIComponent(fileName,"utf-8"));
1609
+ }
1610
+ res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
1611
+ res1.on("end",d=>{})
1612
+ .on("error",e=>{
1613
+ console.error(e)
1614
+ }).pipe(res);
1615
+ });
1616
+ }
1617
+ }
1618
+
1619
+
1582
1620
  res.render = async function (url) {
1583
1621
  res.alreadySend = true;
1584
1622
  let text="";
@@ -1628,6 +1666,14 @@ M.server = function () {
1628
1666
  res.end();
1629
1667
  G._end(req,text);
1630
1668
  }
1669
+ //扩充res一个renderHtml方法
1670
+ res.renderCss = function (text) {
1671
+ res.alreadySend = true;
1672
+ res.writeHead(200, {"Content-Type": "text/css;charset='utf-8'"});
1673
+ res.write(text);
1674
+ res.end();
1675
+ G._end(req,text);
1676
+ }
1631
1677
  res.redirect = function (url) {
1632
1678
  res.alreadySend = true;
1633
1679
  res.writeHead(302, {'Content-Type': 'text/html; charset=utf-8', 'Location': url});
@@ -1779,62 +1825,86 @@ M.server = function () {
1779
1825
  app.use(plugin,pluginParams)
1780
1826
  }
1781
1827
 
1828
+ app.registServer=function (url,callback,single){
1829
+ if (Array.isArray(url)) {
1830
+ url.forEach(u=>{
1831
+ single(u,callback);
1832
+ })
1833
+ } else {
1834
+ single(url,callback);
1835
+ }
1836
+ }
1837
+
1782
1838
  /**
1783
1839
  * 注册get请求
1784
1840
  */
1785
1841
  app.get = function (url, callback) {
1786
- url = M.formatUrl(url);
1787
- var realUrl = url;
1788
- if (url.indexOf(":") > 0) {
1789
- url = url.substr(0, url.indexOf(":"));
1790
- G._rest[url] = realUrl;
1842
+ const single=(url,callback)=>{
1843
+ url = M.formatUrl(url);
1844
+ var realUrl = url;
1845
+ if (url.indexOf(":") > 0) {
1846
+ url = url.substr(0, url.indexOf(":"));
1847
+ G._rest[url] = realUrl;
1848
+ }
1849
+ G._get[url] = callback;
1791
1850
  }
1792
-
1793
- G._get[url] = callback;
1851
+ app.registServer(url,callback,single)
1794
1852
  }
1795
1853
  /**
1796
1854
  *注册post请求
1797
1855
  */
1798
1856
  app.post = function (url, callback) {
1799
- url = M.formatUrl(url);
1800
- var realUrl = url;
1801
- if (url.indexOf(":") > 0) {
1802
- url = url.substr(0, url.indexOf(":"));
1803
- G._rest[url] = realUrl;
1857
+ const single=(url,callback)=>{
1858
+ url = M.formatUrl(url);
1859
+ var realUrl = url;
1860
+ if (url.indexOf(":") > 0) {
1861
+ url = url.substr(0, url.indexOf(":"));
1862
+ G._rest[url] = realUrl;
1863
+ }
1864
+ G._post[url] = callback;
1804
1865
  }
1805
- G._post[url] = callback;
1866
+ app.registServer(url,callback,single)
1806
1867
  }
1807
1868
 
1808
1869
  app.put = function (url, callback) {
1809
- url = M.formatUrl(url);
1810
- var realUrl = url;
1811
- if (url.indexOf(":") > 0) {
1812
- url = url.substr(0, url.indexOf(":"));
1813
- G._rest[url] = realUrl;
1870
+ const single=(url,callback)=>{
1871
+ url = M.formatUrl(url);
1872
+ var realUrl = url;
1873
+ if (url.indexOf(":") > 0) {
1874
+ url = url.substr(0, url.indexOf(":"));
1875
+ G._rest[url] = realUrl;
1876
+ }
1877
+ G._put[url] = callback;
1814
1878
  }
1815
- G._put[url] = callback;
1879
+ app.registServer(url,callback,single)
1816
1880
  }
1817
1881
 
1818
1882
  app.delete = function (url, callback) {
1819
- url = M.formatUrl(url);
1820
- var realUrl = url;
1821
- if (url.indexOf(":") > 0) {
1822
- url = url.substr(0, url.indexOf(":"));
1823
- G._rest[url] = realUrl;
1883
+ const single=(url,callback)=>{
1884
+ url = M.formatUrl(url);
1885
+ var realUrl = url;
1886
+ if (url.indexOf(":") > 0) {
1887
+ url = url.substr(0, url.indexOf(":"));
1888
+ G._rest[url] = realUrl;
1889
+ }
1890
+ G._delete[url] = callback;
1824
1891
  }
1825
- G._delete[url] = callback;
1892
+ app.registServer(url,callback,single);
1826
1893
  }
1827
1894
  /**
1828
1895
  *注册任意请求方法的请求
1829
1896
  */
1830
1897
  app.mapping = function (url, callback) {
1831
- url = M.formatUrl(url);
1832
- var realUrl = url;
1833
- if (url.indexOf(":") > 0) {
1834
- url = url.substr(0, url.indexOf(":"));
1835
- G._rest[url] = realUrl;
1898
+ const single=(url,callback)=>{
1899
+ url = M.formatUrl(url);
1900
+ var realUrl = url;
1901
+ if (url.indexOf(":") > 0) {
1902
+ url = url.substr(0, url.indexOf(":"));
1903
+ G._rest[url] = realUrl;
1904
+ }
1905
+ G._mapping[url] = callback;
1836
1906
  }
1837
- G._mapping[url] = callback;
1907
+ app.registServer(url,callback,single);
1838
1908
  }
1839
1909
 
1840
1910
 
@@ -2012,8 +2082,9 @@ M.axios = function (axiosConfig) {
2012
2082
  privateObj.dealUseServer = async function (req, res) {
2013
2083
  for (let key in M._use){
2014
2084
  if(M._use[key].regExp.test(req.url)){
2015
- await M._use[key].callback(req,res);
2016
- return;
2085
+ if(!res.alreadySend){
2086
+ await M._use[key].callback(req,res);
2087
+ }
2017
2088
  }
2018
2089
  }
2019
2090
  }
@@ -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.8.0"
49
+ "version": "2.9.3"
50
50
  }
@@ -10,7 +10,7 @@ class AbstractBaseRest{
10
10
 
11
11
  async add(obj){}
12
12
  async delete(obj){}
13
- async list({page,num,...queryCase}){}
13
+ async list({page,num,order,...queryCase}){}
14
14
  async listAll(obj){}
15
15
  async update(obj){}
16
16
  async getChildenList(id,queryCase){}
@@ -42,9 +42,9 @@ class AbstractBaseRest{
42
42
  });
43
43
 
44
44
  app.get(`${this.prefix}/:id`,async (req,res)=>{
45
- const {id,page,num,...queryCase}=req.params;
45
+ const {id,page,num,order,...queryCase}=req.params;
46
46
  if(id==undefined){
47
- let r=await this.list({page,num,queryCase});
47
+ let r=await this.list({page,num,order,queryCase});
48
48
  res.send(M.successResult(r));
49
49
  }else {
50
50
  let r=await this.getById(req.params.id);
@@ -27,12 +27,12 @@ class ApiCloudBaseRestApi extends AbstractBaseRestApi{
27
27
  return r[0];
28
28
  }
29
29
 
30
- async list({page=1,num=10,queryCase}){
30
+ async list({page=1,num=10,order="createdAt ASC",queryCase}){
31
31
  page=Number.parseInt(page);
32
32
  num=Number.parseInt(num);
33
33
  let limit=num;
34
34
  let skip= (page-1)*num
35
- let r=await this.tableClient.list(queryCase,limit,skip,"createdAt ASC");
35
+ let r=await this.tableClient.list(queryCase,limit,skip,order);
36
36
  return r;
37
37
  }
38
38
 
@@ -29,7 +29,7 @@ class FileBaseRestApi extends AbstractBaseRestApi{
29
29
  return r;
30
30
  }
31
31
 
32
- async list({page=1,num=10,queryCase}){
32
+ async list({page=1,num=10,order,queryCase}){
33
33
  page=Number.parseInt(page);
34
34
  num=Number.parseInt(num);
35
35
  if (page <= 0) page = 1;
@@ -25,8 +25,8 @@ class MemoryBaseRestApi extends AbstractBaseRestApi{
25
25
  return r;
26
26
  }
27
27
 
28
- async list({page,num,queryCase}){
29
- let r= this.memoryDb.listByPage(page,num,queryCase);
28
+ async list({page,num,order,queryCase}){
29
+ let r= this.memoryDb.listByPage({page,num,caseObj:queryCase,order});
30
30
  return r;
31
31
  }
32
32
 
@@ -28,7 +28,7 @@ class MongoDbBaseRestApi extends AbstractBaseRestApi{
28
28
  return r;
29
29
  }
30
30
 
31
- async list({page=1,num=10,queryCase}){
31
+ async list({page=1,num=10,order,queryCase}){
32
32
  page=Number.parseInt(page);
33
33
  num=Number.parseInt(num);
34
34
  let tableName=this.tableName;
@@ -30,9 +30,9 @@ class MysqlBaseRestApi extends AbstractBaseRestApi{
30
30
  return r;
31
31
  }
32
32
 
33
- async list({page,num,queryCase}){
34
- let queryCaseStr= MysqlBaseRestApi.obj2QueryCase(queryCase);
35
- let r= this.dbBaseMapper.selectPage({page,num,queryCase:queryCaseStr});
33
+ async list({page,num,order,queryCase}){
34
+ let queryCaseStr= MysqlBaseRpcApi.obj2QueryCase(queryCase);
35
+ let r= this.dbBaseMapper.selectPage({order,page,num,queryCase:queryCaseStr});
36
36
  return r;
37
37
  }
38
38
 
@@ -11,7 +11,7 @@ class AbstractBaseRpcApi{
11
11
 
12
12
  async add(obj){}
13
13
  async delete(obj){}
14
- async list({page,num,...queryCase}){}
14
+ async list({page,num,order,...queryCase}){}
15
15
  async listAll(obj){}
16
16
  async update(obj){}
17
17
  async getChildenList(id,queryCase){}
@@ -51,10 +51,13 @@ class AbstractBaseRpcApi{
51
51
  });
52
52
 
53
53
  app.get(`${this.prefix}/list`,async (req,res)=>{
54
- const {page,num,...queryCase}=req.params;
55
- let r=await this.list({page,num,queryCase});
54
+ let {page,num,order,...queryCase}=req.params;
55
+ if(!order){
56
+ order=null;
57
+ }
58
+ let r=await this.list({page,num,order,queryCase});
56
59
  res.send(M.successResult(r));
57
- })
60
+ });
58
61
 
59
62
  /**
60
63
  * 如果有parent_id才能返回树
@@ -27,12 +27,12 @@ class ApiCloudBaseRpcApi extends AbstractBaseRpcApi{
27
27
  return r[0];
28
28
  }
29
29
 
30
- async list({page=1,num=10,queryCase}){
30
+ async list({page=1,num=10,order="createdAt ASC",queryCase}){
31
31
  page=Number.parseInt(page);
32
32
  num=Number.parseInt(num);
33
33
  let limit=num;
34
34
  let skip= (page-1)*num
35
- let rows=await this.tableClient.list(queryCase,limit,skip,"createdAt ASC");
35
+ let rows=await this.tableClient.list(queryCase,limit,skip,order);
36
36
  let countResult=await this.tableClient.count(queryCase);
37
37
  return {rows, total:countResult.count}
38
38
  }
@@ -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,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
  }
@@ -25,8 +25,8 @@ class MemoryBaseRpcApi extends AbstractBaseRpcApi{
25
25
  return r;
26
26
  }
27
27
 
28
- async list({page,num,queryCase}){
29
- let r= this.memoryDb.listByPage(page,num,queryCase);
28
+ async list({page,num,order,queryCase}){
29
+ let r= this.memoryDb.listByPage({page,num,caseObj: queryCase,order});
30
30
  return r;
31
31
  }
32
32
 
@@ -28,7 +28,7 @@ class MongoDbBaseRpcApi extends AbstractBaseRpcApi{
28
28
  return r;
29
29
  }
30
30
 
31
- async list({page=1,num=10,queryCase}){
31
+ async list({page=1,num=10,order,queryCase}){
32
32
  page=Number.parseInt(page);
33
33
  num=Number.parseInt(num);
34
34
  let tableName=this.tableName;
@@ -30,9 +30,9 @@ class MysqlBaseRpcApi extends AbstractBaseRpcApi{
30
30
  return r;
31
31
  }
32
32
 
33
- async list({page,num,queryCase}){
33
+ async list({page,num,order,queryCase}){
34
34
  let queryCaseStr= MysqlBaseRpcApi.obj2QueryCase(queryCase);
35
- let r= this.dbBaseMapper.selectPage({page,num,queryCase:queryCaseStr});
35
+ let r= this.dbBaseMapper.selectPage({order,page,num,queryCase:queryCaseStr});
36
36
  return r;
37
37
  }
38
38
 
@@ -0,0 +1,38 @@
1
+ const M = require('../../index.js');
2
+ const { spawn } = require('child_process')
3
+
4
+ let childProcess;
5
+ let timer = null;
6
+
7
+ function debounce(mainFile) {
8
+ timer && clearTimeout(timer)
9
+ timer = setTimeout(() => {
10
+ //console.log('nodemon')
11
+ childProcess && childProcess.kill()
12
+ childProcess = spawn('node', [mainFile], {
13
+ stdio: [process.stdin, process.stdout, process.stderr]
14
+ });
15
+ }, 800)
16
+ }
17
+
18
+ function watchSigleFile(file,mainFile){
19
+ M.watchFile(file,({file,event})=>{
20
+ if(event=="change"){
21
+ console.log("change:"+file);
22
+ debounce(mainFile)
23
+ }
24
+ })
25
+ }
26
+
27
+ module.exports = function (watchFile,mainFile){
28
+ debounce(mainFile);
29
+ let r=[];
30
+ if(Array.isArray(watchFile)) {
31
+ r=watchFile;
32
+ }else {
33
+ r.push(watchFile);
34
+ }
35
+ r.forEach(u=>{
36
+ watchSigleFile(u,mainFile);
37
+ })
38
+ };