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.
- package/README.md +11 -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 +90 -35
- package/module/MemoryDb.js +136 -0
- package/module/MiApiCloudClient.js +649 -0
- package/package.json +1 -1
- package/plugins/BaseGraphqlApi/getGraphqlSchema.js +145 -0
- package/plugins/BaseGraphqlApi/getGraphqlSchemaDemo.js +76 -0
- package/plugins/BaseGraphqlApi/index.js +23 -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 +57 -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
@@ -0,0 +1,145 @@
|
|
1
|
+
M=require("../../index");
|
2
|
+
const Db=M.getMySql({});
|
3
|
+
const{
|
4
|
+
GraphQLObjectType,
|
5
|
+
GraphQLString,
|
6
|
+
GraphQLInt,
|
7
|
+
GraphQLSchema,
|
8
|
+
GraphQLList,
|
9
|
+
GraphQLNonNull,
|
10
|
+
GraphQLFloat,
|
11
|
+
GraphQLBoolean,
|
12
|
+
} = require('graphql');
|
13
|
+
|
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
|
+
}
|
32
|
+
|
33
|
+
|
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]}' `
|
39
|
+
}
|
40
|
+
}
|
41
|
+
return queryCase;
|
42
|
+
|
43
|
+
}
|
44
|
+
|
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',
|
50
|
+
fields:{
|
51
|
+
code:{type:GraphQLInt},
|
52
|
+
msg:{type:GraphQLString},
|
53
|
+
data:{type:GraphQLString},
|
54
|
+
}
|
55
|
+
})
|
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)
|
65
|
+
const RootQuery = new GraphQLObjectType({
|
66
|
+
name:tableName+'Query',
|
67
|
+
fields:{
|
68
|
+
rows:{
|
69
|
+
type: new GraphQLList(qlObjectType),
|
70
|
+
args:{...qlObjectTypeParams.fields,
|
71
|
+
page:{type:GraphQLInt, description:"页码"},
|
72
|
+
num:{type:GraphQLInt, description:"每页条数"},
|
73
|
+
order:{type:GraphQLInt, description:"排序"}
|
74
|
+
},
|
75
|
+
async resolve(parentValue,args){
|
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})
|
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
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
})
|
96
|
+
|
97
|
+
var mutation = new GraphQLObjectType({
|
98
|
+
name:tableName+"Mutation",
|
99
|
+
fields:{
|
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:{
|
125
|
+
type:ResponseType,
|
126
|
+
args:qlObjectTypeParams.fields,
|
127
|
+
async resolve(parenValue,args){
|
128
|
+
const queryCaseStr= obj2QueryCase(args);
|
129
|
+
await dbBaseMapper.delete(queryCaseStr);
|
130
|
+
return {code:0,msg:"ok"}
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
})
|
136
|
+
const schema = new GraphQLSchema({
|
137
|
+
query: RootQuery,
|
138
|
+
mutation: mutation
|
139
|
+
})
|
140
|
+
return schema;
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
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,23 @@
|
|
1
|
+
var getGraphqlSchema= require("./getGraphqlSchema")
|
2
|
+
const { graphqlHTTP } = require('express-graphql')
|
3
|
+
|
4
|
+
class BaseGraphqlApi{
|
5
|
+
constructor({dbBaseMapper,prefix,generateTime}) {
|
6
|
+
this.dbBaseMapper=dbBaseMapper;
|
7
|
+
this.tableName=dbBaseMapper.tableName;
|
8
|
+
this.prefix=prefix?prefix:tableName;
|
9
|
+
this.generateTime=generateTime;
|
10
|
+
}
|
11
|
+
async install(app,args){
|
12
|
+
let schema=await getGraphqlSchema({
|
13
|
+
dbBaseMapper:this.dbBaseMapper,
|
14
|
+
generateTime:this.generateTime}
|
15
|
+
);
|
16
|
+
|
17
|
+
app.mapping(`/${this.prefix}`,graphqlHTTP({
|
18
|
+
schema:schema,
|
19
|
+
graphiql:true
|
20
|
+
}))
|
21
|
+
}
|
22
|
+
}
|
23
|
+
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;
|