ming_node 2.2.3 → 2.2.5
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -1
- package/beforeTest/ApiCloudBaseRpcApiTest.js +17 -0
- package/beforeTest/FileBaseRpcApiTest.js +8 -0
- package/beforeTest/MemorDbTest.js +16 -0
- package/beforeTest/MemoryBaseRestApiTest.js +8 -0
- package/beforeTest/MemoryBaseRpcApiTest.js +8 -0
- package/beforeTest/MiApiCloudClientTest.js +16 -0
- package/beforeTest/MongoDbBaseRestApiTest.js +19 -0
- package/beforeTest/MongoDbBaseRpcApiTest.js +19 -0
- package/beforeTest/MySqlBaseRestApiTest.js +13 -0
- package/beforeTest/MysqlBaseRpcApiTest.js +18 -0
- package/beforeTest/graphql_test.js +12 -0
- package/index.js +99 -28
- package/ming_node.md +4 -4
- package/module/BaseMapper.js +20 -4
- package/module/MemoryDb.js +136 -0
- package/module/MiApiCloudClient.js +649 -0
- package/package.json +1 -1
- package/plugins/BaseGraphqlApi/getGraphqlSchema.js +73 -0
- package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
- package/plugins/BaseGraphqlApi/index.js +18 -0
- package/plugins/BaseRestApi/AbstractBaseRestApi.js +59 -0
- package/plugins/BaseRestApi/ApiCloudBaseRestApi.js +56 -0
- package/plugins/BaseRestApi/FileBaseRestApi.js +62 -0
- package/plugins/BaseRestApi/MemoryBaseRestApi.js +50 -0
- package/plugins/BaseRestApi/MongoDbBaseRestApi.js +75 -0
- package/plugins/BaseRestApi/MysqlBaseRestApi.js +72 -0
- package/plugins/BaseRpcApi/AbstractBaseRpcApi.js +72 -0
- package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +56 -0
- package/plugins/BaseRpcApi/FileBaseRpcApi.js +62 -0
- package/plugins/BaseRpcApi/MemoryBaseRpcApi.js +50 -0
- package/plugins/BaseRpcApi/MongoDbBaseRpcApi.js +75 -0
- package/plugins/BaseRpcApi/MysqlBaseRpcApi.js +72 -0
- package/utils/common/CollectionUtils.js +28 -0
- package/beforeTest/t1.js +0 -18
package/README.md
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const ApiCloudBaseRpcApi= require("../plugins/BaseRpcApi/ApiCloudBaseRpcApi");
|
3
|
+
const MiApiCloudClient=require("../module/MiApiCloudClient");
|
4
|
+
const apiCloudClient = new MiApiCloudClient("A6032931027980", "FF279F8E-8B09-5F1A-1036-F6AE53F3538D");
|
5
|
+
let apiCloudBaseRpcApi = new ApiCloudBaseRpcApi(
|
6
|
+
{
|
7
|
+
prefix:"ming",
|
8
|
+
tableName:"ming",
|
9
|
+
apiCloudClient
|
10
|
+
}
|
11
|
+
)
|
12
|
+
|
13
|
+
var app=M.server();
|
14
|
+
|
15
|
+
app.listen(8888);
|
16
|
+
|
17
|
+
app.use(apiCloudBaseRpcApi);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const FileBaseRpcApi= require("../plugins/BaseRpcApi/FileBaseRpcApi");
|
3
|
+
|
4
|
+
let fileBaseRpcApi = new FileBaseRpcApi({prefix:"ming", tableName:"a.json",generateTime:true})
|
5
|
+
var app=M.server();
|
6
|
+
app.listen(8888);
|
7
|
+
|
8
|
+
app.use(fileBaseRpcApi);
|
@@ -0,0 +1,8 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const MemoryBaseRestApi= require("../plugins/BaseRestApi/MemoryBaseRestApi");
|
3
|
+
|
4
|
+
let memoryBaseRestApi = new MemoryBaseRestApi({tableName:"ming",generateTime:true})
|
5
|
+
var app=M.server();
|
6
|
+
app.listen(8888);
|
7
|
+
|
8
|
+
app.use(memoryBaseRestApi);
|
@@ -0,0 +1,16 @@
|
|
1
|
+
const MiApiCloudClient=require("../module/MiApiCloudClient")
|
2
|
+
|
3
|
+
|
4
|
+
M.MiApiCloudClient = new MiApiCloudClient("A6032931027980", "FF279F8E-8B09-5F1A-1036-F6AE53F3538D").tableClient("mi_user");
|
5
|
+
|
6
|
+
|
7
|
+
mi_resource.list({},null,null,"sort").then(d => {
|
8
|
+
let result = { rows: d }
|
9
|
+
console.log(result)
|
10
|
+
})
|
11
|
+
|
12
|
+
// mi_resource.add({
|
13
|
+
// username:"minglie2234"
|
14
|
+
// }).then(d=>{
|
15
|
+
// console.log(d)
|
16
|
+
// })
|
@@ -0,0 +1,19 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
|
3
|
+
|
4
|
+
Db= M.getMongoDB({
|
5
|
+
dbUrl: "mongodb://root:123456@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=miapi",
|
6
|
+
dbName:"miapi"
|
7
|
+
})
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
const MongoDbBaseRestApi= require("../plugins/BaseRestApi/MongoDbBaseRestApi");
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
let mongoDbBaseRestApi = new MongoDbBaseRestApi({tableName:"test",prefix:"ming", generateTime:true})
|
16
|
+
var app=M.server();
|
17
|
+
app.listen(8888);
|
18
|
+
|
19
|
+
app.use(mongoDbBaseRestApi);
|
@@ -0,0 +1,19 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
|
3
|
+
|
4
|
+
Db= M.getMongoDB({
|
5
|
+
dbUrl: "mongodb://root:123456@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=miapi",
|
6
|
+
dbName:"miapi"
|
7
|
+
})
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
const MongoDbBaseRpcApi= require("../plugins/BaseRpcApi/MongoDbBaseRpcApi");
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
let mongoDbBaseRpcApi = new MongoDbBaseRpcApi({tableName:"test",prefix:"ming", generateTime:true})
|
16
|
+
var app=M.server();
|
17
|
+
app.listen(8888);
|
18
|
+
|
19
|
+
app.use(mongoDbBaseRpcApi);
|
@@ -0,0 +1,13 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
const MysqlBaseRestApi= require("../plugins/BaseRestApi/MysqlBaseRestApi");
|
3
|
+
|
4
|
+
M.getMySql({
|
5
|
+
database:"miapi"
|
6
|
+
})
|
7
|
+
|
8
|
+
|
9
|
+
let mysqlBaseRestApi = new MysqlBaseRestApi({tableName:"ming",generateTime:true})
|
10
|
+
var app=M.server();
|
11
|
+
app.listen(8888);
|
12
|
+
|
13
|
+
app.use(mysqlBaseRestApi);
|
@@ -0,0 +1,18 @@
|
|
1
|
+
var M=require("../index");
|
2
|
+
|
3
|
+
|
4
|
+
M.getMySql({
|
5
|
+
database:"miapi"
|
6
|
+
})
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
const MysqlBaseRpcApi= require("../plugins/BaseRpcApi/MysqlBaseRpcApi");
|
11
|
+
|
12
|
+
|
13
|
+
|
14
|
+
let mysqlBaseRpcApi = new MysqlBaseRpcApi({tableName:"ming",generateTime:true})
|
15
|
+
var app=M.server();
|
16
|
+
app.listen(8888);
|
17
|
+
|
18
|
+
app.use(mysqlBaseRpcApi);
|
@@ -0,0 +1,12 @@
|
|
1
|
+
var M = require('../index');
|
2
|
+
var BaseMapper=require("../module/BaseMapper")
|
3
|
+
var Db=M.getMySql({
|
4
|
+
"database" : "miapi"
|
5
|
+
})
|
6
|
+
let dbBaseMapper= new BaseMapper("person");
|
7
|
+
const BaseGraphqlApi= require("../plugins/BaseGraphqlApi/index");
|
8
|
+
let baseGraphqlApi = new BaseGraphqlApi({prefix:"person", dbBaseMapper});
|
9
|
+
|
10
|
+
var app = M.server()
|
11
|
+
app.listen(4000)
|
12
|
+
app.use(baseGraphqlApi);
|
package/index.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
* File :
|
2
|
+
* File : MemoryBaseRpcApi.js
|
3
3
|
* By : Minglie
|
4
4
|
* QQ: 934031452
|
5
|
-
* Date :2021.
|
6
|
-
* version :2.2.
|
5
|
+
* Date :2021.12.01
|
6
|
+
* version :2.2.5
|
7
7
|
*/
|
8
8
|
var http = require('http');
|
9
9
|
var https = require('https');
|
@@ -32,6 +32,7 @@
|
|
32
32
|
M.httpProxy = {};// http 代理配置
|
33
33
|
M._sseClientMap=new Map();
|
34
34
|
M._sseHeatTime=3000;
|
35
|
+
M._moduleMap=new Map();//模块map
|
35
36
|
M.httpBefore = (d) => {
|
36
37
|
return d
|
37
38
|
}
|
@@ -47,7 +48,16 @@
|
|
47
48
|
M.remoteStaticPathEnable = true;
|
48
49
|
//代理服务器配置
|
49
50
|
M.proxyHost = "http://127.0.0.1:8888"
|
50
|
-
M.proxyHost = ""
|
51
|
+
M.proxyHost = "";
|
52
|
+
|
53
|
+
M.setModule=function (key,module){
|
54
|
+
M._moduleMap.set(key,module);
|
55
|
+
}
|
56
|
+
M.getModule=function (key){
|
57
|
+
M._moduleMap.get(key);
|
58
|
+
}
|
59
|
+
|
60
|
+
|
51
61
|
/**
|
52
62
|
* ----------------------客户端START--------------------------------------------
|
53
63
|
*/
|
@@ -657,13 +667,14 @@ M.request.put=M.put;
|
|
657
667
|
}
|
658
668
|
}
|
659
669
|
M.writeObjToFile(file, d);
|
670
|
+
return d_num;
|
660
671
|
}
|
661
672
|
|
662
673
|
M.updateObjByIdFile = function (file, obj) {
|
663
674
|
var d = M.getObjByFile(file);
|
664
675
|
for (let i = 0; i < d.length; i++) {
|
665
676
|
if (d[i].id == obj.id) {
|
666
|
-
d.splice(i, 1, obj);
|
677
|
+
d.splice(i, 1, Object.assign(d[i],obj));
|
667
678
|
break;
|
668
679
|
}
|
669
680
|
}
|
@@ -677,17 +688,28 @@ M.request.put=M.put;
|
|
677
688
|
}
|
678
689
|
}
|
679
690
|
}
|
680
|
-
M.listAllObjByPropFile = function (file,
|
681
|
-
|
682
|
-
let
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
691
|
+
M.listAllObjByPropFile = function (file, caseObj) {
|
692
|
+
var d = M.getObjByFile(file);
|
693
|
+
let o_keys = Object.keys(caseObj);
|
694
|
+
if (caseObj && o_keys.length>0) {
|
695
|
+
let r_list = [];
|
696
|
+
let o_vals = Object.values(caseObj);
|
697
|
+
for (let i = 0; i < d.length; i++) {
|
698
|
+
let s=0;
|
699
|
+
for (let j=0;j<o_keys.length;j++){
|
700
|
+
if (d[i][o_keys[j]] != o_vals[j]) {
|
701
|
+
break
|
702
|
+
}
|
703
|
+
s++;
|
704
|
+
}
|
705
|
+
if(s==o_keys.length){
|
706
|
+
r_list.push(d[i]);
|
707
|
+
}
|
688
708
|
}
|
709
|
+
return r_list;
|
710
|
+
} else {
|
711
|
+
return d;
|
689
712
|
}
|
690
|
-
return r_list;
|
691
713
|
}
|
692
714
|
/**
|
693
715
|
* 文件型数据库第二层封装
|
@@ -976,6 +998,7 @@ M.getMySql = function (dbConfig) {
|
|
976
998
|
timezone: "08:00"
|
977
999
|
}
|
978
1000
|
var Db = {};
|
1001
|
+
Db.dbConfig=defaultDbConfig;
|
979
1002
|
console.log("connect mysql", defaultDbConfig)
|
980
1003
|
var pool = mysql.createPool(defaultDbConfig);
|
981
1004
|
Db.pool=pool;
|
@@ -1209,8 +1232,9 @@ M.getMongoDB = function (dbConfig) {
|
|
1209
1232
|
}
|
1210
1233
|
|
1211
1234
|
}
|
1212
|
-
|
1213
|
-
|
1235
|
+
MingMongoClient.ObjectID=(id)=> new ObjectID(id)
|
1236
|
+
let Db=MingMongoClient;
|
1237
|
+
Db.dbConfig=Config;
|
1214
1238
|
MingMongoClient.collectionName="test"
|
1215
1239
|
M.mongoDb=Db;
|
1216
1240
|
return Db;
|
@@ -1358,6 +1382,40 @@ M.failResult=(msg,code,d)=>{
|
|
1358
1382
|
M.randomStr = function () {
|
1359
1383
|
return (Math.random().toString(36) + new Date().getTime()).slice(2);
|
1360
1384
|
}
|
1385
|
+
|
1386
|
+
M.urlStringify = function (obj) {
|
1387
|
+
if (obj !== null && typeof obj === 'object') {
|
1388
|
+
var keys = Object.keys(obj);
|
1389
|
+
var len = keys.length;
|
1390
|
+
var flast = len - 1;
|
1391
|
+
var fields = '';
|
1392
|
+
for (var i = 0; i < len; ++i) {
|
1393
|
+
var k = keys[i];
|
1394
|
+
var v = obj[k];
|
1395
|
+
var ks = k + "=";
|
1396
|
+
fields += ks + v;
|
1397
|
+
if (i < flast)
|
1398
|
+
fields += "&";
|
1399
|
+
}
|
1400
|
+
return fields;
|
1401
|
+
}
|
1402
|
+
return '';
|
1403
|
+
};
|
1404
|
+
|
1405
|
+
M.urlParse = function (url) {
|
1406
|
+
url = url.substr(url.indexOf("?") + 1);
|
1407
|
+
var t, n, r, i = url, s = {};
|
1408
|
+
t = i.split("&"),
|
1409
|
+
r = null,
|
1410
|
+
n = null;
|
1411
|
+
for (var o in t) {
|
1412
|
+
var u = t[o].indexOf("=");
|
1413
|
+
u !== -1 && (r = t[o].substr(0, u),
|
1414
|
+
n = t[o].substr(u + 1),
|
1415
|
+
s[r] = n)
|
1416
|
+
}
|
1417
|
+
return s
|
1418
|
+
};
|
1361
1419
|
|
1362
1420
|
/**
|
1363
1421
|
* 异常处理钩子
|
@@ -1663,15 +1721,30 @@ M.failResult=(msg,code,d)=>{
|
|
1663
1721
|
G._server = callback;
|
1664
1722
|
}
|
1665
1723
|
app.use=function (url,callback){
|
1666
|
-
if
|
1667
|
-
url
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1724
|
+
if(typeof url === 'function' || typeof url === 'object' ){
|
1725
|
+
let plugin=url;
|
1726
|
+
let args=callback;
|
1727
|
+
if(plugin.installed){
|
1728
|
+
return app;
|
1729
|
+
}
|
1730
|
+
if (typeof plugin === 'function') {
|
1731
|
+
plugin(app, args);
|
1732
|
+
} else {
|
1733
|
+
plugin.install(app, args);
|
1734
|
+
}
|
1735
|
+
plugin.installed = true;
|
1736
|
+
}else {
|
1737
|
+
if (Array.isArray(url)) {
|
1738
|
+
url.forEach(u=>{
|
1739
|
+
let regExp=new RegExp(u)
|
1740
|
+
G._use[u] = {url,regExp,callback};
|
1741
|
+
})
|
1742
|
+
} else {
|
1743
|
+
let regExp=new RegExp(url)
|
1744
|
+
G._use[url] = {url,regExp,callback};
|
1745
|
+
}
|
1674
1746
|
}
|
1747
|
+
return app;
|
1675
1748
|
}
|
1676
1749
|
/**
|
1677
1750
|
* 注册get请求
|
@@ -1766,9 +1839,8 @@ M.failResult=(msg,code,d)=>{
|
|
1766
1839
|
app.set("gloable_exception_handle",(err,req,res)=>{
|
1767
1840
|
console.error(err.stack)
|
1768
1841
|
if (res && !res.alreadySend) {
|
1769
|
-
|
1770
|
-
res.
|
1771
|
-
res.end();
|
1842
|
+
// res.writeHead(500, { "Content-Type": "text/j;charset='utf-8'" });
|
1843
|
+
res.send(M.result(err.message,false,-1));
|
1772
1844
|
}
|
1773
1845
|
})
|
1774
1846
|
|
@@ -1812,7 +1884,6 @@ M.failResult=(msg,code,d)=>{
|
|
1812
1884
|
console.log("listen on port:" + port);
|
1813
1885
|
return server;
|
1814
1886
|
}
|
1815
|
-
|
1816
1887
|
return app;
|
1817
1888
|
}
|
1818
1889
|
M["_gloable_exception_handle"]=(err)=>{
|
package/ming_node.md
CHANGED
@@ -1001,7 +1001,7 @@ console.log(new Date().format("yyyy-MM-dd"))
|
|
1001
1001
|
在含有static文件夹的目录执行下面命令,static便作为web根目录
|
1002
1002
|
```bash
|
1003
1003
|
#node
|
1004
|
-
curl https://minglie.github.io/js/
|
1004
|
+
curl https://minglie.github.io/js/MemoryBaseRpcApi.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1005
1005
|
#python
|
1006
1006
|
curl https://minglie.github.io/python/index.py > index.py && python index.py
|
1007
1007
|
|
@@ -1009,12 +1009,12 @@ curl https://minglie.github.io/python/index.py > index.py && python index.py
|
|
1009
1009
|
git clone https://github.com/minglie/ming_mockServer.git && cd ming_mockServer && npm i && npm run start
|
1010
1010
|
|
1011
1011
|
#curl启动ming_mockServer0
|
1012
|
-
curl https://minglie.gitee.io/mingpage/static/js/ming_mockServer0.js >
|
1012
|
+
curl https://minglie.gitee.io/mingpage/static/js/ming_mockServer0.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1013
1013
|
|
1014
1014
|
```
|
1015
1015
|
## 当前目录静态页
|
1016
1016
|
```javascript
|
1017
|
-
curl https://minglie.gitee.io/mingpage/static/js/index_cur.js >
|
1017
|
+
curl https://minglie.gitee.io/mingpage/static/js/index_cur.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1018
1018
|
```
|
1019
1019
|
```javascript
|
1020
1020
|
+async function(){
|
@@ -1082,7 +1082,7 @@ app.post("/axios", async (req, res) => {
|
|
1082
1082
|
## 写web接口最快捷的方式ming_share_edit
|
1083
1083
|
运行脚本, 访问 [http://localhost:8888/](http://localhost:8888/)
|
1084
1084
|
```bash
|
1085
|
-
curl https://minglie.gitee.io/mi/i2.js >
|
1085
|
+
curl https://minglie.gitee.io/mi/i2.js > MemoryBaseRpcApi.js && node MemoryBaseRpcApi.js
|
1086
1086
|
```
|
1087
1087
|
![image.png](https://ming-bucket-01.oss-cn-beijing.aliyuncs.com/yuque/1591515706715-62d3b6f6-f113-497f-a532-63792180cd09.png#align=left&display=inline&height=506&margin=%5Bobject%20Object%5D&name=image.png&originHeight=506&originWidth=720&size=65942&status=done&style=none&width=720)
|
1088
1088
|
## ming_api_mock
|
package/module/BaseMapper.js
CHANGED
@@ -1,9 +1,14 @@
|
|
1
|
-
const M=require("
|
2
|
-
const Db=M.getMySql({})
|
1
|
+
const M=require("../index")
|
2
|
+
const Db=M.getMySql({});
|
3
3
|
|
4
4
|
class BaseMapper {
|
5
|
+
|
6
|
+
//static BaseMapperMap=new Map();
|
7
|
+
|
5
8
|
constructor(tableName) {
|
6
9
|
this.tableName =tableName;
|
10
|
+
this.tableSchema=null;
|
11
|
+
//BaseMapper.BaseMapperMap.set(tableName,this);
|
7
12
|
}
|
8
13
|
|
9
14
|
/**
|
@@ -12,6 +17,7 @@ class BaseMapper {
|
|
12
17
|
* @returns {Promise<*>}
|
13
18
|
*/
|
14
19
|
async insert(obj){
|
20
|
+
delete obj.id;
|
15
21
|
let sql= BaseMapper.getInsertObjSql(this.tableName,obj)
|
16
22
|
let r=await Db.doSql(sql);
|
17
23
|
return r;
|
@@ -52,7 +58,7 @@ class BaseMapper {
|
|
52
58
|
}
|
53
59
|
|
54
60
|
/**
|
55
|
-
|
61
|
+
* 删除
|
56
62
|
* @param caseStr
|
57
63
|
* @returns {Promise<*>}
|
58
64
|
*/
|
@@ -139,7 +145,7 @@ class BaseMapper {
|
|
139
145
|
let dataList=await Db.doSql(`SELECT ${queryColumn} FROM ${this.tableName} where ${whereCase} order by ${order} LIMIT ${start},${num}`)
|
140
146
|
let totalR=await Db.doSql(`SELECT count(1) c FROM ${this.tableName} where ${whereCase}`)
|
141
147
|
let total=totalR[0].c
|
142
|
-
return {dataList,total};
|
148
|
+
return {rows:dataList,total};
|
143
149
|
}
|
144
150
|
|
145
151
|
/**
|
@@ -213,6 +219,14 @@ class BaseMapper {
|
|
213
219
|
return rootList;
|
214
220
|
}
|
215
221
|
|
222
|
+
async getTableSchema(){
|
223
|
+
if(this.tableSchema==null){
|
224
|
+
let dataBaseName=Db.dbConfig.database;
|
225
|
+
let sql=`select COLUMN_NAME,COLUMN_TYPE,COLUMN_COMMENT from information_schema.columns where table_schema ='${dataBaseName}' and table_name = '${this.tableName}';`
|
226
|
+
let tableSchema=await Db.doSql(sql);
|
227
|
+
return tableSchema;
|
228
|
+
}
|
229
|
+
}
|
216
230
|
|
217
231
|
|
218
232
|
static getColumn(columns){
|
@@ -289,6 +303,8 @@ class BaseMapper {
|
|
289
303
|
return sql;
|
290
304
|
}
|
291
305
|
|
306
|
+
|
307
|
+
|
292
308
|
}
|
293
309
|
|
294
310
|
|
@@ -0,0 +1,136 @@
|
|
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 MemoryDb{
|
12
|
+
|
13
|
+
constructor(tableName) {
|
14
|
+
this.tableName=tableName;
|
15
|
+
this.dataList=[]
|
16
|
+
}
|
17
|
+
|
18
|
+
/**
|
19
|
+
* 单个加
|
20
|
+
* @param obj
|
21
|
+
* @returns {Promise<*>}
|
22
|
+
*/
|
23
|
+
async add(obj){
|
24
|
+
obj.id = M.randomStr();
|
25
|
+
this.dataList=[...this.dataList,obj];
|
26
|
+
return obj;
|
27
|
+
}
|
28
|
+
|
29
|
+
async update(obj){
|
30
|
+
for (let i = 0; i < this.dataList.length; i++) {
|
31
|
+
if (this.dataList[i].id == obj.id) {
|
32
|
+
this.dataList[i]=obj;
|
33
|
+
return
|
34
|
+
}
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
listAll(caseObj){
|
39
|
+
let o_keys = Object.keys(caseObj);
|
40
|
+
if (caseObj && o_keys.length>0) {
|
41
|
+
let r_list = [];
|
42
|
+
let o_vals = Object.values(caseObj);
|
43
|
+
var d = this.dataList;
|
44
|
+
for (let i = 0; i < d.length; i++) {
|
45
|
+
let s=0;
|
46
|
+
for (let j=0;j<o_keys.length;j++){
|
47
|
+
if (d[i][o_keys[j]] != o_vals[j]) {
|
48
|
+
break
|
49
|
+
}
|
50
|
+
s++;
|
51
|
+
}
|
52
|
+
if(s==o_keys.length){
|
53
|
+
r_list.push(d[i]);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
return r_list;
|
57
|
+
} else {
|
58
|
+
return this.dataList;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
|
63
|
+
listByPage(startPage=0, limit=10, caseObj) {
|
64
|
+
startPage=Number.parseInt(startPage);
|
65
|
+
limit=Number.parseInt(limit);
|
66
|
+
if (startPage <= 0) startPage = 1;
|
67
|
+
let rows;
|
68
|
+
if (caseObj) {
|
69
|
+
rows = this.listAll(caseObj);
|
70
|
+
} else {
|
71
|
+
rows = this.listAll();
|
72
|
+
}
|
73
|
+
let total = rows.length;
|
74
|
+
rows=JSON.parse(JSON.stringify(rows))
|
75
|
+
rows = rows.splice((startPage - 1) * limit, limit)
|
76
|
+
return {rows, total}
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
deleteAll(o) {
|
81
|
+
if (o) {
|
82
|
+
let r_list = [];
|
83
|
+
let o_keys = Object.keys(o);
|
84
|
+
let o_vals = Object.values(o)
|
85
|
+
var d = this.dataList;
|
86
|
+
let delete_index=[]
|
87
|
+
for (let i = 0; i < d.length; i++) {
|
88
|
+
let s=0;
|
89
|
+
for (let j=0;j<o_keys.length;j++){
|
90
|
+
if (d[i][o_keys[j]] != o_vals[j]) {
|
91
|
+
break
|
92
|
+
}
|
93
|
+
s++;
|
94
|
+
}
|
95
|
+
if(s==o_keys.length){
|
96
|
+
delete_index.push(i)
|
97
|
+
}
|
98
|
+
}
|
99
|
+
for (let i = 0; i < d.length; i++) {
|
100
|
+
if(!delete_index.includes(i)){
|
101
|
+
r_list.push(d[i])
|
102
|
+
}
|
103
|
+
}
|
104
|
+
this.dataList=r_list;
|
105
|
+
return delete_index.length;
|
106
|
+
} else {
|
107
|
+
let length=this.dataList.length;
|
108
|
+
this.dataList=[];
|
109
|
+
return length;
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
getById(id) {
|
114
|
+
var d = this.dataList;
|
115
|
+
for (let i = 0; i < d.length; i++) {
|
116
|
+
if(d[i].id==id){
|
117
|
+
return d[i];
|
118
|
+
}
|
119
|
+
}
|
120
|
+
return null;
|
121
|
+
}
|
122
|
+
|
123
|
+
deleteById(id) {
|
124
|
+
var d = this.dataList;
|
125
|
+
for (let i = 0; i < d.length; i++) {
|
126
|
+
if(d[i].id==id){
|
127
|
+
this.dataList.splice(i, 1);
|
128
|
+
return id;
|
129
|
+
}
|
130
|
+
}
|
131
|
+
return 0;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
module.exports = MemoryDb;
|