@steedos/service-rest 2.7.0-beta.9 → 2.7.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.
package/consts.js CHANGED
@@ -1,11 +1,11 @@
1
1
  /*
2
2
  * @Author: sunhaolin@hotoa.com
3
3
  * @Date: 2023-06-12 17:20:40
4
- * @LastEditors: sunhaolin@hotoa.com
5
- * @LastEditTime: 2023-06-15 18:38:17
4
+ * @LastEditors: baozhoutao@steedos.com
5
+ * @LastEditTime: 2024-03-29 13:17:38
6
6
  * @Description:
7
7
  */
8
8
  module.exports = {
9
- QUERY_DOCS_TOP: 5000,
9
+ QUERY_DOCS_TOP: 500,
10
10
  REQUEST_SUCCESS_STATUS: 0,
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steedos/service-rest",
3
- "version": "2.7.0-beta.9",
3
+ "version": "2.7.0",
4
4
  "main": "package.service.js",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -16,9 +16,9 @@
16
16
  "repository": {},
17
17
  "license": "MIT",
18
18
  "dependencies": {
19
- "@steedos/objectql": "2.7.0-beta.9",
20
- "@steedos/service-object-mixin": "2.7.0-beta.9",
19
+ "@steedos/objectql": "2.7.0",
20
+ "@steedos/service-object-mixin": "2.7.0",
21
21
  "lodash": "^4.17.21"
22
22
  },
23
- "gitHead": "b9fad2d43fc0216dbcfcf965b7218590167d8f55"
23
+ "gitHead": "2f1586ea4f8af5b93753878e96f1f48adbef31a7"
24
24
  }
@@ -1,8 +1,8 @@
1
1
  /*
2
2
  * @Author: sunhaolin@hotoa.com
3
3
  * @Date: 2023-03-23 15:12:14
4
- * @LastEditors: 殷亮辉 yinlianghui@hotoa.com
5
- * @LastEditTime: 2024-03-14 10:39:22
4
+ * @LastEditors: 孙浩林 sunhaolin@steedos.com
5
+ * @LastEditTime: 2024-04-18 10:48:25
6
6
  * @Description:
7
7
  */
8
8
  "use strict";
@@ -47,6 +47,114 @@ module.exports = {
47
47
  return 'ok'
48
48
  }
49
49
  },
50
+ /**
51
+ * @api {POST} /api/v1/batch 批处理接口
52
+ * @apiVersion 0.0.0
53
+ * @apiGroup @steedos/service-rest
54
+ * @apiBody {Object[]} find 查询条件, 例如:[{objectName: 'contracts', filters: [['name', '=', 'test']], fields: ['name', 'description'], top: 10, skip: 0, sort: 'name desc'}]
55
+ * @apiBody {Object[]} count 查询条件, 例如:[{objectName: 'contracts', filters: [['name', '=', 'test']]}]
56
+ * @apiBody {Object[]} findOne 查询条件, 例如:[{objectName: 'contracts', id: "xxx", fields: ['name', 'description']}]
57
+ * @apiBody {Object[]} insert 插入数据, 例如: [{objectName: 'contracts', doc: {name: 'test', description: 'test'}}]
58
+ * @apiBody {Object[]} update 更新数据, 例如: [{objectName: 'contracts', id: "xxx", doc: {name: 'test', description: 'test'}}]
59
+ * @apiBody {Object[]} delete 删除数据, 例如: [{objectName: 'contracts', id: "xxx"}]
60
+ * @apiName batch
61
+ */
62
+ // batch: {
63
+ // rest: {
64
+ // method: "POST",
65
+ // path: "/batch"
66
+ // },
67
+ // async handler(ctx){
68
+
69
+ // const { find, count, findOne, insert, update, delete: deleteAs } = ctx.params;
70
+
71
+ // const res = {}
72
+
73
+ // const findRes = [];
74
+ // if(find){
75
+ // for (const item of find) {
76
+ // const args = {...item};
77
+ // if(_.has(item, 'fields') && !_.isString(item.fields) ){
78
+ // args.fields = JSON.stringify(item.fields)
79
+ // }
80
+ // if(_.has(item, 'uiFields') && !_.isString(item.uiFields) ){
81
+ // args.uiFields = JSON.stringify(item.uiFields)
82
+ // }
83
+ // if(_.has(item, 'expandFields') && !_.isString(item.expandFields) ){
84
+ // args.expandFields = JSON.stringify(item.expandFields)
85
+ // }
86
+ // if(_.has(item, 'filters') && !_.isString(item.filters) ){
87
+ // args.filters = JSON.stringify(item.filters)
88
+ // }
89
+ // findRes.push(ctx.broker.call(`rest.find`, args).catch(err => {
90
+ // return err;
91
+ // }))
92
+ // }
93
+
94
+ // res.find = await Promise.all(findRes);
95
+ // }
96
+
97
+ // const countRes = [];
98
+ // if(count){
99
+ // for (const item of count) {
100
+ // const args = {...item};
101
+ // if(_.has(item, 'filters') && !_.isString(item.filters) ){
102
+ // args.filters = JSON.stringify(item.filters)
103
+ // }
104
+ // countRes.push(ctx.broker.call(`rest.count`, args).catch(err => {
105
+ // return err;
106
+ // }))
107
+ // }
108
+ // res.count = await Promise.all(countRes);
109
+ // }
110
+
111
+ // const findOneRes = []
112
+ // if(findOne){
113
+ // for (const item of findOne) {
114
+ // const args = {...item};
115
+ // if(_.has(item, 'fields') && !_.isString(item.fields) ){
116
+ // args.fields = JSON.stringify(item.fields)
117
+ // }
118
+ // findOneRes.push(ctx.broker.call(`rest.findOne`, args).catch(err => {
119
+ // return err;
120
+ // }))
121
+ // }
122
+ // res.findOne = await Promise.all(findOneRes);
123
+ // }
124
+
125
+ // const insertRes = []
126
+ // if(insert){
127
+ // for (const item of insert) {
128
+ // insertRes.push(ctx.broker.call(`rest.insert`, item).catch(err => {
129
+ // return err;
130
+ // }))
131
+ // }
132
+ // res.insert = await Promise.all(insertRes);
133
+ // }
134
+
135
+ // const updateRes = []
136
+ // if(update){
137
+ // for (const item of update) {
138
+ // updateRes.push(ctx.broker.call(`rest.update`, item).catch(err => {
139
+ // return err;
140
+ // }))
141
+ // }
142
+ // res.update = await Promise.all(updateRes);
143
+ // }
144
+
145
+ // const deleteRes = []
146
+ // if(deleteAs){
147
+ // for (const item of deleteAs) {
148
+ // deleteRes.push(ctx.broker.call(`rest.delete`, item).catch(err => {
149
+ // return err;
150
+ // }))
151
+ // }
152
+ // res.delete = await Promise.all(deleteRes);
153
+ // }
154
+
155
+ // return res;
156
+ // }
157
+ // },
50
158
  /**
51
159
  * @api {GET} /api/v1/:objectName 获取列表记录
52
160
  * @apiVersion 0.0.0
@@ -57,7 +165,7 @@ module.exports = {
57
165
  * @apiQuery {String} [uiFields] 字段名,如:'["owner", "date"]',此参数中的字段要求在参数fields中存在
58
166
  * @apiQuery {String} [expandFields] 字段名,如:'{owner: {fields: ["name"], uiFields: ["owner"], expandFields: ...}}'
59
167
  * @apiQuery {String} [filters] 过滤条件,如:'[["name", "=", "test"],["amount", ">", 100]]'
60
- * @apiQuery {String} [top] 获取条数,如:'10',最多5000
168
+ * @apiQuery {String} [top] 获取条数,如:'10',最多500
61
169
  * @apiQuery {String} [skip] 跳过条数,如:'10'
62
170
  * @apiQuery {String} [sort] 排序,如:'name desc'
63
171
  * @apiSuccess {Object[]} find 记录列表
@@ -154,7 +262,7 @@ module.exports = {
154
262
  return []
155
263
  }
156
264
  if (query.top > QUERY_DOCS_TOP) {
157
- query.top = QUERY_DOCS_TOP // 最多返回5000条数据
265
+ query.top = QUERY_DOCS_TOP // 最多返回500条数据
158
266
  }
159
267
  }
160
268
 
@@ -288,7 +396,7 @@ module.exports = {
288
396
  * @apiParam {String} objectName 对象API Name,如:contracts
289
397
  * @apiBody {String[]} [fields] 字段名,如:["name", "description"]
290
398
  * @apiBody {Object[]} [filters] 过滤条件,如:[['name', '=', 'test'],['amount', '>', 100]]
291
- * @apiBody {Number} [top] 获取条数,如:10,最多5000
399
+ * @apiBody {Number} [top] 获取条数,如:10,最多500
292
400
  * @apiBody {Number} [skip] 跳过条数,如:10
293
401
  * @apiBody {String} [sort] 排序,如:'name desc'
294
402
  * @apiSuccess {Object[]} search 记录列表
@@ -356,7 +464,7 @@ module.exports = {
356
464
  return []
357
465
  }
358
466
  if (query.top > QUERY_DOCS_TOP) {
359
- query.top = QUERY_DOCS_TOP // 最多返回5000条数据
467
+ query.top = QUERY_DOCS_TOP // 最多返回500条数据
360
468
  }
361
469
  }
362
470
 
@@ -582,17 +690,21 @@ module.exports = {
582
690
  async handler(ctx) {
583
691
  const userSession = ctx.meta.user;
584
692
  const { objectName, id } = ctx.params;
585
- const objectConfig = await getObject(objectName).getConfig()
586
- const enableTrash = objectConfig.enable_trash
587
- if (!enableTrash) {
588
- await this.delete(objectName, id, userSession)
589
- } else {
590
- const data = {
591
- is_deleted: true,
592
- deleted: new Date(),
593
- deleted_by: userSession ? userSession.userId : null
693
+ const obj = getObject(objectName)
694
+ const doc = await obj.findOne(id, { fields: { _id: 1 } }) // 检查记录是否存在
695
+ if (doc) {
696
+ const objectConfig = await obj.getConfig()
697
+ const enableTrash = objectConfig.enable_trash
698
+ if (!enableTrash) {
699
+ await this.delete(objectName, id, userSession)
700
+ } else {
701
+ const data = {
702
+ is_deleted: true,
703
+ deleted: new Date(),
704
+ deleted_by: userSession ? userSession.userId : null
705
+ }
706
+ await this.update(objectName, id, data, userSession)
594
707
  }
595
- await this.update(objectName, id, data, userSession)
596
708
  }
597
709
  return {
598
710
  "status": REQUEST_SUCCESS_STATUS,
@@ -602,8 +714,7 @@ module.exports = {
602
714
  }
603
715
  }
604
716
  }
605
- },
606
-
717
+ }
607
718
  },
608
719
 
609
720
  /**