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.
- package/README.md +1 -1
- package/beforeTest/ApiCloudBaseRpcApiTest.js +17 -0
- package/beforeTest/FileBaseRpcApiTest.js +8 -0
- package/beforeTest/MemorDbTest.js +16 -0
- package/beforeTest/MemoryBaseRestApiTest.js +8 -0
- package/beforeTest/MemoryBaseRpcApiTest.js +8 -0
- package/beforeTest/MiApiCloudClientTest.js +16 -0
- package/beforeTest/MongoDbBaseRestApiTest.js +19 -0
- package/beforeTest/MongoDbBaseRpcApiTest.js +19 -0
- package/beforeTest/MySqlBaseRestApiTest.js +13 -0
- package/beforeTest/MysqlBaseRpcApiTest.js +18 -0
- package/beforeTest/graphql_test.js +12 -0
- package/beforeTest/sseserver.js +44 -0
- package/beforeTest/static/ssetest.html +21 -0
- package/index.js +187 -49
- package/ming_node.md +4 -4
- package/module/BaseMapper.js +73 -35
- package/module/MemoryDb.js +136 -0
- package/module/MiApiCloudClient.js +649 -0
- package/package.json +1 -1
- package/plugins/BaseGraphqlApi/getGraphqlSchema.js +73 -0
- package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
- package/plugins/BaseGraphqlApi/index.js +18 -0
- package/plugins/BaseRestApi/AbstractBaseRestApi.js +59 -0
- package/plugins/BaseRestApi/ApiCloudBaseRestApi.js +56 -0
- package/plugins/BaseRestApi/FileBaseRestApi.js +62 -0
- package/plugins/BaseRestApi/MemoryBaseRestApi.js +50 -0
- package/plugins/BaseRestApi/MongoDbBaseRestApi.js +75 -0
- package/plugins/BaseRestApi/MysqlBaseRestApi.js +72 -0
- package/plugins/BaseRpcApi/AbstractBaseRpcApi.js +72 -0
- package/plugins/BaseRpcApi/ApiCloudBaseRpcApi.js +56 -0
- package/plugins/BaseRpcApi/FileBaseRpcApi.js +62 -0
- package/plugins/BaseRpcApi/MemoryBaseRpcApi.js +50 -0
- package/plugins/BaseRpcApi/MongoDbBaseRpcApi.js +75 -0
- package/plugins/BaseRpcApi/MysqlBaseRpcApi.js +72 -0
- package/utils/common/CollectionUtils.js +28 -0
- package/beforeTest/t1.js +0 -18
package/module/BaseMapper.js
CHANGED
@@ -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
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
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
|
/**
|
@@ -156,7 +169,7 @@ class BaseMapper {
|
|
156
169
|
* @param queryCase
|
157
170
|
* @returns {Promise<*>}
|
158
171
|
*/
|
159
|
-
|
172
|
+
async selectDescendantIdList({parent_id=-1, queryCase="1=1"}){
|
160
173
|
let sql=`
|
161
174
|
select id from (
|
162
175
|
select t1.id,
|
@@ -167,9 +180,9 @@ class BaseMapper {
|
|
167
180
|
(select @pids := ${parent_id}) t2
|
168
181
|
) t3 where ischild != 0
|
169
182
|
`;
|
170
|
-
|
171
|
-
|
172
|
-
|
183
|
+
let dataList=await Db.doSql(sql);
|
184
|
+
return dataList;
|
185
|
+
}
|
173
186
|
|
174
187
|
/**
|
175
188
|
* 查树
|
@@ -178,34 +191,42 @@ class BaseMapper {
|
|
178
191
|
* @returns {Promise<*>}
|
179
192
|
*/
|
180
193
|
async selectTree({columns="*",parent_id=-1}){
|
181
|
-
|
182
|
-
|
183
|
-
|
194
|
+
const that=this;
|
195
|
+
async function getList(pid){
|
196
|
+
let sql=`
|
184
197
|
SELECT ${columns} from ${that.tableName} WHERE parent_id=${pid};
|
185
198
|
`;
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
199
|
+
let dataList=await Db.doSql(sql);
|
200
|
+
return dataList;
|
201
|
+
};
|
202
|
+
async function addChilden(record){
|
203
|
+
let cList= await getList(record.id);
|
204
|
+
record.childen=cList;
|
205
|
+
if(cList.length==0){
|
206
|
+
return
|
207
|
+
}
|
208
|
+
for (let i=0;i<cList.length;i++){
|
209
|
+
let cObj= cList[i];
|
210
|
+
addChilden(cObj);
|
211
|
+
}
|
212
|
+
}
|
200
213
|
|
201
214
|
let rootList=await getList(parent_id);
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
215
|
+
for (let i=0;i<rootList.length;i++){
|
216
|
+
let obj=rootList[i];
|
217
|
+
await addChilden(obj);
|
218
|
+
}
|
206
219
|
return rootList;
|
207
220
|
}
|
208
221
|
|
222
|
+
async getTableSchema(){
|
223
|
+
if(this.tableSchema==null){
|
224
|
+
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}';`
|
226
|
+
let tableSchema=await Db.doSql(sql);
|
227
|
+
return tableSchema;
|
228
|
+
}
|
229
|
+
}
|
209
230
|
|
210
231
|
|
211
232
|
static getColumn(columns){
|
@@ -215,7 +236,7 @@ class BaseMapper {
|
|
215
236
|
}
|
216
237
|
return queryColumn;
|
217
238
|
}
|
218
|
-
|
239
|
+
|
219
240
|
static getFirstRecord(records){
|
220
241
|
if(records.length==0){
|
221
242
|
return null
|
@@ -239,6 +260,21 @@ class BaseMapper {
|
|
239
260
|
return sql;
|
240
261
|
}
|
241
262
|
|
263
|
+
static getReplaceObjSql(tableName, obj) {
|
264
|
+
var fields = "(";
|
265
|
+
var values = "(";
|
266
|
+
for (let field in obj) {
|
267
|
+
fields += field + ",";
|
268
|
+
values += `'${obj[field]}'` + ",";
|
269
|
+
}
|
270
|
+
fields = fields.substr(0, fields.lastIndexOf(","));
|
271
|
+
values = values.substr(0, values.lastIndexOf(","));
|
272
|
+
fields += ")";
|
273
|
+
values += ");";
|
274
|
+
let sql = "replace into " + tableName + fields + " values " + values;
|
275
|
+
return sql;
|
276
|
+
}
|
277
|
+
|
242
278
|
static getDeleteObjSql(tableName,caseStr) {
|
243
279
|
caseStr=!caseStr? `id=${obj.id}`:caseStr;
|
244
280
|
let sql = `delete from ${tableName} where ${caseStr}`;
|
@@ -256,7 +292,7 @@ class BaseMapper {
|
|
256
292
|
sql = `update ${tableName} set ${fields.map(u => u + "='" + obj[u] + "'")} where ${caseStr}`;
|
257
293
|
return sql;
|
258
294
|
}
|
259
|
-
|
295
|
+
|
260
296
|
static getSelectObjSql(tableName, obj) {
|
261
297
|
var fields = [];
|
262
298
|
for (let field in obj) {
|
@@ -266,7 +302,9 @@ class BaseMapper {
|
|
266
302
|
sql = sql.replace(/,/g, " and ")
|
267
303
|
return sql;
|
268
304
|
}
|
269
|
-
|
305
|
+
|
306
|
+
|
307
|
+
|
270
308
|
}
|
271
309
|
|
272
310
|
|
@@ -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;
|