ming_node 2.1.0 → 2.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) 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/beforeTest/sseserver.js +44 -0
  14. package/beforeTest/static/ssetest.html +21 -0
  15. package/index.js +187 -49
  16. package/ming_node.md +4 -4
  17. package/module/BaseMapper.js +73 -35
  18. package/module/MemoryDb.js +136 -0
  19. package/module/MiApiCloudClient.js +649 -0
  20. package/package.json +1 -1
  21. package/plugins/BaseGraphqlApi/getGraphqlSchema.js +73 -0
  22. package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
  23. package/plugins/BaseGraphqlApi/index.js +18 -0
  24. package/plugins/BaseRestApi/AbstractBaseRestApi.js +59 -0
  25. package/plugins/BaseRestApi/ApiCloudBaseRestApi.js +56 -0
  26. package/plugins/BaseRestApi/FileBaseRestApi.js +62 -0
  27. package/plugins/BaseRestApi/MemoryBaseRestApi.js +50 -0
  28. package/plugins/BaseRestApi/MongoDbBaseRestApi.js +75 -0
  29. package/plugins/BaseRestApi/MysqlBaseRestApi.js +72 -0
  30. package/plugins/BaseRpcApi/AbstractBaseRpcApi.js +72 -0
  31. package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +56 -0
  32. package/plugins/BaseRpcApi/FileBaseRpcApi.js +62 -0
  33. package/plugins/BaseRpcApi/MemoryBaseRpcApi.js +50 -0
  34. package/plugins/BaseRpcApi/MongoDbBaseRpcApi.js +75 -0
  35. package/plugins/BaseRpcApi/MysqlBaseRpcApi.js +72 -0
  36. package/utils/common/CollectionUtils.js +28 -0
  37. package/beforeTest/t1.js +0 -18
@@ -0,0 +1,73 @@
1
+ M=require("../../index");
2
+ const Db=M.getMySql({});
3
+ const{
4
+ GraphQLObjectType,
5
+ GraphQLString,
6
+ GraphQLInt,
7
+ GraphQLSchema,
8
+ GraphQLList,
9
+ GraphQLNonNull
10
+ } = require('graphql');
11
+
12
+
13
+
14
+
15
+ function getGraphqlSchema({dbBaseMapper}){
16
+ const ResponseType = new GraphQLObjectType({
17
+ name:'response',
18
+ fields:{
19
+ status:{type:GraphQLInt},
20
+ msg:{type:GraphQLString}
21
+ }
22
+ })
23
+
24
+ const qlObjectType = new GraphQLObjectType({
25
+ name:'person',
26
+ fields:{
27
+ id:{type:GraphQLInt},
28
+ name:{type:GraphQLString}
29
+ }
30
+ })
31
+
32
+ const RootQuery = new GraphQLObjectType({
33
+ name:'personQuery',
34
+ fields:{
35
+ person:{
36
+ type: new GraphQLList(qlObjectType),
37
+ args:{
38
+ id:{type:GraphQLInt},
39
+ name:{type:GraphQLString}
40
+ },
41
+ async resolve(parentValue,args){
42
+ return Db.doSql(`select * from person`);
43
+ }
44
+ }
45
+ }
46
+ })
47
+
48
+ var mutation = new GraphQLObjectType({
49
+ name:"Mutation",
50
+ fields:{
51
+ addPerson:{
52
+ type:ResponseType,
53
+ args:{
54
+ name:{type:GraphQLString}
55
+ },
56
+ async resolve(parenValue,args){
57
+ //await personDao.insert(args.name)
58
+ return {status:0,msg:"ok"}
59
+ }
60
+ }
61
+ }
62
+
63
+ })
64
+ const schema = new GraphQLSchema({
65
+ query: RootQuery,
66
+ mutation: mutation
67
+ })
68
+ return schema;
69
+ }
70
+
71
+
72
+
73
+ module.exports=getGraphqlSchema;
@@ -0,0 +1,76 @@
1
+ M=require("../../index")
2
+
3
+ const{
4
+ GraphQLObjectType,
5
+ GraphQLString,
6
+ GraphQLInt,
7
+ GraphQLSchema,
8
+ GraphQLList,
9
+ GraphQLNonNull
10
+ } = require('graphql');
11
+
12
+
13
+
14
+
15
+ function getGraphqlSchema({dbBaseMapper}){
16
+
17
+ const ResponseType = new GraphQLObjectType({
18
+ name:'response',
19
+ fields:{
20
+ status:{type:GraphQLInt},
21
+ msg:{type:GraphQLString}
22
+ }
23
+ })
24
+
25
+ const qlObjectType = new GraphQLObjectType({
26
+ name:'person',
27
+ fields:{
28
+ id:{type:GraphQLInt},
29
+ name:{type:GraphQLString}
30
+ }
31
+ })
32
+
33
+ const RootQuery = new GraphQLObjectType({
34
+ name:'personQuery',
35
+ fields:{
36
+ person:{
37
+ type: new GraphQLList(qlObjectType),
38
+ args:{
39
+ id:{type:GraphQLInt},
40
+ name:{type:GraphQLString}
41
+ },
42
+ async resolve(parentValue,args){
43
+ return Db.doSql(`select * from person`);
44
+ }
45
+ }
46
+ }
47
+ })
48
+
49
+
50
+
51
+ var mutation = new GraphQLObjectType({
52
+ name:"Mutation",
53
+ fields:{
54
+ addPerson:{
55
+ type:ResponseType,
56
+ args:{
57
+ name:{type:GraphQLString}
58
+ },
59
+ async resolve(parenValue,args){
60
+ //await personDao.insert(args.name)
61
+ return {status:0,msg:"ok"}
62
+ }
63
+ }
64
+ }
65
+
66
+ })
67
+ const schema = new GraphQLSchema({
68
+ query: RootQuery,
69
+ mutation: mutation
70
+ })
71
+ return schema;
72
+ }
73
+
74
+
75
+
76
+ module.exports=getGraphqlSchema;
@@ -0,0 +1,18 @@
1
+ var getGraphqlSchema= require("./getGraphqlSchema")
2
+ const { graphqlHTTP } = require('express-graphql')
3
+
4
+ class BaseGraphqlApi{
5
+ constructor({dbBaseMapper,prefix}) {
6
+ this.dbBaseMapper=dbBaseMapper;
7
+ this.tableName=dbBaseMapper.tableName;
8
+ this.prefix=prefix?prefix:tableName;
9
+ }
10
+ install(app,args){
11
+ let schema= getGraphqlSchema( this.dbBaseMapper)
12
+ app.mapping(`/${this.prefix}`,graphqlHTTP({
13
+ schema:schema,
14
+ graphiql:true
15
+ }))
16
+ }
17
+ }
18
+ module.exports = BaseGraphqlApi;
@@ -0,0 +1,59 @@
1
+ const M=require("../../index");
2
+
3
+ class AbstractBaseRest{
4
+
5
+ constructor({tableName,prefix,generateTime=false}) {
6
+ this.tableName=tableName;
7
+ this.prefix=prefix?prefix:tableName;
8
+ this.generateTime=generateTime;
9
+ }
10
+
11
+ async add(obj){}
12
+ async delete(obj){}
13
+ async list({page,num,...queryCase}){}
14
+ async listAll(obj){}
15
+ async update(obj){}
16
+ async getChildenList(id,queryCase){}
17
+
18
+ install(app,args){
19
+ app.post(`${this.prefix}`,async (req,res)=>{
20
+ const params=req.params;
21
+ if(this.generateTime){
22
+ params.gmt_create=new Date();
23
+ }
24
+ let r=await this.add(params)
25
+ res.send(M.successResult(r));
26
+ })
27
+ app.delete(`${this.prefix}`,async (req,res)=>{
28
+ let r=await this.delete(req.params);
29
+ res.send(M.successResult(r));
30
+ });
31
+
32
+ /**
33
+ * 修改
34
+ */
35
+ app.put(`${this.prefix}`,async (req,res)=>{
36
+ const params=req.params;
37
+ if(this.generateTime){
38
+ params.gmt_modified=new Date();
39
+ }
40
+ await this.update(params);
41
+ res.send(M.successResult());
42
+ });
43
+
44
+ app.get(`${this.prefix}/:id`,async (req,res)=>{
45
+ const {id,page,num,...queryCase}=req.params;
46
+ if(id==undefined){
47
+ let r=await this.list({page,num,queryCase});
48
+ res.send(M.successResult(r));
49
+ }else {
50
+ let r=await this.getById(req.params.id);
51
+ res.send(M.successResult(r));
52
+ }
53
+ });
54
+ }
55
+ }
56
+
57
+
58
+
59
+ module.exports = AbstractBaseRest;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * 数据源为内存的rpc风格接口
3
+ */
4
+ const AbstractBaseRestApi=require("./AbstractBaseRestApi");
5
+
6
+
7
+ class ApiCloudBaseRestApi extends AbstractBaseRestApi{
8
+ constructor(props) {
9
+ super(props);
10
+ this.appid=props.appid;
11
+ this.appkey=props.appkey;
12
+ this.apiCloudClient = props.apiCloudClient;
13
+ this.tableClient= this.apiCloudClient.tableClient(props.tableName);
14
+ }
15
+ async add(obj){
16
+ let r= await this.tableClient.add(obj);
17
+ return r;
18
+ }
19
+
20
+ async delete(obj){
21
+ let r= await this.tableClient.delete(obj);
22
+ return r;
23
+ }
24
+
25
+ async getById(id){
26
+ let r=await this.tableClient.list({id},null,null,"sort");
27
+ return r[0];
28
+ }
29
+
30
+ async list({page=1,num=10,queryCase}){
31
+ page=Number.parseInt(page);
32
+ num=Number.parseInt(num);
33
+ let limit=num;
34
+ let skip= (page-1)*num
35
+ let r=await this.tableClient.list(queryCase,limit,skip,"createdAt ASC");
36
+ return r;
37
+ }
38
+
39
+ async listAll(obj){
40
+ let r=await this.tableClient.list(obj,null,null,"sort");
41
+ return r;
42
+ }
43
+
44
+ async update(obj){
45
+ let r= await this.tableClient.update(obj);
46
+ return r;
47
+ }
48
+
49
+ async getChildenList(id,caseObj){
50
+ let r= this.listAll({parent_id:id,...caseObj});
51
+ return r;
52
+ }
53
+ }
54
+
55
+
56
+ module.exports = ApiCloudBaseRestApi;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * 数据源为内存的rpc风格接口
3
+ */
4
+ const M=require("../../index")
5
+ const AbstractBaseRestApi=require("./AbstractBaseRestApi");
6
+
7
+ class FileBaseRestApi extends AbstractBaseRestApi{
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 = FileBaseRestApi;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * 数据源为内存的rpc风格接口
3
+ */
4
+ const MemoryDb=require("../../module/MemoryDb");
5
+ const AbstractBaseRestApi=require("./AbstractBaseRestApi");
6
+
7
+ class MemoryBaseRestApi extends AbstractBaseRestApi{
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 = MemoryBaseRestApi;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * @type {MemoryDb|{}}
3
+ */
4
+ const M=require("../../index");
5
+ const AbstractBaseRestApi=require("./AbstractBaseRestApi");
6
+
7
+
8
+ class MongoDbBaseRestApi extends AbstractBaseRestApi{
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 = MongoDbBaseRestApi;
@@ -0,0 +1,72 @@
1
+ /**
2
+ * @type {MemoryDb|{}}
3
+ */
4
+
5
+ const BaseMapper=require("../../module/BaseMapper");
6
+ const AbstractBaseRestApi=require("./AbstractBaseRestApi");
7
+
8
+
9
+ class MysqlBaseRestApi extends AbstractBaseRestApi{
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= MysqlBaseRestApi.obj2QueryCase(queryCase);
35
+ let r= this.dbBaseMapper.selectPage({page,num,queryCase:queryCaseStr});
36
+ return r;
37
+ }
38
+
39
+ async listAll(obj){
40
+ let queryCase= MysqlBaseRestApi.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 = MysqlBaseRestApi;
@@ -0,0 +1,72 @@
1
+ const CollectionUtils=require("../../utils/common/CollectionUtils");
2
+ const M=require("../../index");
3
+
4
+ class AbstractBaseRpcApi{
5
+
6
+ constructor({tableName,prefix,generateTime=false}) {
7
+ this.tableName=tableName;
8
+ this.prefix=prefix?prefix:tableName;
9
+ this.generateTime=generateTime;
10
+ }
11
+
12
+ async add(obj){}
13
+ async delete(obj){}
14
+ async list({page,num,...queryCase}){}
15
+ async listAll(obj){}
16
+ async update(obj){}
17
+ async getChildenList(id,queryCase){}
18
+
19
+ install(app,args){
20
+ app.post(`${this.prefix}/add`,async (req,res)=>{
21
+ const params=req.params;
22
+ if(this.generateTime){
23
+ params.gmt_create=new Date();
24
+ }
25
+ let r=await this.add(params)
26
+ res.send(M.successResult(r));
27
+ })
28
+ app.post(`${this.prefix}/delete`,async (req,res)=>{
29
+ let r=await this.delete(req.params);
30
+ res.send(M.successResult(r));
31
+ });
32
+
33
+ app.post(`${this.prefix}/update`,async (req,res)=>{
34
+ const params=req.params;
35
+ if(this.generateTime){
36
+ params.gmt_modified=new Date();
37
+ }
38
+ await this.update(params);
39
+ res.send(M.successResult());
40
+ });
41
+
42
+ app.get(`${this.prefix}/getById`,async (req,res)=>{
43
+ let r=await this.getById(req.params.id);
44
+ res.send(M.successResult(r));
45
+ });
46
+
47
+ app.get(`${this.prefix}/listAll`,async (req,res)=>{
48
+ const params=req.params;
49
+ let r=await this.listAll(params);
50
+ res.send(M.successResult(r));
51
+ });
52
+
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});
56
+ res.send(M.successResult(r));
57
+ })
58
+
59
+ /**
60
+ * 如果有parent_id才能返回树
61
+ */
62
+ app.get(`${this.prefix}/tree`,async (req,res)=>{
63
+ const {parent_id,...queryCase}=req.params;
64
+ let r=await CollectionUtils.selectTree(this.getChildenList.bind(this), parent_id,queryCase);
65
+ res.send(M.successResult(r));
66
+ })
67
+ }
68
+ }
69
+
70
+
71
+
72
+ module.exports = AbstractBaseRpcApi;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * 数据源为内存的rpc风格接口
3
+ */
4
+ const AbstractBaseRpcApi=require("./AbstractBaseRpcApi");
5
+
6
+
7
+ class ApiCloudBaseRpcApi extends AbstractBaseRpcApi{
8
+ constructor(props) {
9
+ super(props);
10
+ this.appid=props.appid;
11
+ this.appkey=props.appkey;
12
+ this.apiCloudClient = props.apiCloudClient;
13
+ this.tableClient= this.apiCloudClient.tableClient(props.tableName);
14
+ }
15
+ async add(obj){
16
+ let r= await this.tableClient.add(obj);
17
+ return r;
18
+ }
19
+
20
+ async delete(obj){
21
+ let r= await this.tableClient.delete(obj);
22
+ return r;
23
+ }
24
+
25
+ async getById(id){
26
+ let r=await this.tableClient.list({id},null,null,"sort");
27
+ return r[0];
28
+ }
29
+
30
+ async list({page=1,num=10,queryCase}){
31
+ page=Number.parseInt(page);
32
+ num=Number.parseInt(num);
33
+ let limit=num;
34
+ let skip= (page-1)*num
35
+ let r=await this.tableClient.list(queryCase,limit,skip,"createdAt ASC");
36
+ return r;
37
+ }
38
+
39
+ async listAll(obj){
40
+ let r=await this.tableClient.list(obj,null,null,"sort");
41
+ return r;
42
+ }
43
+
44
+ async update(obj){
45
+ let r= await this.tableClient.update(obj);
46
+ return r;
47
+ }
48
+
49
+ async getChildenList(id,caseObj){
50
+ let r= this.listAll({parent_id:id,...caseObj});
51
+ return r;
52
+ }
53
+ }
54
+
55
+
56
+ module.exports = ApiCloudBaseRpcApi;