@byted-apaas/server-sdk-node 1.1.17-beta.1 → 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,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;
@@ -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, pageLimit) {
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,7 +450,7 @@ class _KQuery {
448
450
  offset = queryBuilder.getOffset();
449
451
  orders = queryBuilder.getOrder();
450
452
  }
451
- if (pageLimit == 0) {
453
+ if (pageLimit === 0) {
452
454
  pageLimit = queryBuilder_1.defaultLimit;
453
455
  }
454
456
  if (orders.length === 0 && offset === 0) {
@@ -481,8 +483,12 @@ class _KQuery {
481
483
  criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
482
484
  }
483
485
  else {
484
- criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
485
- criterion = await (0, logic_1.handleCriterion)(criterion);
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);
486
492
  }
487
493
  while (true) {
488
494
  let newLimit = pageLimit;
@@ -494,6 +500,7 @@ class _KQuery {
494
500
  }
495
501
  let rs;
496
502
  if (appCtx) {
503
+ criterionV2[criterionV2.length - 1].rightValue = maxId;
497
504
  const param = {
498
505
  limit: newLimit,
499
506
  offset: 0,
@@ -507,6 +514,9 @@ class _KQuery {
507
514
  });
508
515
  }
509
516
  else {
517
+ // 更新 maxId
518
+ criterion.conditions[criterion.conditions.length - 1].right.settings.data = maxId;
519
+ // 发起请求
510
520
  rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, criterion, queryBuilder.fuzzySearch, [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, newLimit, false, this.authType);
511
521
  }
512
522
  queryCount += rs.length;
@@ -557,16 +567,21 @@ class _KQuery {
557
567
  criterionV2.push({ leftValue: '_id', operator: operator_1.operates.GT, rightValue: maxId });
558
568
  }
559
569
  else {
560
- criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
561
- criterion = await (0, logic_1.handleCriterion)(criterion);
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);
562
576
  }
563
- for (let i = offset; !limit || i < offset + limit; i += pageLimit) {
577
+ let curOffset = offset;
578
+ while (true) {
564
579
  let newLimit = function () {
565
580
  if (limit == 0) {
566
581
  return pageLimit;
567
582
  }
568
- if (offset + limit - i < pageLimit) {
569
- return offset + limit - i;
583
+ if (offset + limit - curOffset < pageLimit) {
584
+ return offset + limit - curOffset;
570
585
  }
571
586
  return pageLimit;
572
587
  }();
@@ -575,9 +590,10 @@ class _KQuery {
575
590
  }
576
591
  let rs;
577
592
  if (appCtx) {
593
+ criterionV2[criterionV2.length - 1].rightValue = maxId;
578
594
  const param = {
579
595
  limit: newLimit,
580
- offset: i,
596
+ offset: curOffset,
581
597
  sort: orders,
582
598
  fields: selectFields,
583
599
  count: false,
@@ -588,7 +604,10 @@ class _KQuery {
588
604
  });
589
605
  }
590
606
  else {
591
- rs = await Request.GetInstance().getRecordsOrCountByCriterion(apiName, 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);
592
611
  }
593
612
  queryCount += rs.length;
594
613
  try {
@@ -602,6 +621,7 @@ class _KQuery {
602
621
  if (rs.length < pageLimit) {
603
622
  break;
604
623
  }
624
+ curOffset += pageLimit;
605
625
  }
606
626
  }
607
627
  async getRecordsByPage() {
@@ -625,8 +645,12 @@ class _KQuery {
625
645
  });
626
646
  }
627
647
  else {
628
- let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName, maxId);
629
- 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 不再迭代
630
654
  [new order_1.Order('_id', 'asc')], false, queryBuilder.getSelect(), 0, queryBuilder_1.defaultLimit, false, this.authType);
631
655
  }
632
656
  rs.forEach((r) => {
@@ -759,7 +783,10 @@ class _KQuery {
759
783
  }
760
784
  else {
761
785
  let criterion = (0, logic_1.buildCriterion)(queryBuilder.getLogic(), apiName);
762
- 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);
763
790
  }
764
791
  }
765
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.17-beta.1",
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
  }