apaas-oapi-client 0.1.26 → 0.1.27

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/dist/index.js CHANGED
@@ -355,10 +355,11 @@ class Client {
355
355
  /**
356
356
  * 批量更新 - 支持超过 100 条数据,自动拆分
357
357
  * @description 更新指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
358
- * @param params 请求参数
359
- * @returns 所有子请求的返回结果数组
358
+ * @param params 请求参数,包含 object_name, records, limit
359
+ * @returns { total, success, failed, successCount, failedCount }
360
360
  */
361
361
  recordsWithIterator: async (params) => {
362
+ var _a, _b, _c, _d;
362
363
  const { object_name, records, limit = 100 } = params;
363
364
  // 参数校验
364
365
  if (!records || !Array.isArray(records)) {
@@ -367,7 +368,7 @@ class Client {
367
368
  }
368
369
  if (records.length === 0) {
369
370
  this.log(LoggerLevel.warn, '[object.update.recordsWithIterator] Empty records array provided, returning empty result');
370
- return [];
371
+ return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
371
372
  }
372
373
  const chunkSize = limit;
373
374
  const chunks = [];
@@ -375,22 +376,62 @@ class Client {
375
376
  chunks.push(records.slice(i, i + chunkSize));
376
377
  }
377
378
  this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunking ${records.length} records into ${chunks.length} groups of ${chunkSize}`);
378
- const results = [];
379
+ const successItems = [];
380
+ const failedItems = [];
379
381
  for (const [index, chunk] of chunks.entries()) {
380
382
  this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
381
- const res = await this.object.update.records({
382
- object_name,
383
- records: chunk
384
- });
385
- if (res.code !== '0') {
386
- this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Error updating records: code=${res.code}, msg=${res.msg}`);
387
- throw new Error(res.msg || `Update failed with code ${res.code}`);
383
+ try {
384
+ const res = await this.object.update.records({
385
+ object_name,
386
+ records: chunk
387
+ });
388
+ if (res.code !== '0') {
389
+ this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
390
+ // 整个批次失败,将这批次的所有记录标记为失败
391
+ chunk.forEach((record) => {
392
+ failedItems.push({
393
+ _id: record._id || 'unknown',
394
+ success: false,
395
+ error: res.msg || `Update failed with code ${res.code}`
396
+ });
397
+ });
398
+ continue;
399
+ }
400
+ // 处理响应中的 items
401
+ if (res.data && Array.isArray(res.data.items)) {
402
+ res.data.items.forEach((item) => {
403
+ if (item.success) {
404
+ successItems.push(item);
405
+ }
406
+ else {
407
+ failedItems.push(item);
408
+ }
409
+ });
410
+ }
411
+ this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunk ${index + 1} completed: ${object_name}, success=${(_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.items) === null || _b === void 0 ? void 0 : _b.filter((i) => i.success).length}, failed=${(_d = (_c = res.data) === null || _c === void 0 ? void 0 : _c.items) === null || _d === void 0 ? void 0 : _d.filter((i) => !i.success).length}`);
412
+ this.log(LoggerLevel.trace, `[object.update.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
413
+ }
414
+ catch (error) {
415
+ this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
416
+ // 整个批次异常,将这批次的所有记录标记为失败
417
+ chunk.forEach((record) => {
418
+ failedItems.push({
419
+ _id: record._id || 'unknown',
420
+ success: false,
421
+ error: error instanceof Error ? error.message : String(error)
422
+ });
423
+ });
388
424
  }
389
- this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunk ${index + 1} completed: ${object_name}, code=${res.code}`);
390
- this.log(LoggerLevel.trace, `[object.update.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
391
- results.push(res);
392
425
  }
393
- return results;
426
+ const result = {
427
+ total: records.length,
428
+ success: successItems,
429
+ failed: failedItems,
430
+ successCount: successItems.length,
431
+ failedCount: failedItems.length
432
+ };
433
+ this.log(LoggerLevel.info, `[object.update.recordsWithIterator] Update completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
434
+ return result;
394
435
  }
395
436
  },
396
437
  delete: {
@@ -200,14 +200,27 @@ declare class Client {
200
200
  /**
201
201
  * 批量更新 - 支持超过 100 条数据,自动拆分
202
202
  * @description 更新指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
203
- * @param params 请求参数
204
- * @returns 所有子请求的返回结果数组
203
+ * @param params 请求参数,包含 object_name, records, limit
204
+ * @returns { total, success, failed, successCount, failedCount }
205
205
  */
206
206
  recordsWithIterator: (params: {
207
207
  object_name: string;
208
208
  records: any[];
209
209
  limit?: number;
210
- }) => Promise<any[]>;
210
+ }) => Promise<{
211
+ total: number;
212
+ success: Array<{
213
+ _id: string;
214
+ success: true;
215
+ }>;
216
+ failed: Array<{
217
+ _id: string;
218
+ success: false;
219
+ error?: string;
220
+ }>;
221
+ successCount: number;
222
+ failedCount: number;
223
+ }>;
211
224
  };
212
225
  delete: {
213
226
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apaas-oapi-client",
3
- "version": "0.1.26",
3
+ "version": "0.1.27",
4
4
  "main": "dist/index.js",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
package/src/index.ts CHANGED
@@ -553,10 +553,16 @@ class Client {
553
553
  /**
554
554
  * 批量更新 - 支持超过 100 条数据,自动拆分
555
555
  * @description 更新指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
556
- * @param params 请求参数
557
- * @returns 所有子请求的返回结果数组
556
+ * @param params 请求参数,包含 object_name, records, limit
557
+ * @returns { total, success, failed, successCount, failedCount }
558
558
  */
559
- recordsWithIterator: async (params: { object_name: string; records: any[]; limit?: number }): Promise<any[]> => {
559
+ recordsWithIterator: async (params: { object_name: string; records: any[]; limit?: number }): Promise<{
560
+ total: number;
561
+ success: Array<{ _id: string; success: true }>;
562
+ failed: Array<{ _id: string; success: false; error?: string }>;
563
+ successCount: number;
564
+ failedCount: number;
565
+ }> => {
560
566
  const { object_name, records, limit = 100 } = params;
561
567
 
562
568
  // 参数校验
@@ -567,7 +573,7 @@ class Client {
567
573
 
568
574
  if (records.length === 0) {
569
575
  this.log(LoggerLevel.warn, '[object.update.recordsWithIterator] Empty records array provided, returning empty result');
570
- return [];
576
+ return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
571
577
  }
572
578
 
573
579
  const chunkSize = limit;
@@ -578,27 +584,68 @@ class Client {
578
584
 
579
585
  this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunking ${records.length} records into ${chunks.length} groups of ${chunkSize}`);
580
586
 
581
- const results: any[] = [];
587
+ const successItems: Array<{ _id: string; success: true }> = [];
588
+ const failedItems: Array<{ _id: string; success: false; error?: string }> = [];
589
+
582
590
  for (const [index, chunk] of chunks.entries()) {
583
591
  this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
584
592
 
585
- const res = await this.object.update.records({
586
- object_name,
587
- records: chunk
588
- });
593
+ try {
594
+ const res = await this.object.update.records({
595
+ object_name,
596
+ records: chunk
597
+ });
589
598
 
590
- if (res.code !== '0') {
591
- this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Error updating records: code=${res.code}, msg=${res.msg}`);
592
- throw new Error(res.msg || `Update failed with code ${res.code}`);
593
- }
599
+ if (res.code !== '0') {
600
+ this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
601
+ // 整个批次失败,将这批次的所有记录标记为失败
602
+ chunk.forEach((record: any) => {
603
+ failedItems.push({
604
+ _id: record._id || 'unknown',
605
+ success: false,
606
+ error: res.msg || `Update failed with code ${res.code}`
607
+ });
608
+ });
609
+ continue;
610
+ }
594
611
 
595
- this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunk ${index + 1} completed: ${object_name}, code=${res.code}`);
596
- this.log(LoggerLevel.trace, `[object.update.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
612
+ // 处理响应中的 items
613
+ if (res.data && Array.isArray(res.data.items)) {
614
+ res.data.items.forEach((item: any) => {
615
+ if (item.success) {
616
+ successItems.push(item);
617
+ } else {
618
+ failedItems.push(item);
619
+ }
620
+ });
621
+ }
597
622
 
598
- results.push(res);
623
+ this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunk ${index + 1} completed: ${object_name}, success=${res.data?.items?.filter((i: any) => i.success).length}, failed=${res.data?.items?.filter((i: any) => !i.success).length}`);
624
+ this.log(LoggerLevel.trace, `[object.update.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
625
+ } catch (error) {
626
+ this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
627
+ // 整个批次异常,将这批次的所有记录标记为失败
628
+ chunk.forEach((record: any) => {
629
+ failedItems.push({
630
+ _id: record._id || 'unknown',
631
+ success: false,
632
+ error: error instanceof Error ? error.message : String(error)
633
+ });
634
+ });
635
+ }
599
636
  }
600
637
 
601
- return results;
638
+ const result = {
639
+ total: records.length,
640
+ success: successItems,
641
+ failed: failedItems,
642
+ successCount: successItems.length,
643
+ failedCount: failedItems.length
644
+ };
645
+
646
+ this.log(LoggerLevel.info, `[object.update.recordsWithIterator] Update completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
647
+
648
+ return result;
602
649
  }
603
650
  },
604
651