@byted-apaas/server-sdk-node 1.1.17-beta.3 → 1.1.17-beta.5

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,21 +155,7 @@ export interface _IKQuery<T> {
155
155
  * 如果未设置排序字段,默认以 _id 增序查询;
156
156
  * 如果有设置排序字段,必须设置具有唯一属性的字段,否则会有数据重复的风险;
157
157
  * @param handler 业务处理函数
158
- * @example
159
- * ```
160
- * await application.data.object('_user').findStream(async (records) => {
161
- * // doSomething ...
162
- * });
163
- * ```
164
- */
165
- findStream(handler: (records: object[]) => Promise<void>): Promise<void>;
166
- /**
167
- * 遍历全部符合条件的记录
168
- * 注:
169
- * 如果未设置排序字段,默认以 _id 增序查询;
170
- * 如果有设置排序字段,必须设置具有唯一属性的字段,否则会有数据重复的风险;
171
- * @param handler 业务处理函数
172
- * @param pageLimit 分页查询的数量
158
+ * @param pageLimit 分页查询的数量,可选参数,默认值为 200,
173
159
  * @example
174
160
  * ```
175
161
  * await application.data.object('_user').findStream(async (records) => {
@@ -177,7 +163,7 @@ export interface _IKQuery<T> {
177
163
  * }, 300);
178
164
  * ```
179
165
  */
180
- findStream(handler: (records: object[]) => Promise<void>, pageLimit: number): Promise<void>;
166
+ findStream: (handler: (records: object[]) => Promise<void>, pageLimit?: number) => Promise<void>;
181
167
  /**
182
168
  * 无需入参,返回符合条件的记录,单次返回 200 条
183
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;
@@ -451,7 +450,7 @@ class _KQuery {
451
450
  offset = queryBuilder.getOffset();
452
451
  orders = queryBuilder.getOrder();
453
452
  }
454
- if (pageLimit == 0) {
453
+ if (pageLimit === 0) {
455
454
  pageLimit = queryBuilder_1.defaultLimit;
456
455
  }
457
456
  if (orders.length === 0 && offset === 0) {
@@ -481,12 +480,15 @@ class _KQuery {
481
480
  let criterion;
482
481
  if (appCtx) {
483
482
  criterionV2 = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
483
+ criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
484
484
  }
485
485
  else {
486
486
  criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
487
487
  if (queryBuilder.getLogic().logics.length > 0) { // 有自定义搜索条件时,需要获取 fields
488
488
  criterion = await (0, logic_1.handleCriterion)(criterion);
489
489
  }
490
+ // 在最后,添加 _id>maxId 条件
491
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
490
492
  }
491
493
  while (true) {
492
494
  let newLimit = pageLimit;
@@ -498,7 +500,7 @@ class _KQuery {
498
500
  }
499
501
  let rs;
500
502
  if (appCtx) {
501
- criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
503
+ criterionV2[criterionV2.length - 1].rightValue = maxId;
502
504
  const param = {
503
505
  limit: newLimit,
504
506
  offset: 0,
@@ -512,8 +514,8 @@ class _KQuery {
512
514
  });
513
515
  }
514
516
  else {
515
- // 添加 _id>maxId 条件
516
- criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
517
+ // 更新 maxId
518
+ criterion.conditions[criterion.conditions.length - 1].right.settings.data = maxId;
517
519
  // 发起请求
518
520
  rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, newLimit, false, this.authType);
519
521
  }
@@ -562,20 +564,24 @@ class _KQuery {
562
564
  let criterion;
563
565
  if (appCtx) {
564
566
  criterionV2 = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
567
+ criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
565
568
  }
566
569
  else {
567
570
  criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
568
- if (queryBuilder.getLogic().logics.length > 0) {
571
+ if (queryBuilder.getLogic().logics.length > 0) { // 有自定义搜索条件时,需要获取 fields
569
572
  criterion = await (0, logic_1.handleCriterion)(criterion);
570
573
  }
574
+ // 在最后,添加 _id>maxId 条件
575
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
571
576
  }
572
- for (let i = offset; !limit || i < offset + limit; i += pageLimit) {
577
+ let curOffset = offset;
578
+ while (true) {
573
579
  let newLimit = function () {
574
580
  if (limit == 0) {
575
581
  return pageLimit;
576
582
  }
577
- if (offset + limit - i < pageLimit) {
578
- return offset + limit - i;
583
+ if (offset + limit - curOffset < pageLimit) {
584
+ return offset + limit - curOffset;
579
585
  }
580
586
  return pageLimit;
581
587
  }();
@@ -584,10 +590,10 @@ class _KQuery {
584
590
  }
585
591
  let rs;
586
592
  if (appCtx) {
587
- criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
593
+ criterionV2[criterionV2.length - 1].rightValue = maxId;
588
594
  const param = {
589
595
  limit: newLimit,
590
- offset: i,
596
+ offset: curOffset,
591
597
  sort: orders,
592
598
  fields: selectFields,
593
599
  count: false,
@@ -598,10 +604,10 @@ class _KQuery {
598
604
  });
599
605
  }
600
606
  else {
601
- // 添加 _id>maxId 条件
602
- criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
607
+ // 更新 maxId
608
+ criterion.conditions[criterion.conditions.length - 1].right.settings.data = maxId;
603
609
  // 发起请求
604
- rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, orders, false, selectFields, i, newLimit, false, this.authType);
610
+ rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, orders, false, selectFields, curOffset, newLimit, false, this.authType);
605
611
  }
606
612
  queryCount += rs.length;
607
613
  try {
@@ -615,6 +621,7 @@ class _KQuery {
615
621
  if (rs.length < pageLimit) {
616
622
  break;
617
623
  }
624
+ curOffset += pageLimit;
618
625
  }
619
626
  }
620
627
  async getRecordsByPage() {
@@ -179,7 +179,7 @@ exports.buildExpression = buildExpression;
179
179
  */
180
180
  function addIDCriterion(criterion, apiName, maxId) {
181
181
  criterion.conditions.push(buildExpression(apiName, '_id', new Condition('_id', maxId, operator_1.operates.GT), criterion.conditions.length + 1));
182
- let logics = criterion.logic.split('');
182
+ const logics = criterion.logic.split('');
183
183
  logics.splice(0, 0, '(');
184
184
  logics.push(' and ', `${criterion.conditions.length}`, ')');
185
185
  criterion.logic = logics.join('');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byted-apaas/server-sdk-node",
3
- "version": "1.1.17-beta.3",
3
+ "version": "1.1.17-beta.5",
4
4
  "description": "aPaaS Server SDK",
5
5
  "author": "zhouwexin <zhouwexin@bytedance.com>",
6
6
  "homepage": "",
@@ -301,7 +301,7 @@ async function getRecordsOrCountByCriterion(objectApiName, criterion, fuzzySearc
301
301
  }
302
302
  // 3.请求
303
303
  options.json = {
304
- 'criterion': criterion,
304
+ criterion,
305
305
  order,
306
306
  'ignore_back_lookup_field': ignoreBackLookupField,
307
307
  'field_api_names': fieldApiNames,