ming_node 2.2.5 → 2.2.6

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
  ```
11
11
  # cdn
12
12
 
13
- https://unpkg.com/ming_node/MemoryBaseRpcApi.js
13
+ https://unpkg.com/ming_node/index.js
14
14
 
15
15
 
16
16
  # ming_node最小环境
@@ -190,6 +190,16 @@ async function main(){
190
190
 
191
191
  ```
192
192
 
193
+ # Rpc风格,服务插件
194
+ [ming_node api插件.yuque](https://www.yuque.com/docs/share/f4444345-ea5b-4f3d-b0c7-ab267e901e81)
195
+ ```js
196
+ var M=require("ming_node");
197
+ const Api= require("ming_node/plugins/BaseRpcApi/MemoryBaseRpcApi");
198
+ let api = new Api({tableName:"ming",generateTime:true})
199
+ var app=M.server();
200
+ app.listen(8888);
201
+ app.use(api);
202
+ ```
193
203
 
194
204
  # 基于ming_node 的 ming_api_mock
195
205
 
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.2.5
6
+ * version :2.2.6
7
7
  */
8
8
  var http = require('http');
9
9
  var https = require('https');
@@ -163,6 +163,23 @@ class BaseMapper {
163
163
  return dataList;
164
164
  }
165
165
 
166
+
167
+ /**
168
+ * 分页查
169
+ * @param page
170
+ * @param num
171
+ * @param queryCase
172
+ * @param columns
173
+ * @param order
174
+ */
175
+ async selectPurePageList({page=1,num=10,queryCase="1=1", columns="*",order="id desc"}){
176
+ let start = (page - 1) * num;
177
+ let whereCase=queryCase;
178
+ let queryColumn=BaseMapper.getColumn(columns);
179
+ let dataList=await Db.doSql(`SELECT ${queryColumn} FROM ${this.tableName} where ${whereCase} order by ${order} LIMIT ${start},${num}`)
180
+ return dataList;
181
+ }
182
+
166
183
  /**
167
184
  * 查后代
168
185
  * @param parent_id
@@ -222,7 +239,7 @@ class BaseMapper {
222
239
  async getTableSchema(){
223
240
  if(this.tableSchema==null){
224
241
  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}';`
242
+ let sql=`select column_name,column_type,column_comment from information_schema.columns where table_schema ='${dataBaseName}' and table_name = '${this.tableName}';`
226
243
  let tableSchema=await Db.doSql(sql);
227
244
  return tableSchema;
228
245
  }
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.2.5"
49
+ "version": "2.2.6"
50
50
  }
@@ -6,56 +6,128 @@ const{
6
6
  GraphQLInt,
7
7
  GraphQLSchema,
8
8
  GraphQLList,
9
- GraphQLNonNull
9
+ GraphQLNonNull,
10
+ GraphQLFloat,
11
+ GraphQLBoolean,
10
12
  } = require('graphql');
11
13
 
12
14
 
15
+ function getGraphTypeBySqlType(sqlType) {
16
+ sqlType = sqlType.toLowerCase();
17
+ if (sqlType.indexOf("string") >= 0 || sqlType.indexOf("varchar") >= 0 || sqlType.indexOf("text") >= 0) {
18
+ return GraphQLString;
19
+ } else if (sqlType.indexOf("int") >= 0) {
20
+ return GraphQLInt;
21
+ } else if (sqlType.indexOf("date") >= 0 || sqlType.indexOf("time") >= 0) {
22
+ return GraphQLString;
23
+ } else if (sqlType.indexOf("double") >= 0) {
24
+ return GraphQLFloat;
25
+ } else if (sqlType.indexOf("float") >= 0) {
26
+ return GraphQLFloat;
27
+ } else if (sqlType.indexOf("decimal") >= 0) {
28
+ return GraphQLFloat;
29
+ }
30
+ return GraphQLString;
31
+ }
13
32
 
14
33
 
15
- function getGraphqlSchema({dbBaseMapper}){
16
- const ResponseType = new GraphQLObjectType({
17
- name:'response',
18
- fields:{
19
- status:{type:GraphQLInt},
20
- msg:{type:GraphQLString}
34
+ function obj2QueryCase(obj){
35
+ let queryCase="1=1";
36
+ if(obj && Object.keys(obj).length>0){
37
+ for(var p in obj) {
38
+ queryCase=queryCase+` and ${p}='${obj[p]}' `
21
39
  }
22
- })
40
+ }
41
+ return queryCase;
42
+
43
+ }
23
44
 
24
- const qlObjectType = new GraphQLObjectType({
25
- name:'person',
45
+ async function getGraphqlSchema({dbBaseMapper,generateTime}){
46
+ let tableName=dbBaseMapper.tableName;
47
+ let tableSchema=await dbBaseMapper.getTableSchema()
48
+ const ResponseType = new GraphQLObjectType({
49
+ name:'response',
26
50
  fields:{
27
- id:{type:GraphQLInt},
28
- name:{type:GraphQLString}
51
+ code:{type:GraphQLInt},
52
+ msg:{type:GraphQLString},
53
+ data:{type:GraphQLString},
29
54
  }
30
55
  })
31
-
56
+ let qlObjectTypeParams={};
57
+ qlObjectTypeParams.name=tableName;
58
+ qlObjectTypeParams.fields={};
59
+ tableSchema.forEach(u=>{
60
+ qlObjectTypeParams.fields[u.column_name]={
61
+ type:getGraphTypeBySqlType(u.column_type),
62
+ description:u.column_comment};
63
+ })
64
+ const qlObjectType = new GraphQLObjectType(qlObjectTypeParams)
32
65
  const RootQuery = new GraphQLObjectType({
33
- name:'personQuery',
66
+ name:tableName+'Query',
34
67
  fields:{
35
- person:{
68
+ rows:{
36
69
  type: new GraphQLList(qlObjectType),
37
- args:{
38
- id:{type:GraphQLInt},
39
- name:{type:GraphQLString}
40
- },
70
+ args:{...qlObjectTypeParams.fields,
71
+ page:{type:GraphQLInt, description:"页码"},
72
+ num:{type:GraphQLInt, description:"每页条数"},
73
+ order:{type:GraphQLInt, description:"排序"}
74
+ },
41
75
  async resolve(parentValue,args){
42
- return Db.doSql(`select * from person`);
76
+ const {page=1,num=10,order="id",...queryCase}=args;
77
+ const queryCaseStr= obj2QueryCase(queryCase);
78
+ return dbBaseMapper.selectPurePageList({
79
+ page,
80
+ num,
81
+ order,
82
+ queryCase:queryCaseStr})
43
83
  }
84
+ },
85
+ total:{
86
+ type: GraphQLInt,
87
+ args:qlObjectTypeParams.fields,
88
+ async resolve(parentValue,args){
89
+ const {page=1,num=10,order="id",...queryCase}=args;
90
+ const queryCaseStr= obj2QueryCase(queryCase);
91
+ return dbBaseMapper.selectCount({queryCase:queryCaseStr})
92
+ }
44
93
  }
45
94
  }
46
95
  })
47
96
 
48
97
  var mutation = new GraphQLObjectType({
49
- name:"Mutation",
98
+ name:tableName+"Mutation",
50
99
  fields:{
51
- addPerson:{
100
+ add:{
101
+ type:ResponseType,
102
+ args:qlObjectTypeParams.fields,
103
+ async resolve(parenValue,args){
104
+ if(generateTime){
105
+ args.gmt_create=new Date().format("yyyy-MM-dd hh:mm:ss");
106
+ }
107
+ await dbBaseMapper.insert(args);
108
+ return {code:0,msg:"success"}
109
+ }
110
+ },
111
+ update:{
112
+ type:ResponseType,
113
+ args:qlObjectTypeParams.fields,
114
+ async resolve(parenValue,args){
115
+ const {id,...updateObj}=args;
116
+ if(generateTime){
117
+ updateObj.gmt_modified=new Date().format("yyyy-MM-dd hh:mm:ss");
118
+ }
119
+ await dbBaseMapper.update(updateObj,`id=${id}`)
120
+ return {code:0,msg:"success"}
121
+ }
122
+ }
123
+ ,
124
+ delete:{
52
125
  type:ResponseType,
53
- args:{
54
- name:{type:GraphQLString}
55
- },
126
+ args:qlObjectTypeParams.fields,
56
127
  async resolve(parenValue,args){
57
- //await personDao.insert(args.name)
58
- return {status:0,msg:"ok"}
128
+ const queryCaseStr= obj2QueryCase(args);
129
+ await dbBaseMapper.delete(queryCaseStr);
130
+ return {code:0,msg:"ok"}
59
131
  }
60
132
  }
61
133
  }
@@ -2,13 +2,18 @@ var getGraphqlSchema= require("./getGraphqlSchema")
2
2
  const { graphqlHTTP } = require('express-graphql')
3
3
 
4
4
  class BaseGraphqlApi{
5
- constructor({dbBaseMapper,prefix}) {
5
+ constructor({dbBaseMapper,prefix,generateTime}) {
6
6
  this.dbBaseMapper=dbBaseMapper;
7
7
  this.tableName=dbBaseMapper.tableName;
8
8
  this.prefix=prefix?prefix:tableName;
9
+ this.generateTime=generateTime;
9
10
  }
10
- install(app,args){
11
- let schema= getGraphqlSchema( this.dbBaseMapper)
11
+ async install(app,args){
12
+ let schema=await getGraphqlSchema({
13
+ dbBaseMapper:this.dbBaseMapper,
14
+ generateTime:this.generateTime}
15
+ );
16
+
12
17
  app.mapping(`/${this.prefix}`,graphqlHTTP({
13
18
  schema:schema,
14
19
  graphiql:true
@@ -32,8 +32,9 @@ class ApiCloudBaseRpcApi extends AbstractBaseRpcApi{
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");
36
- return r;
35
+ let rows=await this.tableClient.list(queryCase,limit,skip,"createdAt ASC");
36
+ let countResult=await this.tableClient.count(queryCase);
37
+ return {rows, total:countResult.count}
37
38
  }
38
39
 
39
40
  async listAll(obj){