ming_node 2.2.5 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
  }
@@ -303,7 +320,44 @@ class BaseMapper {
303
320
  return sql;
304
321
  }
305
322
 
306
-
323
+ /**
324
+ * 给 list 加name
325
+ * @param tableName 表名
326
+ * @param list 列表
327
+ * @param list_idkey 列表中的idkey
328
+ * @param list_namekey 列表中的namekey
329
+ * @param db_idkey 库中的namekey
330
+ * @param db_namekey 库中的namekey
331
+ * @returns {Promise<void>}
332
+ */
333
+ static async appendListName(
334
+ {
335
+ tableName="t_user",
336
+ list,
337
+ list_idkey="create_user",
338
+ list_namekey="name",
339
+ db_idkey="create_user",
340
+ db_namekey="create_user_name",
341
+ }
342
+ ){
343
+ if(list==null || list.length==0){
344
+ return
345
+ }
346
+ const idKeyList=list.map(u=>u[list_idkey]);
347
+ let sql=`select ${db_idkey}, ${db_namekey} from ${tableName} where ${db_idkey} in (?)`
348
+ let dbDataList=await Db.doSql(sql,[idKeyList]);
349
+ const userMap = {};
350
+ for (let i = 0; i < dbDataList.length; i++) {
351
+ const idkeyV = dbDataList[i][db_idkey];
352
+ userMap[idkeyV] = dbDataList[i];
353
+ }
354
+ for (let i=0;i<list.length;i++){
355
+ if(userMap[list[i][list_idkey]]){
356
+ list[i][list_namekey]= userMap[list[i][list_idkey]][db_namekey];
357
+ }
358
+ }
359
+ return;
360
+ }
307
361
 
308
362
  }
309
363
 
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.4.0"
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){
@@ -23,6 +23,117 @@ class CollectionUtils{
23
23
  }
24
24
 
25
25
 
26
+ static list2Csv({list,titlesKeys,titlesNames}){
27
+ if(list==null||list.length==0){
28
+ return;
29
+ }
30
+ if(!titlesKeys){
31
+ titlesKeys=Object.keys(list[0])
32
+ }
33
+ if(!titlesNames){
34
+ titlesNames=titlesKeys
35
+ }
36
+ let bodyStr="";
37
+ list.forEach((items, index) => {
38
+ for(let key of titlesKeys){
39
+ items[key]=items[key]+"";
40
+ bodyStr=bodyStr+`"${!items[key]?'null': ""+items[key].replace(/\s/g,"")}"`+","
41
+ }
42
+ // console.log(bodyStr)
43
+ bodyStr=bodyStr+"\n";
44
+ })
45
+ return `\ufeff`+titlesNames+'\n'+bodyStr;
46
+ }
47
+
48
+ static n(arr,key="unionId"){
49
+ function n1(r1,r2,key){
50
+ if(r1==null || r2==null ||r1==undefined||r2==undefined || r1.length==0 || r2.length==0){
51
+ return []
52
+ }
53
+ let r2keyList=r2.map(u=>u[key])
54
+ let result= r1.filter(u=>r2keyList.indexOf(u[key])!==-1)
55
+ return result;
56
+ }
57
+ let allResult=[]
58
+ try{
59
+ if(arr==null || arr==undefined || arr.length==0){
60
+ return []
61
+ }
62
+ for (let i=0;i<arr.length;i++){
63
+ if(arr[i]==null||arr[i]==undefined||arr[i].length==0){
64
+ return []
65
+ }
66
+ }
67
+ allResult= arr.reduce((r1,r2)=>{
68
+ let ri= n1(r1,r2,key)
69
+ if(ri==null||ri==undefined||ri.length==0){
70
+ throw []
71
+ }
72
+ return ri;
73
+ },arr[0])
74
+ }catch (e){
75
+ allResult=e;
76
+ }
77
+ return allResult;
78
+ }
79
+
80
+ static u(arr,key="unionId"){
81
+ function u1(r1,r2,key){
82
+ if(r1==null && r2==null){
83
+ return []
84
+ }
85
+ let r=[...r1,...r2];
86
+ return r;
87
+ }
88
+ let allResult=[]
89
+ try{
90
+ if(arr==null || arr==undefined || arr.length==0){
91
+ return []
92
+ }
93
+ for (let i=0;i<arr.length;i++){
94
+ if(arr[i]==null||arr[i]==undefined||arr[i].length==0){
95
+ return []
96
+ }
97
+ }
98
+ allResult= arr.reduce((r1,r2)=>{
99
+ let ri= u1(r1,r2,key)
100
+ if(ri==null||ri==undefined||ri.length==0){
101
+ return [];
102
+ }
103
+ return ri;
104
+ },arr[0])
105
+ }catch (e){
106
+ allResult=[];
107
+ }
108
+ let allResultKeys=allResult.map(u=>u[key]);
109
+ let allResult1=[];
110
+ let allResult1KeyObj={};
111
+ for (let i=0;i<allResult.length;i++){
112
+ let allResultObj=allResult[i];
113
+ let k= allResultObj[key];
114
+ if(allResult1KeyObj[k]){
115
+ continue;
116
+ }else {
117
+ allResult1.push(allResultObj);
118
+ allResult1KeyObj[k]=true;
119
+ }
120
+ }
121
+ return allResult1;
122
+ }
123
+
124
+ static dif(arr1,arr2,key="unionId"){
125
+ if(arr1==null || arr1==null|| arr1.length==0 ){
126
+ return []
127
+ }
128
+ if(arr2==null || arr2==null|| arr2.length==0 ){
129
+ return arr1;
130
+ }
131
+ let r2keyList=arr2.map(u=>u[key])
132
+ let result= arr1.filter(u=>r2keyList.indexOf(u[key])==-1)
133
+ return result;
134
+
135
+ }
136
+
26
137
  }
27
138
 
28
139
  module.exports = CollectionUtils;
@@ -0,0 +1,36 @@
1
+
2
+
3
+ class DateUtils {
4
+ static difDate(startDate="2021-04-01",endDate="2022-02-10"){
5
+ let flag = [1, 3, 5, 7, 8, 10, 12, 4, 6, 9, 11, 2];
6
+ let start = new Date(startDate);
7
+ let end = new Date(endDate);
8
+ let year = end.getFullYear() - start.getFullYear();
9
+ let month = end.getMonth() - start.getMonth();
10
+ let day = end.getDate() - start.getDate();
11
+ if (month < 0) {
12
+ year--;
13
+ month = end.getMonth() + (12 - start.getMonth());
14
+ }
15
+ if (day < 0) {
16
+ month--;
17
+ let index = flag.findIndex((temp) => {
18
+ return temp === start.getMonth() + 1
19
+ });
20
+ let monthLength;
21
+ if (index <= 6) {
22
+ monthLength = 31;
23
+ } else if (index > 6 && index <= 10) {
24
+ monthLength = 30;
25
+ } else {
26
+ monthLength = 28;
27
+ }
28
+ day = end.getDate() + (monthLength - start.getDate());
29
+ }
30
+ return {year,month,day}
31
+ }
32
+
33
+ }
34
+
35
+
36
+ module.exports = DateUtils;