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.
Files changed (35) hide show
  1. package/README.md +1 -1
  2. package/beforeTest/ApiCloudBaseRpcApiTest.js +17 -0
  3. package/beforeTest/FileBaseRpcApiTest.js +8 -0
  4. package/beforeTest/MemorDbTest.js +16 -0
  5. package/beforeTest/MemoryBaseRestApiTest.js +8 -0
  6. package/beforeTest/MemoryBaseRpcApiTest.js +8 -0
  7. package/beforeTest/MiApiCloudClientTest.js +16 -0
  8. package/beforeTest/MongoDbBaseRestApiTest.js +19 -0
  9. package/beforeTest/MongoDbBaseRpcApiTest.js +19 -0
  10. package/beforeTest/MySqlBaseRestApiTest.js +13 -0
  11. package/beforeTest/MysqlBaseRpcApiTest.js +18 -0
  12. package/beforeTest/graphql_test.js +12 -0
  13. package/index.js +99 -28
  14. package/ming_node.md +4 -4
  15. package/module/BaseMapper.js +20 -4
  16. package/module/MemoryDb.js +136 -0
  17. package/module/MiApiCloudClient.js +649 -0
  18. package/package.json +1 -1
  19. package/plugins/BaseGraphqlApi/getGraphqlSchema.js +73 -0
  20. package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
  21. package/plugins/BaseGraphqlApi/index.js +18 -0
  22. package/plugins/BaseRestApi/AbstractBaseRestApi.js +59 -0
  23. package/plugins/BaseRestApi/ApiCloudBaseRestApi.js +56 -0
  24. package/plugins/BaseRestApi/FileBaseRestApi.js +62 -0
  25. package/plugins/BaseRestApi/MemoryBaseRestApi.js +50 -0
  26. package/plugins/BaseRestApi/MongoDbBaseRestApi.js +75 -0
  27. package/plugins/BaseRestApi/MysqlBaseRestApi.js +72 -0
  28. package/plugins/BaseRpcApi/AbstractBaseRpcApi.js +72 -0
  29. package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +56 -0
  30. package/plugins/BaseRpcApi/FileBaseRpcApi.js +62 -0
  31. package/plugins/BaseRpcApi/MemoryBaseRpcApi.js +50 -0
  32. package/plugins/BaseRpcApi/MongoDbBaseRpcApi.js +75 -0
  33. package/plugins/BaseRpcApi/MysqlBaseRpcApi.js +72 -0
  34. package/utils/common/CollectionUtils.js +28 -0
  35. package/beforeTest/t1.js +0 -18
@@ -0,0 +1,62 @@
1
+ /**
2
+ * 数据源为内存的rpc风格接口
3
+ */
4
+ const M=require("../../index")
5
+ const AbstractBaseRpcApi=require("./AbstractBaseRpcApi");
6
+
7
+ class FileBaseRpcApi extends AbstractBaseRpcApi{
8
+ constructor(props) {
9
+ super(props);
10
+ }
11
+ async add(obj){
12
+ obj.id = M.randomStr();
13
+ M.addObjToFile(this.tableName, obj);
14
+ return obj;
15
+ }
16
+
17
+ 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
+ }
24
+ return r;
25
+ }
26
+
27
+ async getById(id){
28
+ let r=M.getById(this.tableName,id);
29
+ return r;
30
+ }
31
+
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}
40
+ }
41
+
42
+ async listAll(obj){
43
+ if (obj) {
44
+ return M.listAllObjByPropFile(this.tableName, obj);
45
+ } else {
46
+ return M.getObjByFile(this.tableName);
47
+ }
48
+ }
49
+
50
+ async update(obj){
51
+ let r= M.updateObjByIdFile(this.tableName,obj);
52
+ return r;
53
+ }
54
+
55
+ async getChildenList(id,caseObj){
56
+ let r= this.listAll({parent_id:id,...caseObj});
57
+ return r;
58
+ }
59
+ }
60
+
61
+
62
+ module.exports = FileBaseRpcApi;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * 数据源为内存的rpc风格接口
3
+ */
4
+ const MemoryDb=require("../../module/MemoryDb");
5
+ const AbstractBaseRpcApi=require("./AbstractBaseRpcApi");
6
+
7
+ class MemoryBaseRpcApi extends AbstractBaseRpcApi{
8
+
9
+ constructor(props) {
10
+ super(props);
11
+ this.memoryDb=new MemoryDb(props.tableName);
12
+ }
13
+ async add(obj){
14
+ let r= this.memoryDb.add(obj);
15
+ return r;
16
+ }
17
+
18
+ async delete(obj){
19
+ let r= this.memoryDb.deleteAll(obj);
20
+ return r;
21
+ }
22
+
23
+ async getById(id){
24
+ let r=this.memoryDb.getById(id);
25
+ return r;
26
+ }
27
+
28
+ async list({page,num,queryCase}){
29
+ let r= this.memoryDb.listByPage(page,num,queryCase);
30
+ return r;
31
+ }
32
+
33
+ async listAll(obj){
34
+ let r= this.memoryDb.listAll(obj);
35
+ return r;
36
+ }
37
+
38
+ async update(obj){
39
+ let r= this.memoryDb.update(obj);
40
+ return r;
41
+ }
42
+
43
+ async getChildenList(id,caseObj){
44
+ let r= this.memoryDb.listAll({parent_id:id,...caseObj});
45
+ return r;
46
+ }
47
+ }
48
+
49
+
50
+ module.exports = MemoryBaseRpcApi;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * @type {MemoryDb|{}}
3
+ */
4
+ const M=require("../../index");
5
+ const AbstractBaseRpcApi=require("./AbstractBaseRpcApi");
6
+
7
+
8
+ class MongoDbBaseRpcApi extends AbstractBaseRpcApi{
9
+ constructor(props) {
10
+ super(props);
11
+ this.mongoDb=M.mongoDb;
12
+ }
13
+
14
+ async add(obj){
15
+ let r= this.mongoDb.insert(this.tableName,obj);
16
+ return r;
17
+ }
18
+
19
+ async delete(obj){
20
+ let r= this.mongoDb.remove(this.tableName,{
21
+ _id: M.mongoDb.ObjectID(obj.id)
22
+ });
23
+ return r;
24
+ }
25
+
26
+ async getById(id){
27
+ let r=await this.mongoDb.getById(this.tableName,id);
28
+ return r;
29
+ }
30
+
31
+ async list({page=1,num=10,queryCase}){
32
+ page=Number.parseInt(page);
33
+ num=Number.parseInt(num);
34
+ let tableName=this.tableName;
35
+ return new Promise(async (resolve,reject)=>{
36
+ this.mongoDb.connect().then((db)=>{
37
+ var result=db.collection(this.tableName).find(queryCase,{
38
+ skip:(page-1)*num,
39
+ limit:num
40
+ });
41
+ result.toArray(async function(err,docs){
42
+ if(err){
43
+ reject(err);
44
+ return;
45
+ }
46
+ let total=await db.collection(tableName).count(queryCase);
47
+ resolve({rows:docs,total:total});
48
+ })
49
+ })
50
+ })
51
+ }
52
+
53
+
54
+ async listAll(obj){
55
+ let r= this.mongoDb.find(this.tableName,obj);
56
+ return r;
57
+ }
58
+
59
+ async update(obj){
60
+ let {id,...newObj}=obj;
61
+ let r= this.mongoDb.update(this.tableName,{
62
+ _id: M.mongoDb.ObjectID(id)
63
+ },newObj);
64
+ return r;
65
+ }
66
+
67
+ async getChildenList(id,caseObj){
68
+ let r= this.listAll({parent_id:id,...caseObj});
69
+ return r;
70
+ }
71
+
72
+ }
73
+
74
+
75
+ module.exports = MongoDbBaseRpcApi;
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @type {MemoryDb|{}}
3
+ */
4
+
5
+ const BaseMapper=require("../../module/BaseMapper");
6
+ const AbstractBaseRpcApi=require("./AbstractBaseRpcApi");
7
+
8
+
9
+ class MysqlBaseRpcApi extends AbstractBaseRpcApi{
10
+ constructor(props) {
11
+ super(props);
12
+ this.dbBaseMapper= new BaseMapper(this.tableName);
13
+ }
14
+
15
+ async add(obj){
16
+ if(obj.gmt_create){
17
+ obj.gmt_create=obj.gmt_create.format("yyyy-MM-dd hh:mm:ss");
18
+ }
19
+ let r= this.dbBaseMapper.insert(obj);
20
+ return r;
21
+ }
22
+
23
+ async delete(obj){
24
+ let r= this.dbBaseMapper.delete(obj);
25
+ return r;
26
+ }
27
+
28
+ async getById(id){
29
+ let r=await this.dbBaseMapper.selectById({id});
30
+ return r;
31
+ }
32
+
33
+ async list({page,num,queryCase}){
34
+ let queryCaseStr= MysqlBaseRpcApi.obj2QueryCase(queryCase);
35
+ let r= this.dbBaseMapper.selectPage({page,num,queryCase:queryCaseStr});
36
+ return r;
37
+ }
38
+
39
+ async listAll(obj){
40
+ let queryCase= MysqlBaseRpcApi.obj2QueryCase(obj);
41
+ let r= this.dbBaseMapper.selectList({queryCase:queryCase});
42
+ return r;
43
+ }
44
+
45
+ async update(obj){
46
+ if(obj.gmt_modified){
47
+ obj.gmt_modified=obj.gmt_modified.format("yyyy-MM-dd hh:mm:ss");
48
+ }
49
+ let {id,...newObj}=obj;
50
+ let r= this.dbBaseMapper.update(newObj,`id=${id}`);
51
+ return r;
52
+ }
53
+
54
+ async getChildenList(id,caseObj){
55
+ let r= this.listAll({parent_id:id,...caseObj});
56
+ return r;
57
+ }
58
+
59
+ static obj2QueryCase(obj){
60
+ let queryCase="1=1";
61
+ if(obj && Object.keys(obj).length>0){
62
+ for(var p in obj) {
63
+ queryCase=queryCase+` and ${p}='${obj[p]}' `
64
+ }
65
+ }
66
+ return queryCase;
67
+
68
+ }
69
+ }
70
+
71
+
72
+ module.exports = MysqlBaseRpcApi;
@@ -0,0 +1,28 @@
1
+
2
+
3
+ class CollectionUtils{
4
+ static async selectTree(getChildenList,parent_id=-1,queryCase={}){
5
+ async function addChilden(record){
6
+ let cList= await getChildenList(record.id,queryCase);
7
+ record.childen=cList;
8
+ if(cList.length==0){
9
+ return
10
+ }
11
+ for (let i=0;i<cList.length;i++){
12
+ let cObj= cList[i];
13
+ addChilden(cObj);
14
+ }
15
+ }
16
+
17
+ let rootList=await getChildenList(parent_id,queryCase);
18
+ for (let i=0;i<rootList.length;i++){
19
+ let obj=rootList[i];
20
+ await addChilden(obj);
21
+ }
22
+ return rootList;
23
+ }
24
+
25
+
26
+ }
27
+
28
+ module.exports = CollectionUtils;
package/beforeTest/t1.js DELETED
@@ -1,18 +0,0 @@
1
- var M=require("../index");
2
- var app=M.server();
3
- app.listen(8888);
4
- app.put("/getById/:id",(req,res)=>{
5
- console.log("put",req.params);
6
- res.send("ok");
7
- })
8
-
9
- app.post("/getById/:id",(req,res)=>{
10
- console.log("post",req.params);
11
- res.send("ok");
12
- })
13
-
14
-
15
- app.delete("/getById/:id",(req,res)=>{
16
- console.log("delete",req.params);
17
- res.send("ok");
18
- })