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

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.
@@ -163,6 +163,21 @@ export interface _IKQuery<T> {
163
163
  * ```
164
164
  */
165
165
  findStream(handler: (records: object[]) => Promise<void>): Promise<void>;
166
+ /**
167
+ * 遍历全部符合条件的记录
168
+ * 注:
169
+ * 如果未设置排序字段,默认以 _id 增序查询;
170
+ * 如果有设置排序字段,必须设置具有唯一属性的字段,否则会有数据重复的风险;
171
+ * @param handler 业务处理函数
172
+ * @param pageLimit 分页查询的数量
173
+ * @example
174
+ * ```
175
+ * await application.data.object('_user').findStream(async (records) => {
176
+ * // doSomething ...
177
+ * }, 300);
178
+ * ```
179
+ */
180
+ findStream(handler: (records: object[]) => Promise<void>, pageLimit: number): Promise<void>;
166
181
  /**
167
182
  * 无需入参,返回符合条件的记录,单次返回 200 条
168
183
  * @example
@@ -411,8 +411,11 @@ class _KQuery {
411
411
  });
412
412
  }
413
413
  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);
414
+ let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
415
+ if (queryBuilder.getLogic().logics.length > 0) {
416
+ criterion = await (0, logic_1.handleCriterion)(criterion);
417
+ }
418
+ return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), false, this.authType);
416
419
  }
417
420
  }
418
421
  ;
@@ -437,7 +440,7 @@ class _KQuery {
437
440
  return await this.getRecordsByPage();
438
441
  }
439
442
  ;
440
- async findStream(handler) {
443
+ async findStream(handler, pageLimit) {
441
444
  const { queryBuilder, queryV2 } = queryPropertiesStore.get(this);
442
445
  let offset = 0, orders = [];
443
446
  if (queryV2) {
@@ -448,14 +451,17 @@ class _KQuery {
448
451
  offset = queryBuilder.getOffset();
449
452
  orders = queryBuilder.getOrder();
450
453
  }
454
+ if (pageLimit == 0) {
455
+ pageLimit = queryBuilder_1.defaultLimit;
456
+ }
451
457
  if (orders.length === 0 && offset === 0) {
452
458
  // 走主键翻页
453
- return await this.walkRecordsByID(handler);
459
+ return await this.walkRecordsByID(handler, pageLimit);
454
460
  }
455
461
  // 走 limit offset 翻页
456
- return await this.walkRecordsByLimitOffset(handler);
462
+ return await this.walkRecordsByLimitOffset(handler, pageLimit);
457
463
  }
458
- async walkRecordsByID(handler) {
464
+ async walkRecordsByID(handler, pageLimit) {
459
465
  const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
460
466
  let limit = 0, selectFields = [];
461
467
  if (queryV2) {
@@ -471,9 +477,20 @@ class _KQuery {
471
477
  selectFields = queryBuilder.getSelect();
472
478
  }
473
479
  let maxId = 0, queryCount = 0;
480
+ let criterionV2;
481
+ let criterion;
482
+ if (appCtx) {
483
+ criterionV2 = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
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
+ }
474
491
  while (true) {
475
- let newLimit = queryBuilder_1.defaultLimit;
476
- if (limit > 0 && limit - queryCount < queryBuilder_1.defaultLimit) {
492
+ let newLimit = pageLimit;
493
+ if (limit > 0 && limit - queryCount < pageLimit) {
477
494
  newLimit = limit - queryCount;
478
495
  }
479
496
  if (newLimit <= 0) {
@@ -481,23 +498,24 @@ class _KQuery {
481
498
  }
482
499
  let rs;
483
500
  if (appCtx) {
484
- let criterion = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
485
- criterion.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
501
+ criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
486
502
  const param = {
487
503
  limit: newLimit,
488
504
  offset: 0,
489
505
  sort: [{ field: '_id', direction: 'asc', type: '' }],
490
506
  fields: selectFields,
491
507
  count: false,
492
- filter: criterion,
508
+ filter: criterionV2,
493
509
  };
494
510
  rs = await (0, common_1.runCtxForOpenSDK)(appCtx, async () => {
495
511
  return await Request.GetInstance().openSDKGetRecords(apiName, param);
496
512
  });
497
513
  }
498
514
  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);
515
+ // 添加 _id>maxId 条件
516
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
517
+ // 发起请求
518
+ rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, newLimit, false, this.authType);
501
519
  }
502
520
  queryCount += rs.length;
503
521
  rs.forEach((r) => {
@@ -511,12 +529,12 @@ class _KQuery {
511
529
  catch (err) {
512
530
  throw err;
513
531
  }
514
- if (rs.length < queryBuilder_1.defaultLimit) {
532
+ if (rs.length < pageLimit) {
515
533
  break;
516
534
  }
517
535
  }
518
536
  }
519
- async walkRecordsByLimitOffset(handler) {
537
+ async walkRecordsByLimitOffset(handler, pageLimit) {
520
538
  const { apiName, appCtx, queryBuilder, queryV2 } = queryPropertiesStore.get(this);
521
539
  let limit = 0, offset = 0, selectFields = [], orders = [];
522
540
  if (queryV2) {
@@ -540,38 +558,50 @@ class _KQuery {
540
558
  orders = [{ field: '_id', direction: 'asc', type: '' }];
541
559
  }
542
560
  let maxId = 0, queryCount = 0;
543
- for (let i = offset; !limit || i < offset + limit; i += queryBuilder_1.defaultLimit) {
561
+ let criterionV2;
562
+ let criterion;
563
+ if (appCtx) {
564
+ criterionV2 = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
565
+ }
566
+ else {
567
+ criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
568
+ if (queryBuilder.getLogic().logics.length > 0) {
569
+ criterion = await (0, logic_1.handleCriterion)(criterion);
570
+ }
571
+ }
572
+ for (let i = offset; !limit || i < offset + limit; i += pageLimit) {
544
573
  let newLimit = function () {
545
574
  if (limit == 0) {
546
- return queryBuilder_1.defaultLimit;
575
+ return pageLimit;
547
576
  }
548
- if (offset + limit - i < queryBuilder_1.defaultLimit) {
577
+ if (offset + limit - i < pageLimit) {
549
578
  return offset + limit - i;
550
579
  }
551
- return queryBuilder_1.defaultLimit;
580
+ return pageLimit;
552
581
  }();
553
582
  if (newLimit <= 0) {
554
583
  break;
555
584
  }
556
585
  let rs;
557
586
  if (appCtx) {
558
- let criterion = (0, logicV2_1.buildCriterionV2)(queryV2.filter);
559
- criterion.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
587
+ criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
560
588
  const param = {
561
589
  limit: newLimit,
562
590
  offset: i,
563
591
  sort: orders,
564
592
  fields: selectFields,
565
593
  count: false,
566
- filter: criterion,
594
+ filter: criterionV2,
567
595
  };
568
596
  rs = await (0, common_1.runCtxForOpenSDK)(appCtx, async () => {
569
597
  return await Request.GetInstance().openSDKGetRecords(apiName, param);
570
598
  });
571
599
  }
572
600
  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);
601
+ // 添加 _id>maxId 条件
602
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
603
+ // 发起请求
604
+ rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, orders, false, selectFields, i, newLimit, false, this.authType);
575
605
  }
576
606
  queryCount += rs.length;
577
607
  try {
@@ -582,7 +612,7 @@ class _KQuery {
582
612
  catch (err) {
583
613
  throw err;
584
614
  }
585
- if (rs.length < queryBuilder_1.defaultLimit) {
615
+ if (rs.length < pageLimit) {
586
616
  break;
587
617
  }
588
618
  }
@@ -608,8 +638,12 @@ class _KQuery {
608
638
  });
609
639
  }
610
640
  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 不再迭代
641
+ let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
642
+ if (queryBuilder.getLogic().logics.length > 0) {
643
+ criterion = await (0, logic_1.handleCriterion)(criterion);
644
+ }
645
+ criterion = (0, logic_1.addIDCriterion)(criterion, apiName, maxId);
646
+ rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, null, // FindAll 不再迭代
613
647
  [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, queryBuilder_1.defaultLimit, false, this.authType);
614
648
  }
615
649
  rs.forEach((r) => {
@@ -742,7 +776,10 @@ class _KQuery {
742
776
  }
743
777
  else {
744
778
  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);
779
+ if (queryBuilder.getLogic().logics.length > 0) {
780
+ criterion = await (0, logic_1.handleCriterion)(criterion);
781
+ }
782
+ return await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, queryBuilder.getOrder(), false, queryBuilder.getSelect(), queryBuilder.getOffset(), queryBuilder.getLimit(), true, this.authType);
746
783
  }
747
784
  }
748
785
  ;
@@ -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,9 @@ 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
+ expressions.push(buildExpression(objectApiName, '_isDeleted', false, expressions.length + 1));
121
+ logics.push('1');
122
+ return { conditions: expressions, logic: logics.join('') };
132
123
  }
133
124
  let isContainDeleted = false;
134
125
  for (let i = 0; i < expressions.length; i++) {
@@ -148,11 +139,6 @@ function buildCriterion(logic, objectApiName, maxId = -1) {
148
139
  logics.splice(0, 0, '(');
149
140
  logics.push(' and ', `${expressions.length}`, ')');
150
141
  }
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
142
  return { conditions: expressions, logic: logics.join('') };
157
143
  }
158
144
  exports.buildCriterion = buildCriterion;
@@ -183,6 +169,22 @@ function buildExpression(objectApiName, left, right, index = 1) {
183
169
  return null;
184
170
  }
185
171
  exports.buildExpression = buildExpression;
172
+ /**
173
+ * 在 criterion 中添加 _id>maxId 的判断条件
174
+ *
175
+ * @param criterion
176
+ * @param apiName
177
+ * @param maxId 添加 _id>maxId 条件
178
+ */
179
+ function addIDCriterion(criterion, apiName, maxId) {
180
+ criterion.conditions.push(buildExpression(apiName, '_id', new Condition('_id', maxId, operator_1.operates.GT), criterion.conditions.length + 1));
181
+ let logics = criterion.logic.split('');
182
+ logics.splice(0, 0, '(');
183
+ logics.push(' and ', `${criterion.conditions.length}`, ')');
184
+ criterion.logic = logics.join('');
185
+ return criterion;
186
+ }
187
+ exports.addIDCriterion = addIDCriterion;
186
188
  // 逻辑操作:and 和 or
187
189
  class Logic {
188
190
  constructor(expressions = null, logicValue = 'and') {
@@ -339,9 +341,6 @@ class Filter {
339
341
  }
340
342
  }
341
343
  async function handleCriterion(criterion) {
342
- if (typeof criterion === 'string') {
343
- return criterion;
344
- }
345
344
  if (!criterion || !criterion.conditions || !(criterion.conditions instanceof Array)) {
346
345
  return criterion;
347
346
  }
@@ -355,7 +354,7 @@ async function handleCriterion(criterion) {
355
354
  }
356
355
  await handleCondition(condition, fieldRes);
357
356
  }
358
- return JSON.stringify(criterion);
357
+ return criterion;
359
358
  }
360
359
  exports.handleCriterion = handleCriterion;
361
360
  async function handleLookupCondition(conditions) {
@@ -376,6 +375,7 @@ async function handleLookupCondition(conditions) {
376
375
  let lookupMap = new Map(), dateSet = new Set();
377
376
  let firstObjApiName = conditions[0].left.settings.fieldPath[0].objectApiName;
378
377
  let fields = [];
378
+ // 获取对象的字段
379
379
  if (queryMap.size === 1) {
380
380
  fields.push(await Request.GetInstance().getField(firstObjApiName, fieldName));
381
381
  }
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.2",
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': criterion,
302
305
  order,
303
306
  'ignore_back_lookup_field': ignoreBackLookupField,
304
307
  'field_api_names': fieldApiNames,