@byted-apaas/server-sdk-node 1.0.12 → 1.0.15
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/README.md +31 -1
- package/context/db/impl/IObject.d.ts +37 -0
- package/context/db/impl/object.d.ts +21 -1
- package/context/db/impl/object.js +228 -42
- package/context/db/impl/oql/ioql.d.ts +8 -0
- package/context/db/impl/oql/oql.d.ts +3 -0
- package/context/db/impl/oql/oql.js +10 -1
- package/context/db/impl/queryBuilder.d.ts +2 -2
- package/context/db/impl/queryBuilder.js +5 -13
- package/context/db/impl/transaction/index.d.ts +3 -0
- package/context/db/impl/transaction/index.js +10 -1
- package/context/db/impl/transaction.d.ts +8 -0
- package/kunlun/operator/impl/logicV2.d.ts +1 -0
- package/kunlun/operator/impl/logicV2.js +1 -4
- package/package.json +2 -2
- package/request/innerapi.d.ts +12 -12
- package/request/innerapi.js +45 -45
- package/request/interface.d.ts +12 -12
- package/request/openapi.d.ts +12 -12
- package/request/openapi.js +85 -25
package/README.md
CHANGED
|
@@ -1 +1,31 @@
|
|
|
1
|
-
## aPaaS
|
|
1
|
+
## aPaaS @byted-apaas/server-sdk-node 版本说明:
|
|
2
|
+
|
|
3
|
+
-------
|
|
4
|
+
### 版本:1.0.15|兼容升级
|
|
5
|
+
新功能:数据操作接口支持指定鉴权身份
|
|
6
|
+
- 不指定: 访问数据模型时, 权限 1.0 应用默认使用系统身份鉴权, 权限 2.0 应用默认使用用户身份鉴权
|
|
7
|
+
- useUserAuth(): 访问数据模型时, 使用用户身份鉴权
|
|
8
|
+
- useSystemAuth(): 访问数据模型时, 使用系统身份鉴权
|
|
9
|
+
```js
|
|
10
|
+
// 示例
|
|
11
|
+
await context.db.object("obj").findOne();
|
|
12
|
+
await context.db.object("obj").useUserAuth().findOne();
|
|
13
|
+
await context.db.object("obj").useSystemAuth().findOne();
|
|
14
|
+
|
|
15
|
+
await context.db.newTransaction().object("obj").commit();
|
|
16
|
+
await context.db.newTransaction().useUserAuth().object("obj").commit();
|
|
17
|
+
await context.db.newTransaction().useSystemAuth().object("obj").commit();
|
|
18
|
+
|
|
19
|
+
await context.db.oql("SELECT _id FROM obj").execute();
|
|
20
|
+
await context.db.oql("SELECT _id FROM obj").useUserAuth().execute();
|
|
21
|
+
await context.db.oql("SELECT _id FROM obj").useSystemAuth().execute();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 版本:1.0.14|兼容升级
|
|
25
|
+
新功能:支持流式查询 findStream
|
|
26
|
+
```js
|
|
27
|
+
await context.db.object("_user").findStream(async (records) => {
|
|
28
|
+
// doSomething ...
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
优化:findAll 标记为过时方法,用 findStream 替代
|
|
@@ -86,6 +86,14 @@ export interface _IKSyncEndpoint<T> {
|
|
|
86
86
|
* @paramExample [{_id: 1001, _name: "John", gender: "male"}, {_id: 1002, _name: "Alis", gender: "female"}]
|
|
87
87
|
*/
|
|
88
88
|
batchUpdate(recordMapList: _Cond<T>[]): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* 用户级鉴权
|
|
91
|
+
*/
|
|
92
|
+
useUserAuth(): this;
|
|
93
|
+
/**
|
|
94
|
+
* 系统级鉴权
|
|
95
|
+
*/
|
|
96
|
+
useSystemAuth(): this;
|
|
89
97
|
}
|
|
90
98
|
export interface _IKAsyncEndpoint<T> {
|
|
91
99
|
/**
|
|
@@ -118,9 +126,19 @@ export interface _IKAsyncEndpoint<T> {
|
|
|
118
126
|
batchUpdateAsync(recordMapList: _Cond<T>[]): Promise<{
|
|
119
127
|
taskID: number;
|
|
120
128
|
}>;
|
|
129
|
+
/**
|
|
130
|
+
* 用户级鉴权
|
|
131
|
+
*/
|
|
132
|
+
useUserAuth(): this;
|
|
133
|
+
/**
|
|
134
|
+
* 系统级鉴权
|
|
135
|
+
*/
|
|
136
|
+
useSystemAuth(): this;
|
|
121
137
|
}
|
|
122
138
|
export interface _IKQuery<T> {
|
|
123
139
|
/**
|
|
140
|
+
* @Deprecated Use FindStream instead.
|
|
141
|
+
*
|
|
124
142
|
* 无需入参,返回全部符合条件的记录
|
|
125
143
|
* @example
|
|
126
144
|
* ```
|
|
@@ -130,6 +148,17 @@ export interface _IKQuery<T> {
|
|
|
130
148
|
* ```
|
|
131
149
|
*/
|
|
132
150
|
findAll(): Promise<_Record<T>[]>;
|
|
151
|
+
/**
|
|
152
|
+
* 遍历全部符合条件的记录
|
|
153
|
+
* @param handler 业务处理函数
|
|
154
|
+
* @example
|
|
155
|
+
* ```
|
|
156
|
+
* await context.db.object("_user").findStream(async (records) => {
|
|
157
|
+
* // doSomething ...
|
|
158
|
+
* });
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
findStream(handler: (records: object[]) => Promise<void>): Promise<void>;
|
|
133
162
|
/**
|
|
134
163
|
* 无需入参,返回符合条件的记录,单次返回 200 条
|
|
135
164
|
* @example
|
|
@@ -248,4 +277,12 @@ export interface _IKQuery<T> {
|
|
|
248
277
|
* ```
|
|
249
278
|
*/
|
|
250
279
|
count(): Promise<number>;
|
|
280
|
+
/**
|
|
281
|
+
* 用户级鉴权
|
|
282
|
+
*/
|
|
283
|
+
useUserAuth(): this;
|
|
284
|
+
/**
|
|
285
|
+
* 系统级鉴权
|
|
286
|
+
*/
|
|
287
|
+
useSystemAuth(): this;
|
|
251
288
|
}
|
|
@@ -23,6 +23,7 @@ export declare class _KApplicationObject<T> {
|
|
|
23
23
|
declare class _KObjectSync<T> implements _IKSyncEndpoint<T> {
|
|
24
24
|
apiName: string;
|
|
25
25
|
appCtx: AppCtx;
|
|
26
|
+
authType: string;
|
|
26
27
|
create(recordMap: _Cond<T>): Promise<{
|
|
27
28
|
_id: number;
|
|
28
29
|
}>;
|
|
@@ -34,9 +35,12 @@ declare class _KObjectSync<T> implements _IKSyncEndpoint<T> {
|
|
|
34
35
|
batchDelete(recordIdList: number[]): Promise<void>;
|
|
35
36
|
batchDelete(recordList: _Cond<T>[]): Promise<void>;
|
|
36
37
|
batchUpdate(recordMapList: _Cond<T>[]): Promise<void>;
|
|
38
|
+
useUserAuth(): this;
|
|
39
|
+
useSystemAuth(): this;
|
|
37
40
|
}
|
|
38
41
|
declare class _KObjectAsync<T> implements _IKAsyncEndpoint<T> {
|
|
39
42
|
apiName: string;
|
|
43
|
+
authType: string;
|
|
40
44
|
batchCreateAsync(recordMapList: _Cond<T>[]): Promise<{
|
|
41
45
|
taskID: number;
|
|
42
46
|
}>;
|
|
@@ -49,13 +53,17 @@ declare class _KObjectAsync<T> implements _IKAsyncEndpoint<T> {
|
|
|
49
53
|
batchUpdateAsync(recordMapList: _Cond<T>[]): Promise<{
|
|
50
54
|
taskID: number;
|
|
51
55
|
}>;
|
|
56
|
+
useUserAuth(): this;
|
|
57
|
+
useSystemAuth(): this;
|
|
52
58
|
}
|
|
53
59
|
declare class _KObjectQuery<T> implements _IKQuery<T> {
|
|
54
60
|
apiName: string;
|
|
55
61
|
appCtx: AppCtx;
|
|
62
|
+
authType: string;
|
|
56
63
|
find(): Promise<_Record<T>[]>;
|
|
57
64
|
findOne(): Promise<_Record<T>>;
|
|
58
65
|
findAll(): Promise<_Record<T>[]>;
|
|
66
|
+
findStream(handler: (records: object[]) => Promise<void>): Promise<void>;
|
|
59
67
|
orderBy<K extends keyof T>(fieldApiNames: K[]): _KQuery<T>;
|
|
60
68
|
orderBy<K extends keyof T>(...fieldApiNames: K[]): _KQuery<T>;
|
|
61
69
|
orderByDesc<K extends keyof T>(fieldApiNames: K[]): _KQuery<T>;
|
|
@@ -67,15 +75,24 @@ declare class _KObjectQuery<T> implements _IKQuery<T> {
|
|
|
67
75
|
limit(limit: number): _KQuery<T>;
|
|
68
76
|
offset(offset: number): _KQuery<T>;
|
|
69
77
|
count(): Promise<number>;
|
|
78
|
+
useSystemAuth(): this;
|
|
79
|
+
useUserAuth(): this;
|
|
70
80
|
}
|
|
71
81
|
/**
|
|
72
82
|
* _KQuery is kunlun query implement, implements ORM operations.
|
|
73
83
|
*/
|
|
74
84
|
declare class _KQuery<T> implements _IKQuery<T> {
|
|
75
|
-
|
|
85
|
+
private authType;
|
|
86
|
+
constructor(objectApiName: string, appCtx?: AppCtx, authType?: string);
|
|
76
87
|
find(): Promise<_Record<T>[]>;
|
|
77
88
|
findOne(): Promise<_Record<T>>;
|
|
89
|
+
/**
|
|
90
|
+
* @Deprecated Use FindStream instead.
|
|
91
|
+
*/
|
|
78
92
|
findAll(): Promise<_Record<T>[]>;
|
|
93
|
+
findStream(handler: (records: object[]) => Promise<void>): Promise<void>;
|
|
94
|
+
private walkRecordsByID;
|
|
95
|
+
private walkRecordsByLimitOffset;
|
|
79
96
|
private getRecordsByPage;
|
|
80
97
|
orderBy<K extends keyof T>(fieldApiNames: K[]): this;
|
|
81
98
|
orderBy<K extends keyof T>(...fieldApiNames: K[]): this;
|
|
@@ -88,5 +105,8 @@ declare class _KQuery<T> implements _IKQuery<T> {
|
|
|
88
105
|
limit(limit: number): this;
|
|
89
106
|
offset(offset: number): this;
|
|
90
107
|
count(): Promise<number>;
|
|
108
|
+
private findPreCheck;
|
|
109
|
+
useSystemAuth(): this;
|
|
110
|
+
useUserAuth(): this;
|
|
91
111
|
}
|
|
92
112
|
export {};
|
|
@@ -12,6 +12,7 @@ const propertiesStore_1 = require("./propertiesStore");
|
|
|
12
12
|
const common_1 = require("../../../application/impl/common");
|
|
13
13
|
const logicV2_1 = require("../../../kunlun/operator/impl/logicV2");
|
|
14
14
|
const operator_1 = require("../../../kunlun/operator/impl/operator");
|
|
15
|
+
const constants_1 = require("@byted-apaas/server-common-node/constants/constants");
|
|
15
16
|
const queryPropertiesStore = new propertiesStore_1.default();
|
|
16
17
|
// _KObject will be Mixin with [_KObjectSync, _KObjectAsync, _KObjectQuery]
|
|
17
18
|
class _KObject {
|
|
@@ -52,7 +53,7 @@ class _KObjectSync {
|
|
|
52
53
|
return await Request.GetInstance().openSDKCreateRecordBySync(this.apiName, recordMap);
|
|
53
54
|
});
|
|
54
55
|
}
|
|
55
|
-
return await Request.GetInstance().createRecordBySync(this.apiName, recordMap);
|
|
56
|
+
return await Request.GetInstance().createRecordBySync(this.apiName, recordMap, this.authType);
|
|
56
57
|
}
|
|
57
58
|
;
|
|
58
59
|
async delete(recordOrRecordID) {
|
|
@@ -79,7 +80,7 @@ class _KObjectSync {
|
|
|
79
80
|
return await Request.GetInstance().openSDKDeleteRecordBySync(this.apiName, recordID);
|
|
80
81
|
});
|
|
81
82
|
}
|
|
82
|
-
return await Request.GetInstance().deleteRecordBySync(this.apiName, recordID);
|
|
83
|
+
return await Request.GetInstance().deleteRecordBySync(this.apiName, recordID, this.authType);
|
|
83
84
|
}
|
|
84
85
|
throw new server_common_node_1.exceptions.InvalidParamError("record must be number or object");
|
|
85
86
|
}
|
|
@@ -115,7 +116,7 @@ class _KObjectSync {
|
|
|
115
116
|
return await Request.GetInstance().openSDKUpdateRecordBySync(this.apiName, recordID, record);
|
|
116
117
|
});
|
|
117
118
|
}
|
|
118
|
-
return await Request.GetInstance().updateRecordBySync(this.apiName, recordID, record);
|
|
119
|
+
return await Request.GetInstance().updateRecordBySync(this.apiName, recordID, record, this.authType);
|
|
119
120
|
}
|
|
120
121
|
;
|
|
121
122
|
// batch sync
|
|
@@ -135,7 +136,7 @@ class _KObjectSync {
|
|
|
135
136
|
let data = (this.appCtx && this.appCtx.mode == 'openSDK') ?
|
|
136
137
|
await (0, common_1.runCtxForOpenSDK)(this.appCtx, async () => {
|
|
137
138
|
return await Request.GetInstance().openSDKCreateRecordsBySync(this.apiName, recordMapList);
|
|
138
|
-
}) : await Request.GetInstance().createRecordsBySync(this.apiName, recordMapList);
|
|
139
|
+
}) : await Request.GetInstance().createRecordsBySync(this.apiName, recordMapList, this.authType);
|
|
139
140
|
// todo: 确定函数返回值
|
|
140
141
|
if (!(data instanceof Array) && data["record_ids"]) {
|
|
141
142
|
return data["record_ids"];
|
|
@@ -169,7 +170,7 @@ class _KObjectSync {
|
|
|
169
170
|
return await Request.GetInstance().openSDKDeleteRecordsBySync(this.apiName, recordIDs);
|
|
170
171
|
});
|
|
171
172
|
}
|
|
172
|
-
return await Request.GetInstance().deleteRecordsBySync(this.apiName, recordIDs);
|
|
173
|
+
return await Request.GetInstance().deleteRecordsBySync(this.apiName, recordIDs, this.authType);
|
|
173
174
|
}
|
|
174
175
|
async batchUpdate(recordMapList) {
|
|
175
176
|
// 参数校验、组装
|
|
@@ -193,9 +194,17 @@ class _KObjectSync {
|
|
|
193
194
|
return await Request.GetInstance().openSDKUpdateRecordsBySync(this.apiName, recordMapList);
|
|
194
195
|
});
|
|
195
196
|
}
|
|
196
|
-
return await Request.GetInstance().updateRecordsBySync(this.apiName, recordMap);
|
|
197
|
+
return await Request.GetInstance().updateRecordsBySync(this.apiName, recordMap, this.authType);
|
|
197
198
|
}
|
|
198
199
|
;
|
|
200
|
+
useUserAuth() {
|
|
201
|
+
this.authType = constants_1.AuthTypeUser;
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
useSystemAuth() {
|
|
205
|
+
this.authType = constants_1.AuthTypeSystem;
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
199
208
|
}
|
|
200
209
|
class _KObjectAsync {
|
|
201
210
|
constructor() {
|
|
@@ -221,7 +230,7 @@ class _KObjectAsync {
|
|
|
221
230
|
if (server_common_node_1.utils.stringify(recordMapList).length > 50 * 1024 * 1024) {
|
|
222
231
|
throw new server_common_node_1.exceptions.InvalidParamError("parameter records size exceeds 50MB");
|
|
223
232
|
}
|
|
224
|
-
return await Request.GetInstance().createRecordsByAsync(this.apiName, recordMapList);
|
|
233
|
+
return await Request.GetInstance().createRecordsByAsync(this.apiName, recordMapList, this.authType);
|
|
225
234
|
}
|
|
226
235
|
;
|
|
227
236
|
async batchDeleteAsync(recordOrIDList) {
|
|
@@ -250,7 +259,7 @@ class _KObjectAsync {
|
|
|
250
259
|
if (server_common_node_1.utils.stringify(recordIDs).length > 50 * 1024 * 1024) {
|
|
251
260
|
throw new server_common_node_1.exceptions.InvalidParamError("parameter records size exceeds 50MB");
|
|
252
261
|
}
|
|
253
|
-
return await Request.GetInstance().deleteRecordsByAsync(this.apiName, recordIDs);
|
|
262
|
+
return await Request.GetInstance().deleteRecordsByAsync(this.apiName, recordIDs, this.authType);
|
|
254
263
|
}
|
|
255
264
|
async batchUpdateAsync(recordMapList) {
|
|
256
265
|
// 参数校验、组装
|
|
@@ -280,9 +289,17 @@ class _KObjectAsync {
|
|
|
280
289
|
if (server_common_node_1.utils.stringify(recordMap).length > 50 * 1024 * 1024) {
|
|
281
290
|
throw new server_common_node_1.exceptions.InvalidParamError("parameter records size exceeds 50MB");
|
|
282
291
|
}
|
|
283
|
-
return await Request.GetInstance().updateRecordsByAsync(this.apiName, recordMap);
|
|
292
|
+
return await Request.GetInstance().updateRecordsByAsync(this.apiName, recordMap, this.authType);
|
|
284
293
|
}
|
|
285
294
|
;
|
|
295
|
+
useUserAuth() {
|
|
296
|
+
this.authType = constants_1.AuthTypeUser;
|
|
297
|
+
return this;
|
|
298
|
+
}
|
|
299
|
+
useSystemAuth() {
|
|
300
|
+
this.authType = constants_1.AuthTypeSystem;
|
|
301
|
+
return this;
|
|
302
|
+
}
|
|
286
303
|
}
|
|
287
304
|
class _KObjectQuery {
|
|
288
305
|
constructor() {
|
|
@@ -290,55 +307,68 @@ class _KObjectQuery {
|
|
|
290
307
|
}
|
|
291
308
|
// find, findOne
|
|
292
309
|
async find() {
|
|
293
|
-
return new _KQuery(this.apiName, this.appCtx).find();
|
|
310
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).find();
|
|
294
311
|
}
|
|
295
312
|
;
|
|
296
313
|
async findOne() {
|
|
297
|
-
return new _KQuery(this.apiName, this.appCtx).findOne();
|
|
314
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).findOne();
|
|
298
315
|
}
|
|
299
316
|
;
|
|
300
317
|
async findAll() {
|
|
301
|
-
return new _KQuery(this.apiName, this.appCtx).findAll();
|
|
318
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).findAll();
|
|
302
319
|
}
|
|
303
320
|
;
|
|
321
|
+
async findStream(handler) {
|
|
322
|
+
return new _KQuery(this.apiName, this.appCtx).findStream(handler);
|
|
323
|
+
}
|
|
304
324
|
orderBy(fieldApiNames, ...restFieldApiNames) {
|
|
305
325
|
fieldApiNames = !fieldApiNames ? [] : fieldApiNames;
|
|
306
|
-
return new _KQuery(this.apiName, this.appCtx).orderBy(server_common_node_1.utils.argsToList(fieldApiNames, restFieldApiNames));
|
|
326
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).orderBy(server_common_node_1.utils.argsToList(fieldApiNames, restFieldApiNames));
|
|
307
327
|
}
|
|
308
328
|
orderByDesc(fieldApiNames, ...restFieldApiNames) {
|
|
309
329
|
fieldApiNames = !fieldApiNames ? [] : fieldApiNames;
|
|
310
|
-
return new _KQuery(this.apiName, this.appCtx).orderByDesc(server_common_node_1.utils.argsToList(fieldApiNames, restFieldApiNames));
|
|
330
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).orderByDesc(server_common_node_1.utils.argsToList(fieldApiNames, restFieldApiNames));
|
|
311
331
|
}
|
|
312
332
|
select(fieldApiNames, ...restFieldApiNames) {
|
|
313
333
|
fieldApiNames = !fieldApiNames ? [] : fieldApiNames;
|
|
314
|
-
return new _KQuery(this.apiName, this.appCtx).select(server_common_node_1.utils.argsToList(fieldApiNames, restFieldApiNames));
|
|
334
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).select(server_common_node_1.utils.argsToList(fieldApiNames, restFieldApiNames));
|
|
315
335
|
}
|
|
316
336
|
where(conditionMap) {
|
|
317
337
|
if (!conditionMap) {
|
|
318
|
-
return new _KQuery(this.apiName, this.appCtx).where();
|
|
338
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).where();
|
|
319
339
|
}
|
|
320
|
-
return new _KQuery(this.apiName, this.appCtx).where(conditionMap);
|
|
340
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).where(conditionMap);
|
|
321
341
|
}
|
|
322
342
|
// limit, offset, count
|
|
323
343
|
limit(limit) {
|
|
324
|
-
return new _KQuery(this.apiName, this.appCtx).limit(limit);
|
|
344
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).limit(limit);
|
|
325
345
|
}
|
|
326
346
|
;
|
|
327
347
|
offset(offset) {
|
|
328
|
-
return new _KQuery(this.apiName, this.appCtx).offset(offset);
|
|
348
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).offset(offset);
|
|
329
349
|
}
|
|
330
350
|
;
|
|
331
351
|
async count() {
|
|
332
|
-
return new _KQuery(this.apiName, this.appCtx).count();
|
|
352
|
+
return new _KQuery(this.apiName, this.appCtx, this.authType).count();
|
|
333
353
|
}
|
|
334
354
|
;
|
|
355
|
+
useSystemAuth() {
|
|
356
|
+
this.authType = constants_1.AuthTypeSystem;
|
|
357
|
+
return this;
|
|
358
|
+
}
|
|
359
|
+
useUserAuth() {
|
|
360
|
+
this.authType = constants_1.AuthTypeUser;
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
335
363
|
}
|
|
336
364
|
/**
|
|
337
365
|
* _KQuery is kunlun query implement, implements ORM operations.
|
|
338
366
|
*/
|
|
339
367
|
class _KQuery {
|
|
340
|
-
|
|
341
|
-
|
|
368
|
+
constructor(objectApiName, appCtx = null, authType = null) {
|
|
369
|
+
if (authType) {
|
|
370
|
+
this.authType = authType;
|
|
371
|
+
}
|
|
342
372
|
if (!objectApiName) {
|
|
343
373
|
throw new server_common_node_1.exceptions.InvalidParamError("objectApiName is empty");
|
|
344
374
|
}
|
|
@@ -350,23 +380,8 @@ class _KQuery {
|
|
|
350
380
|
});
|
|
351
381
|
}
|
|
352
382
|
async find() {
|
|
383
|
+
this.findPreCheck();
|
|
353
384
|
const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
|
|
354
|
-
// 查询非 _id 字段或者关联对象下钻._id, limit 不允许超过 200
|
|
355
|
-
let checkLimit;
|
|
356
|
-
let checkFields;
|
|
357
|
-
if (queryV2) {
|
|
358
|
-
checkLimit = queryV2._limit;
|
|
359
|
-
checkFields = queryV2.fields;
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
362
|
-
checkLimit = queryBuilder.getLimit();
|
|
363
|
-
checkFields = queryBuilder.getSelect();
|
|
364
|
-
}
|
|
365
|
-
if (checkFields.length != 1 || !(checkFields[0] == "_id" || checkFields[0].endsWith("._id"))) {
|
|
366
|
-
if (checkLimit > 200) {
|
|
367
|
-
throw new Error(`limit(${checkLimit}) > ${queryBuilder_1.maxLimit}. Limit should be 1~${queryBuilder_1.maxLimitV2} when querying the id field, otherwise it should be 1~${queryBuilder_1.maxLimit}`);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
385
|
if (appCtx) {
|
|
371
386
|
const param = {
|
|
372
387
|
limit: queryV2._limit,
|
|
@@ -382,7 +397,7 @@ class _KQuery {
|
|
|
382
397
|
}
|
|
383
398
|
else {
|
|
384
399
|
const criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
|
|
385
|
-
return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), false);
|
|
400
|
+
return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), false, this.authType);
|
|
386
401
|
}
|
|
387
402
|
}
|
|
388
403
|
;
|
|
@@ -393,6 +408,9 @@ class _KQuery {
|
|
|
393
408
|
return records instanceof Array ? (records.length > 0 ? records[0] : null) : null;
|
|
394
409
|
}
|
|
395
410
|
;
|
|
411
|
+
/**
|
|
412
|
+
* @Deprecated Use FindStream instead.
|
|
413
|
+
*/
|
|
396
414
|
async findAll() {
|
|
397
415
|
const { queryBuilder, queryV2 } = queryPropertiesStore.get(this);
|
|
398
416
|
if (queryBuilder.getOrder().length > 0) {
|
|
@@ -404,6 +422,152 @@ class _KQuery {
|
|
|
404
422
|
return await this.getRecordsByPage();
|
|
405
423
|
}
|
|
406
424
|
;
|
|
425
|
+
async findStream(handler) {
|
|
426
|
+
const { queryBuilder, queryV2 } = queryPropertiesStore.get(this);
|
|
427
|
+
let offset = 0, orders = [];
|
|
428
|
+
if (queryV2) {
|
|
429
|
+
offset = queryV2._offset;
|
|
430
|
+
orders = queryV2._order;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
offset = queryBuilder.getOffset();
|
|
434
|
+
orders = queryBuilder.getOrder();
|
|
435
|
+
}
|
|
436
|
+
if (orders.length === 0 && offset === 0) {
|
|
437
|
+
// 走主键翻页
|
|
438
|
+
return await this.walkRecordsByID(handler);
|
|
439
|
+
}
|
|
440
|
+
// 走 limit offset 翻页
|
|
441
|
+
return await this.walkRecordsByLimitOffset(handler);
|
|
442
|
+
}
|
|
443
|
+
async walkRecordsByID(handler) {
|
|
444
|
+
const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
|
|
445
|
+
let limit = 0, selectFields = [];
|
|
446
|
+
if (queryV2) {
|
|
447
|
+
if (queryV2.isSetLimit) {
|
|
448
|
+
limit = queryV2._limit;
|
|
449
|
+
}
|
|
450
|
+
selectFields = queryV2.fields;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
if (queryBuilder.isSetLimit) {
|
|
454
|
+
limit = queryBuilder.getLimit();
|
|
455
|
+
}
|
|
456
|
+
selectFields = queryBuilder.getSelect();
|
|
457
|
+
}
|
|
458
|
+
let maxId = 0, queryCount = 0;
|
|
459
|
+
while (true) {
|
|
460
|
+
let newLimit = queryBuilder_1.defaultLimit;
|
|
461
|
+
if (limit > 0 && limit - queryCount < queryBuilder_1.defaultLimit) {
|
|
462
|
+
newLimit = limit - queryCount;
|
|
463
|
+
}
|
|
464
|
+
if (newLimit <= 0) {
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
let rs;
|
|
468
|
+
if (appCtx) {
|
|
469
|
+
let criterion = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
|
|
470
|
+
criterion.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
|
|
471
|
+
const param = {
|
|
472
|
+
limit: newLimit,
|
|
473
|
+
offset: 0,
|
|
474
|
+
sort: [{ field: '_id', direction: 'asc', type: '' }],
|
|
475
|
+
fields: selectFields,
|
|
476
|
+
count: false,
|
|
477
|
+
filter: criterion,
|
|
478
|
+
};
|
|
479
|
+
rs = await (0, common_1.runCtxForOpenSDK)(appCtx, async () => {
|
|
480
|
+
return await Request.GetInstance().openSDKGetRecords(apiName, param);
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
else {
|
|
484
|
+
let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
|
|
485
|
+
rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), [new order_1.Order("_id", "asc")], false, queryBuilder.getSelect(), 0, newLimit, false, this.authType);
|
|
486
|
+
}
|
|
487
|
+
queryCount += rs.length;
|
|
488
|
+
rs.forEach((r) => {
|
|
489
|
+
maxId = r._id > maxId ? r._id : maxId;
|
|
490
|
+
});
|
|
491
|
+
try {
|
|
492
|
+
if (rs.length > 0) {
|
|
493
|
+
await handler(rs);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
catch (err) {
|
|
497
|
+
throw err;
|
|
498
|
+
}
|
|
499
|
+
if (rs.length < queryBuilder_1.defaultLimit) {
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
async walkRecordsByLimitOffset(handler) {
|
|
505
|
+
const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
|
|
506
|
+
let limit = 0, offset = 0, selectFields = [], orders = [];
|
|
507
|
+
if (queryV2) {
|
|
508
|
+
if (queryV2.isSetLimit) {
|
|
509
|
+
limit = queryV2._limit;
|
|
510
|
+
}
|
|
511
|
+
offset = queryV2._offset;
|
|
512
|
+
selectFields = queryV2.fields;
|
|
513
|
+
orders = queryV2._order;
|
|
514
|
+
}
|
|
515
|
+
else {
|
|
516
|
+
if (queryBuilder.isSetLimit) {
|
|
517
|
+
limit = queryBuilder.getLimit();
|
|
518
|
+
}
|
|
519
|
+
offset = queryBuilder.getOffset();
|
|
520
|
+
selectFields = queryBuilder.getSelect();
|
|
521
|
+
orders = queryBuilder.getOrder();
|
|
522
|
+
}
|
|
523
|
+
let maxId = 0, queryCount = 0;
|
|
524
|
+
for (let i = offset; !limit || i < offset + limit; i += queryBuilder_1.defaultLimit) {
|
|
525
|
+
let newLimit = function () {
|
|
526
|
+
if (limit == 0) {
|
|
527
|
+
return queryBuilder_1.defaultLimit;
|
|
528
|
+
}
|
|
529
|
+
if (offset + limit - i < queryBuilder_1.defaultLimit) {
|
|
530
|
+
return offset + limit - i;
|
|
531
|
+
}
|
|
532
|
+
return queryBuilder_1.defaultLimit;
|
|
533
|
+
}();
|
|
534
|
+
if (newLimit <= 0) {
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
let rs;
|
|
538
|
+
if (appCtx) {
|
|
539
|
+
let criterion = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
|
|
540
|
+
criterion.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
|
|
541
|
+
const param = {
|
|
542
|
+
limit: newLimit,
|
|
543
|
+
offset: i,
|
|
544
|
+
sort: orders,
|
|
545
|
+
fields: selectFields,
|
|
546
|
+
count: false,
|
|
547
|
+
filter: criterion,
|
|
548
|
+
};
|
|
549
|
+
rs = await (0, common_1.runCtxForOpenSDK)(appCtx, async () => {
|
|
550
|
+
return await Request.GetInstance().openSDKGetRecords(apiName, param);
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
|
|
555
|
+
rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), orders, false, selectFields, i, newLimit, false, this.authType);
|
|
556
|
+
}
|
|
557
|
+
queryCount += rs.length;
|
|
558
|
+
try {
|
|
559
|
+
if (rs.length > 0) {
|
|
560
|
+
await handler(rs);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
catch (err) {
|
|
564
|
+
throw err;
|
|
565
|
+
}
|
|
566
|
+
if (rs.length < queryBuilder_1.defaultLimit) {
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
407
571
|
async getRecordsByPage() {
|
|
408
572
|
const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
|
|
409
573
|
let maxId = 0, records = [];
|
|
@@ -426,13 +590,13 @@ class _KQuery {
|
|
|
426
590
|
}
|
|
427
591
|
else {
|
|
428
592
|
let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
|
|
429
|
-
rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), [new order_1.Order("_id", "asc")], false, queryBuilder.getSelect(), 0, queryBuilder_1.
|
|
593
|
+
rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), [new order_1.Order("_id", "asc")], false, queryBuilder.getSelect(), 0, queryBuilder_1.defaultLimit, false, this.authType);
|
|
430
594
|
}
|
|
431
595
|
rs.forEach((r) => {
|
|
432
596
|
maxId = r._id > maxId ? r._id : maxId;
|
|
433
597
|
});
|
|
434
598
|
records.push(...rs);
|
|
435
|
-
if (rs.length < queryBuilder_1.
|
|
599
|
+
if (rs.length < queryBuilder_1.defaultLimit) {
|
|
436
600
|
break;
|
|
437
601
|
}
|
|
438
602
|
}
|
|
@@ -538,10 +702,32 @@ class _KQuery {
|
|
|
538
702
|
}
|
|
539
703
|
else {
|
|
540
704
|
let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
|
|
541
|
-
return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), true);
|
|
705
|
+
return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), true, this.authType);
|
|
542
706
|
}
|
|
543
707
|
}
|
|
544
708
|
;
|
|
709
|
+
findPreCheck() {
|
|
710
|
+
const { queryBuilder, queryV2 } = queryPropertiesStore.get(this);
|
|
711
|
+
let limit = queryBuilder_1.defaultLimit;
|
|
712
|
+
if (queryV2) {
|
|
713
|
+
limit = queryV2._limit;
|
|
714
|
+
}
|
|
715
|
+
else {
|
|
716
|
+
limit = queryBuilder.limit;
|
|
717
|
+
}
|
|
718
|
+
if (limit < 1) {
|
|
719
|
+
// need import exceptions
|
|
720
|
+
throw new server_common_node_1.exceptions.InvalidParamError(`limit(${limit}) < 1`);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
useSystemAuth() {
|
|
724
|
+
this.authType = constants_1.AuthTypeSystem;
|
|
725
|
+
return this;
|
|
726
|
+
}
|
|
727
|
+
useUserAuth() {
|
|
728
|
+
this.authType = constants_1.AuthTypeUser;
|
|
729
|
+
return this;
|
|
730
|
+
}
|
|
545
731
|
}
|
|
546
732
|
function applyMixins(derivedCtor, constructors) {
|
|
547
733
|
constructors.forEach((baseCtor) => {
|
|
@@ -3,7 +3,10 @@ export declare class Oql implements IOql {
|
|
|
3
3
|
oql: string;
|
|
4
4
|
params: any[];
|
|
5
5
|
namedParams: Record<string, any>;
|
|
6
|
+
authType: string;
|
|
6
7
|
constructor(oql: string);
|
|
7
8
|
constructor(oql: string, namedArgs: Record<string, any>);
|
|
8
9
|
execute(): Promise<any[]>;
|
|
10
|
+
useSystemAuth(): IOql;
|
|
11
|
+
useUserAuth(): IOql;
|
|
9
12
|
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.Oql = void 0;
|
|
6
6
|
const Request = require("../../../../request/interface");
|
|
7
|
+
const constants_1 = require("@byted-apaas/server-common-node/constants/constants");
|
|
7
8
|
class Oql {
|
|
8
9
|
constructor(oql, namedArgs) {
|
|
9
10
|
this.oql = oql;
|
|
@@ -15,11 +16,19 @@ class Oql {
|
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
async execute() {
|
|
18
|
-
let records = await Request.GetInstance().oql(this.oql, this.params, this.namedParams);
|
|
19
|
+
let records = await Request.GetInstance().oql(this.oql, this.params, this.namedParams, this.authType);
|
|
19
20
|
if (records && Array.isArray(records)) {
|
|
20
21
|
return records;
|
|
21
22
|
}
|
|
22
23
|
return [];
|
|
23
24
|
}
|
|
25
|
+
useSystemAuth() {
|
|
26
|
+
this.authType = constants_1.AuthTypeSystem;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
useUserAuth() {
|
|
30
|
+
this.authType = constants_1.AuthTypeUser;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
24
33
|
}
|
|
25
34
|
exports.Oql = Oql;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Order, OrderType } from './order';
|
|
2
2
|
import { Logic } from '../../../kunlun/operator/impl/logic';
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const maxLimitV2 = 10000;
|
|
3
|
+
export declare const defaultLimit = 200;
|
|
5
4
|
/**
|
|
6
5
|
* 查询构造器,在 DB 中被初始化,用于构建一次查询
|
|
7
6
|
*/
|
|
@@ -11,6 +10,7 @@ export declare class QueryBuilder {
|
|
|
11
10
|
select: string[];
|
|
12
11
|
where: Logic[];
|
|
13
12
|
limit: number;
|
|
13
|
+
isSetLimit: boolean;
|
|
14
14
|
offset: number;
|
|
15
15
|
order: Order[];
|
|
16
16
|
logic: Logic;
|