@byted-apaas/server-sdk-node 1.1.16 → 1.1.17-beta.11

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.
@@ -155,14 +155,15 @@ export interface _IKQuery<T> {
155
155
  * 如果未设置排序字段,默认以 _id 增序查询;
156
156
  * 如果有设置排序字段,必须设置具有唯一属性的字段,否则会有数据重复的风险;
157
157
  * @param handler 业务处理函数
158
+ * @param pageLimit 分页查询的数量,可选参数,默认值为 200,
158
159
  * @example
159
160
  * ```
160
161
  * await application.data.object('_user').findStream(async (records) => {
161
162
  * // doSomething ...
162
- * });
163
+ * }, 300);
163
164
  * ```
164
165
  */
165
- findStream(handler: (records: object[]) => Promise<void>): Promise<void>;
166
+ findStream: (handler: (records: object[]) => Promise<void>, pageLimit?: number) => Promise<void>;
166
167
  /**
167
168
  * 无需入参,返回符合条件的记录,单次返回 200 条
168
169
  * @example
@@ -1,7 +1,7 @@
1
1
  import { _Cond, _Record, _WhereCond } from '../../../types/types';
2
2
  import { _IKAsyncEndpoint, _IKQuery, _IKSyncEndpoint } from './IObject';
3
3
  import { AppCtx } from '../../../application/application';
4
- import { BatchResult } from "../../../common/structs";
4
+ import { BatchResult } from '../../../common/structs';
5
5
  /**
6
6
  * _KObject is kunlun object, every method new a _KQuery object to deal.
7
7
  */
@@ -92,7 +92,7 @@ declare class _KQuery<T> implements _IKQuery<T> {
92
92
  * @Deprecated Use FindStream instead.
93
93
  */
94
94
  findAll(): Promise<_Record<T>[]>;
95
- findStream(handler: (records: object[]) => Promise<void>): Promise<void>;
95
+ findStream(handler: (records: object[]) => Promise<void>, pageLimit?: number): Promise<void>;
96
96
  private walkRecordsByID;
97
97
  private walkRecordsByLimitOffset;
98
98
  private getRecordsByPage;
@@ -139,9 +139,8 @@ class _KObjectSync {
139
139
  }
140
140
  recordMapList = permissionUtils.batchDelUnauthField(recordMapList).newRecords;
141
141
  let data = (this.appCtx && this.appCtx.mode == 'openSDK') ?
142
- await (0, common_1.runCtxForOpenSDK)(this.appCtx, async () => {
143
- return await Request.GetInstance().openSDKCreateRecordsBySync(this.apiName, recordMapList);
144
- }) : await Request.GetInstance().createRecordsBySync(this.apiName, recordMapList, this.authType);
142
+ await (0, common_1.runCtxForOpenSDK)(this.appCtx, async () => await Request.GetInstance().openSDKCreateRecordsBySync(this.apiName, recordMapList))
143
+ : await Request.GetInstance().createRecordsBySync(this.apiName, recordMapList, this.authType);
145
144
  // todo: 确定函数返回值
146
145
  if (!(data instanceof Array) && data.record_ids) {
147
146
  return data.record_ids;
@@ -411,8 +410,11 @@ class _KQuery {
411
410
  });
412
411
  }
413
412
  else {
414
- const criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
415
- return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.fuzzySearch, queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), false, this.authType);
413
+ let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
414
+ if (queryBuilder.getLogic().logics.length > 0) {
415
+ criterion = await (0, logic_1.handleCriterion)(criterion);
416
+ }
417
+ return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), false, this.authType);
416
418
  }
417
419
  }
418
420
  ;
@@ -437,7 +439,7 @@ class _KQuery {
437
439
  return await this.getRecordsByPage();
438
440
  }
439
441
  ;
440
- async findStream(handler) {
442
+ async findStream(handler, pageLimit = 200) {
441
443
  const { queryBuilder, queryV2 } = queryPropertiesStore.get(this);
442
444
  let offset = 0, orders = [];
443
445
  if (queryV2) {
@@ -448,14 +450,17 @@ class _KQuery {
448
450
  offset = queryBuilder.getOffset();
449
451
  orders = queryBuilder.getOrder();
450
452
  }
453
+ if (pageLimit === 0) {
454
+ pageLimit = queryBuilder_1.defaultLimit;
455
+ }
451
456
  if (orders.length === 0 && offset === 0) {
452
457
  // 走主键翻页
453
- return await this.walkRecordsByID(handler);
458
+ return await this.walkRecordsByID(handler, pageLimit);
454
459
  }
455
460
  // 走 limit offset 翻页
456
- return await this.walkRecordsByLimitOffset(handler);
461
+ return await this.walkRecordsByLimitOffset(handler, pageLimit);
457
462
  }
458
- async walkRecordsByID(handler) {
463
+ async walkRecordsByID(handler, pageLimit) {
459
464
  const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
460
465
  let limit = 0, selectFields = [];
461
466
  if (queryV2) {
@@ -471,9 +476,23 @@ class _KQuery {
471
476
  selectFields = queryBuilder.getSelect();
472
477
  }
473
478
  let maxId = 0, queryCount = 0;
479
+ let criterionV2;
480
+ let criterion;
481
+ if (appCtx) {
482
+ criterionV2 = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
483
+ criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
484
+ }
485
+ else {
486
+ criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
487
+ if (queryBuilder.getLogic().logics.length > 0) { // 有自定义搜索条件时,需要获取 fields
488
+ criterion = await (0, logic_1.handleCriterion)(criterion);
489
+ }
490
+ // 在最后,添加 _id>maxId 条件
491
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
492
+ }
474
493
  while (true) {
475
- let newLimit = queryBuilder_1.defaultLimit;
476
- if (limit > 0 && limit - queryCount < queryBuilder_1.defaultLimit) {
494
+ let newLimit = pageLimit;
495
+ if (limit > 0 && limit - queryCount < pageLimit) {
477
496
  newLimit = limit - queryCount;
478
497
  }
479
498
  if (newLimit <= 0) {
@@ -481,23 +500,24 @@ class _KQuery {
481
500
  }
482
501
  let rs;
483
502
  if (appCtx) {
484
- let criterion = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
485
- criterion.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
503
+ criterionV2[criterionV2.length - 1].rightValue = maxId;
486
504
  const param = {
487
505
  limit: newLimit,
488
506
  offset: 0,
489
507
  sort: [{ field: '_id', direction: 'asc', type: '' }],
490
508
  fields: selectFields,
491
509
  count: false,
492
- filter: criterion,
510
+ filter: criterionV2,
493
511
  };
494
512
  rs = await (0, common_1.runCtxForOpenSDK)(appCtx, async () => {
495
513
  return await Request.GetInstance().openSDKGetRecords(apiName, param);
496
514
  });
497
515
  }
498
516
  else {
499
- let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
500
- rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.fuzzySearch, [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, newLimit, false, this.authType);
517
+ // 更新 maxId
518
+ criterion.conditions[criterion.conditions.length - 1].right.settings.data = maxId;
519
+ // 发起请求
520
+ rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, newLimit, false, this.authType);
501
521
  }
502
522
  queryCount += rs.length;
503
523
  rs.forEach((r) => {
@@ -511,12 +531,12 @@ class _KQuery {
511
531
  catch (err) {
512
532
  throw err;
513
533
  }
514
- if (rs.length < queryBuilder_1.defaultLimit) {
534
+ if (rs.length < pageLimit) {
515
535
  break;
516
536
  }
517
537
  }
518
538
  }
519
- async walkRecordsByLimitOffset(handler) {
539
+ async walkRecordsByLimitOffset(handler, pageLimit) {
520
540
  const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
521
541
  let limit = 0, offset = 0, selectFields = [], orders = [];
522
542
  if (queryV2) {
@@ -540,38 +560,54 @@ class _KQuery {
540
560
  orders = [{ field: '_id', direction: 'asc', type: '' }];
541
561
  }
542
562
  let maxId = 0, queryCount = 0;
543
- for (let i = offset; !limit || i < offset + limit; i += queryBuilder_1.defaultLimit) {
563
+ let criterionV2;
564
+ let criterion;
565
+ if (appCtx) {
566
+ criterionV2 = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
567
+ criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
568
+ }
569
+ else {
570
+ criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
571
+ if (queryBuilder.getLogic().logics.length > 0) { // 有自定义搜索条件时,需要获取 fields
572
+ criterion = await (0, logic_1.handleCriterion)(criterion);
573
+ }
574
+ // 在最后,添加 _id>maxId 条件
575
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
576
+ }
577
+ let curOffset = offset;
578
+ while (true) {
544
579
  let newLimit = function () {
545
580
  if (limit == 0) {
546
- return queryBuilder_1.defaultLimit;
581
+ return pageLimit;
547
582
  }
548
- if (offset + limit - i < queryBuilder_1.defaultLimit) {
549
- return offset + limit - i;
583
+ if (offset + limit - curOffset < pageLimit) {
584
+ return offset + limit - curOffset;
550
585
  }
551
- return queryBuilder_1.defaultLimit;
586
+ return pageLimit;
552
587
  }();
553
588
  if (newLimit <= 0) {
554
589
  break;
555
590
  }
556
591
  let rs;
557
592
  if (appCtx) {
558
- let criterion = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
559
- criterion.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
593
+ criterionV2[criterionV2.length - 1].rightValue = maxId;
560
594
  const param = {
561
595
  limit: newLimit,
562
- offset: i,
596
+ offset: curOffset,
563
597
  sort: orders,
564
598
  fields: selectFields,
565
599
  count: false,
566
- filter: criterion,
600
+ filter: criterionV2,
567
601
  };
568
602
  rs = await (0, common_1.runCtxForOpenSDK)(appCtx, async () => {
569
603
  return await Request.GetInstance().openSDKGetRecords(apiName, param);
570
604
  });
571
605
  }
572
606
  else {
573
- let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
574
- rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.fuzzySearch, orders, false, selectFields, i, newLimit, false, this.authType);
607
+ // 更新 maxId
608
+ criterion.conditions[criterion.conditions.length - 1].right.settings.data = maxId;
609
+ // 发起请求
610
+ rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, orders, false, selectFields, curOffset, newLimit, false, this.authType);
575
611
  }
576
612
  queryCount += rs.length;
577
613
  try {
@@ -582,9 +618,10 @@ class _KQuery {
582
618
  catch (err) {
583
619
  throw err;
584
620
  }
585
- if (rs.length < queryBuilder_1.defaultLimit) {
621
+ if (rs.length < pageLimit) {
586
622
  break;
587
623
  }
624
+ curOffset += pageLimit;
588
625
  }
589
626
  }
590
627
  async getRecordsByPage() {
@@ -608,8 +645,12 @@ class _KQuery {
608
645
  });
609
646
  }
610
647
  else {
611
- let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
612
- rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), null, // FindAll 不再迭代
648
+ let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
649
+ if (queryBuilder.getLogic().logics.length > 0) {
650
+ criterion = await (0, logic_1.handleCriterion)(criterion);
651
+ }
652
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
653
+ rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, null, // FindAll 不再迭代
613
654
  [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, queryBuilder_1.defaultLimit, false, this.authType);
614
655
  }
615
656
  rs.forEach((r) => {
@@ -742,7 +783,10 @@ class _KQuery {
742
783
  }
743
784
  else {
744
785
  let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
745
- return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, await (0, logic_1.handleCriterion)(criterion), queryBuilder.fuzzySearch, queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), true, this.authType);
786
+ if (queryBuilder.getLogic().logics.length > 0) {
787
+ criterion = await (0, logic_1.handleCriterion)(criterion);
788
+ }
789
+ return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), true, this.authType);
746
790
  }
747
791
  }
748
792
  ;
@@ -24,8 +24,16 @@ export type Criterion = {
24
24
  * @param objectApiName
25
25
  * @param maxId 添加 _id>maxId 条件,为 -1 表示不使用该参数
26
26
  */
27
- export declare function buildCriterion(logic: Logic, objectApiName: string, maxId?: number): Criterion | string;
27
+ export declare function buildCriterion(logic: Logic, objectApiName: string): Criterion;
28
28
  export declare function buildExpression(objectApiName: string, left: string, right: Condition | UserDataType, index?: number): Expression;
29
+ /**
30
+ * 在 criterion 中添加 _id>maxId 的判断条件
31
+ *
32
+ * @param criterion
33
+ * @param apiName
34
+ * @param maxId 添加 _id>maxId 条件
35
+ */
36
+ export declare function addIDCriterion(criterion: Criterion, apiName: string, maxId: number): Criterion;
29
37
  export declare class Logic {
30
38
  value: string;
31
39
  expressions: {
@@ -47,4 +55,4 @@ export declare class Logic {
47
55
  addLogic(logic: Logic): void;
48
56
  }
49
57
  export declare function buildLogic(param: (Logic | object)[], logicValue?: string): Logic;
50
- export declare function handleCriterion(criterion: string | Criterion): Promise<string | Criterion>;
58
+ export declare function handleCriterion(criterion: Criterion): Promise<Criterion>;
@@ -2,7 +2,7 @@
2
2
  // Copyright 2022 ByteDance Ltd. and/or its affiliates
3
3
  // SPDX-License-Identifier: MIT
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.handleCriterion = exports.buildLogic = exports.Logic = exports.buildExpression = exports.buildCriterion = exports.Condition = void 0;
5
+ exports.handleCriterion = exports.buildLogic = exports.Logic = exports.addIDCriterion = exports.buildExpression = exports.buildCriterion = exports.Condition = void 0;
6
6
  const expression_1 = require("./expression");
7
7
  const operator_1 = require("./operator");
8
8
  const server_common_node_1 = require("@byted-apaas/server-common-node");
@@ -59,7 +59,7 @@ function isKunlunTypeOrSerializedObject(obj) {
59
59
  * @param objectApiName
60
60
  * @param maxId 添加 _id>maxId 条件,为 -1 表示不使用该参数
61
61
  */
62
- function buildCriterion(logic, objectApiName, maxId = -1) {
62
+ function buildCriterion(logic, objectApiName) {
63
63
  let index = 0;
64
64
  let logics = [];
65
65
  let expressions = [];
@@ -117,18 +117,10 @@ function buildCriterion(logic, objectApiName, maxId = -1) {
117
117
  f(logic);
118
118
  // 如果未设置filter,默认添加: _isDeleted = false
119
119
  if (expressions.length < 1) {
120
- if (maxId >= 0) {
121
- return `{"conditions":[{"index":1,"left":{"type":"metadataVariable","settings":{"fieldPath":
122
- [{"objectApiName":"${objectApiName}","fieldApiName":"_isDeleted"}]}},"operator":"equals","right":
123
- {"type":"constant","settings":{"data":false}}},{"index":2,"left":{"type":"metadataVariable","settings":
124
- {"fieldPath":[{"objectApiName":"${objectApiName}","fieldApiName":"_id"}]}},"operator":"greaterThan","right":
125
- {"type":"constant","settings":{"data":${maxId}}}}],"logic":"1 and 2"}`;
126
- }
127
- else {
128
- return `{"conditions":[{"index":1,"left":{"type":"metadataVariable","settings":{"fieldPath":
129
- [{"objectApiName":"${objectApiName}","fieldApiName":"_isDeleted"}]}},"operator":"equals","right":
130
- {"type":"constant","settings":{"data":false}}}],"logic":"1"}`;
131
- }
120
+ logics = [];
121
+ expressions.push(buildExpression(objectApiName, '_isDeleted', false, expressions.length + 1));
122
+ logics.push('1');
123
+ return { conditions: expressions, logic: logics.join('') };
132
124
  }
133
125
  let isContainDeleted = false;
134
126
  for (let i = 0; i < expressions.length; i++) {
@@ -148,11 +140,6 @@ function buildCriterion(logic, objectApiName, maxId = -1) {
148
140
  logics.splice(0, 0, '(');
149
141
  logics.push(' and ', `${expressions.length}`, ')');
150
142
  }
151
- if (maxId >= 0) {
152
- expressions.push(buildExpression(objectApiName, '_id', new Condition('_id', maxId, operator_1.operates.GT), expressions.length + 1));
153
- logics.splice(0, 0, '(');
154
- logics.push(' and ', `${expressions.length}`, ')');
155
- }
156
143
  return { conditions: expressions, logic: logics.join('') };
157
144
  }
158
145
  exports.buildCriterion = buildCriterion;
@@ -183,6 +170,22 @@ function buildExpression(objectApiName, left, right, index = 1) {
183
170
  return null;
184
171
  }
185
172
  exports.buildExpression = buildExpression;
173
+ /**
174
+ * 在 criterion 中添加 _id>maxId 的判断条件
175
+ *
176
+ * @param criterion
177
+ * @param apiName
178
+ * @param maxId 添加 _id>maxId 条件
179
+ */
180
+ function addIDCriterion(criterion, apiName, maxId) {
181
+ criterion.conditions.push(buildExpression(apiName, '_id', new Condition('_id', maxId, operator_1.operates.GT), criterion.conditions.length + 1));
182
+ const logics = criterion.logic.split('');
183
+ logics.splice(0, 0, '(');
184
+ logics.push(' and ', `${criterion.conditions.length}`, ')');
185
+ criterion.logic = logics.join('');
186
+ return criterion;
187
+ }
188
+ exports.addIDCriterion = addIDCriterion;
186
189
  // 逻辑操作:and 和 or
187
190
  class Logic {
188
191
  constructor(expressions = null, logicValue = 'and') {
@@ -339,9 +342,6 @@ class Filter {
339
342
  }
340
343
  }
341
344
  async function handleCriterion(criterion) {
342
- if (typeof criterion === 'string') {
343
- return criterion;
344
- }
345
345
  if (!criterion || !criterion.conditions || !(criterion.conditions instanceof Array)) {
346
346
  return criterion;
347
347
  }
@@ -355,7 +355,7 @@ async function handleCriterion(criterion) {
355
355
  }
356
356
  await handleCondition(condition, fieldRes);
357
357
  }
358
- return JSON.stringify(criterion);
358
+ return criterion;
359
359
  }
360
360
  exports.handleCriterion = handleCriterion;
361
361
  async function handleLookupCondition(conditions) {
@@ -376,6 +376,7 @@ async function handleLookupCondition(conditions) {
376
376
  let lookupMap = new Map(), dateSet = new Set();
377
377
  let firstObjApiName = conditions[0].left.settings.fieldPath[0].objectApiName;
378
378
  let fields = [];
379
+ // 获取对象的字段
379
380
  if (queryMap.size === 1) {
380
381
  fields.push(await Request.GetInstance().getField(firstObjApiName, fieldName));
381
382
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byted-apaas/server-sdk-node",
3
- "version": "1.1.16",
3
+ "version": "1.1.17-beta.11",
4
4
  "description": "aPaaS Server SDK",
5
5
  "author": "zhouwexin <zhouwexin@bytedance.com>",
6
6
  "homepage": "",
@@ -296,9 +296,12 @@ async function getRecordsOrCountByCriterion(objectApiName, criterion, fuzzySearc
296
296
  if (needCount) {
297
297
  fieldApiNames = ['_id'];
298
298
  }
299
+ if (typeof criterion === 'string') {
300
+ criterion = JSON.parse(criterion);
301
+ }
299
302
  // 3.请求
300
303
  options.json = {
301
- 'criterion': JSON.parse(criterion),
304
+ criterion,
302
305
  order,
303
306
  'ignore_back_lookup_field': ignoreBackLookupField,
304
307
  'field_api_names': fieldApiNames,
@@ -646,10 +649,17 @@ async function oql(oql, args, namedArgs, authType) {
646
649
  }
647
650
  options.headers[constants_3.AuthTypeHttpHeader] = authType;
648
651
  }
649
- let result = await openapi.doRequest(null, urlPath, options);
650
- if (result && result.rows) {
651
- permissionUtils.appendUnauthFieldRecordList("", result.rows, result.unauth_field_slice, true);
652
- return result.rows;
652
+ try {
653
+ let result = await openapi.doRequest(null, urlPath, options);
654
+ if (result && result.rows) {
655
+ permissionUtils.appendUnauthFieldRecordList("", result.rows, result.unauth_field_slice, true);
656
+ return result.rows;
657
+ }
658
+ }
659
+ catch (e) {
660
+ if (e.msg.includes("TimeoutError")) {
661
+ throw new exceptions.CallOpenapiError(`OQL timeout, please check the query statement and the execution time of the query statement.`);
662
+ }
653
663
  }
654
664
  return [];
655
665
  }