ming_node 2.2.0 → 2.2.6

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 +11 -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 +90 -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 +145 -0
  22. package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
  23. package/plugins/BaseGraphqlApi/index.js +23 -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 +57 -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
@@ -1,9 +1,14 @@
1
1
  const M=require("../index")
2
- const Db=M.getMySql({})
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,9 +17,17 @@ class BaseMapper {
12
17
  * @returns {Promise<*>}
13
18
  */
14
19
  async insert(obj){
15
- let sql= BaseMapper.getInsertObjSql(this.tableName,obj)
16
- let r=await Db.doSql(sql);
17
- return r;
20
+ delete obj.id;
21
+ let sql= BaseMapper.getInsertObjSql(this.tableName,obj)
22
+ let r=await Db.doSql(sql);
23
+ return r;
24
+ }
25
+
26
+
27
+ async replace(obj){
28
+ let sql= BaseMapper.getReplaceObjSql(this.tableName,obj)
29
+ let r=await Db.doSql(sql);
30
+ return r;
18
31
  }
19
32
 
20
33
  /**
@@ -45,7 +58,7 @@ class BaseMapper {
45
58
  }
46
59
 
47
60
  /**
48
- *根据条件改
61
+ * 删除
49
62
  * @param caseStr
50
63
  * @returns {Promise<*>}
51
64
  */
@@ -125,14 +138,14 @@ class BaseMapper {
125
138
  * @param order
126
139
  * @returns {Promise<{total: (*|{}), dataList: *}>}
127
140
  */
128
- async selectPage({page=1,num=10,queryCase="1=1", columns="*",order="id desc"}){
141
+ async selectPage({page=1,num=10,queryCase="1=1", columns="*",order="id desc"}){
129
142
  let start = (page - 1) * num;
130
143
  let whereCase=queryCase;
131
144
  let queryColumn=BaseMapper.getColumn(columns);
132
145
  let dataList=await Db.doSql(`SELECT ${queryColumn} FROM ${this.tableName} where ${whereCase} order by ${order} LIMIT ${start},${num}`)
133
146
  let totalR=await Db.doSql(`SELECT count(1) c FROM ${this.tableName} where ${whereCase}`)
134
147
  let total=totalR[0].c
135
- return {dataList,total};
148
+ return {rows:dataList,total};
136
149
  }
137
150
 
138
151
  /**
@@ -150,13 +163,30 @@ class BaseMapper {
150
163
  return dataList;
151
164
  }
152
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
+
153
183
  /**
154
184
  * 查后代
155
185
  * @param parent_id
156
186
  * @param queryCase
157
187
  * @returns {Promise<*>}
158
188
  */
159
- async selectDescendantIdList({parent_id=-1, queryCase="1=1"}){
189
+ async selectDescendantIdList({parent_id=-1, queryCase="1=1"}){
160
190
  let sql=`
161
191
  select id from (
162
192
  select t1.id,
@@ -167,9 +197,9 @@ class BaseMapper {
167
197
  (select @pids := ${parent_id}) t2
168
198
  ) t3 where ischild != 0
169
199
  `;
170
- let dataList=await Db.doSql(sql);
171
- return dataList;
172
- }
200
+ let dataList=await Db.doSql(sql);
201
+ return dataList;
202
+ }
173
203
 
174
204
  /**
175
205
  * 查树
@@ -178,34 +208,42 @@ class BaseMapper {
178
208
  * @returns {Promise<*>}
179
209
  */
180
210
  async selectTree({columns="*",parent_id=-1}){
181
- const that=this;
182
- async function getList(pid){
183
- let sql=`
211
+ const that=this;
212
+ async function getList(pid){
213
+ let sql=`
184
214
  SELECT ${columns} from ${that.tableName} WHERE parent_id=${pid};
185
215
  `;
186
- let dataList=await Db.doSql(sql);
187
- return dataList;
188
- };
189
- async function addChilden(record){
190
- let cList= await getList(record.id);
191
- record.childen=cList;
192
- if(cList.length==0){
193
- return
194
- }
195
- for (let i=0;i<cList.length;i++){
196
- let cObj= cList[i];
197
- addChilden(cObj);
198
- }
199
- }
216
+ let dataList=await Db.doSql(sql);
217
+ return dataList;
218
+ };
219
+ async function addChilden(record){
220
+ let cList= await getList(record.id);
221
+ record.childen=cList;
222
+ if(cList.length==0){
223
+ return
224
+ }
225
+ for (let i=0;i<cList.length;i++){
226
+ let cObj= cList[i];
227
+ addChilden(cObj);
228
+ }
229
+ }
200
230
 
201
231
  let rootList=await getList(parent_id);
202
- for (let i=0;i<rootList.length;i++){
203
- let obj=rootList[i];
204
- await addChilden(obj);
205
- }
232
+ for (let i=0;i<rootList.length;i++){
233
+ let obj=rootList[i];
234
+ await addChilden(obj);
235
+ }
206
236
  return rootList;
207
237
  }
208
238
 
239
+ async getTableSchema(){
240
+ if(this.tableSchema==null){
241
+ let dataBaseName=Db.dbConfig.database;
242
+ let sql=`select column_name,column_type,column_comment from information_schema.columns where table_schema ='${dataBaseName}' and table_name = '${this.tableName}';`
243
+ let tableSchema=await Db.doSql(sql);
244
+ return tableSchema;
245
+ }
246
+ }
209
247
 
210
248
 
211
249
  static getColumn(columns){
@@ -215,7 +253,7 @@ class BaseMapper {
215
253
  }
216
254
  return queryColumn;
217
255
  }
218
-
256
+
219
257
  static getFirstRecord(records){
220
258
  if(records.length==0){
221
259
  return null
@@ -239,6 +277,21 @@ class BaseMapper {
239
277
  return sql;
240
278
  }
241
279
 
280
+ static getReplaceObjSql(tableName, obj) {
281
+ var fields = "(";
282
+ var values = "(";
283
+ for (let field in obj) {
284
+ fields += field + ",";
285
+ values += `'${obj[field]}'` + ",";
286
+ }
287
+ fields = fields.substr(0, fields.lastIndexOf(","));
288
+ values = values.substr(0, values.lastIndexOf(","));
289
+ fields += ")";
290
+ values += ");";
291
+ let sql = "replace into " + tableName + fields + " values " + values;
292
+ return sql;
293
+ }
294
+
242
295
  static getDeleteObjSql(tableName,caseStr) {
243
296
  caseStr=!caseStr? `id=${obj.id}`:caseStr;
244
297
  let sql = `delete from ${tableName} where ${caseStr}`;
@@ -256,7 +309,7 @@ class BaseMapper {
256
309
  sql = `update ${tableName} set ${fields.map(u => u + "='" + obj[u] + "'")} where ${caseStr}`;
257
310
  return sql;
258
311
  }
259
-
312
+
260
313
  static getSelectObjSql(tableName, obj) {
261
314
  var fields = [];
262
315
  for (let field in obj) {
@@ -266,7 +319,9 @@ class BaseMapper {
266
319
  sql = sql.replace(/,/g, " and ")
267
320
  return sql;
268
321
  }
269
-
322
+
323
+
324
+
270
325
  }
271
326
 
272
327
 
@@ -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;