apaas-oapi-client 0.1.25 → 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
@@ -260,11 +260,11 @@ class Client {
260
260
  /**
261
261
  * 分批创建所有记录 - 支持超过 100 条数据,自动拆分
262
262
  * @description 创建多条记录到指定对象中,超过 100 条数据会自动拆分为多次请求
263
- * @param params 请求参数 { object_name, records }
263
+ * @param params 请求参数 { object_name, records, limit }
264
264
  * @returns { total, items }
265
265
  */
266
266
  recordsWithIterator: async (params) => {
267
- const { object_name, records } = params;
267
+ const { object_name, records, limit = 100 } = params;
268
268
  // 参数校验
269
269
  if (!records || !Array.isArray(records)) {
270
270
  this.log(LoggerLevel.error, '[object.create.recordsWithIterator] Invalid records parameter: must be a non-empty array');
@@ -276,7 +276,7 @@ class Client {
276
276
  }
277
277
  let results = [];
278
278
  let total = records.length;
279
- const chunkSize = 100;
279
+ const chunkSize = limit;
280
280
  let page = 0;
281
281
  const chunks = [];
282
282
  for (let i = 0; i < records.length; i += chunkSize) {
@@ -355,11 +355,12 @@ 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
- const { object_name, records } = params;
362
+ var _a, _b, _c, _d;
363
+ const { object_name, records, limit = 100 } = params;
363
364
  // 参数校验
364
365
  if (!records || !Array.isArray(records)) {
365
366
  this.log(LoggerLevel.error, '[object.update.recordsWithIterator] Invalid records parameter: must be a non-empty array');
@@ -367,30 +368,70 @@ 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
- const chunkSize = 100;
373
+ const chunkSize = limit;
373
374
  const chunks = [];
374
375
  for (let i = 0; i < records.length; i += chunkSize) {
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: {
@@ -446,7 +487,7 @@ class Client {
446
487
  * @returns 所有子请求的返回结果数组
447
488
  */
448
489
  recordsWithIterator: async (params) => {
449
- const { object_name, ids } = params;
490
+ const { object_name, ids, limit = 100 } = params;
450
491
  // 参数校验
451
492
  if (!ids || !Array.isArray(ids)) {
452
493
  this.log(LoggerLevel.error, '[object.delete.recordsWithIterator] Invalid ids parameter: must be a non-empty array');
@@ -457,7 +498,7 @@ class Client {
457
498
  return [];
458
499
  }
459
500
  const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
460
- const chunkSize = 100;
501
+ const chunkSize = limit;
461
502
  const chunks = [];
462
503
  for (let i = 0; i < ids.length; i += chunkSize) {
463
504
  chunks.push(ids.slice(i, i + chunkSize));
@@ -163,12 +163,13 @@ declare class Client {
163
163
  /**
164
164
  * 分批创建所有记录 - 支持超过 100 条数据,自动拆分
165
165
  * @description 创建多条记录到指定对象中,超过 100 条数据会自动拆分为多次请求
166
- * @param params 请求参数 { object_name, records }
166
+ * @param params 请求参数 { object_name, records, limit }
167
167
  * @returns { total, items }
168
168
  */
169
169
  recordsWithIterator: (params: {
170
170
  object_name: string;
171
171
  records: any[];
172
+ limit?: number;
172
173
  }) => Promise<{
173
174
  total: number;
174
175
  items: any[];
@@ -199,13 +200,27 @@ declare class Client {
199
200
  /**
200
201
  * 批量更新 - 支持超过 100 条数据,自动拆分
201
202
  * @description 更新指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
202
- * @param params 请求参数
203
- * @returns 所有子请求的返回结果数组
203
+ * @param params 请求参数,包含 object_name, records, limit
204
+ * @returns { total, success, failed, successCount, failedCount }
204
205
  */
205
206
  recordsWithIterator: (params: {
206
207
  object_name: string;
207
208
  records: any[];
208
- }) => Promise<any[]>;
209
+ limit?: number;
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
+ }>;
209
224
  };
210
225
  delete: {
211
226
  /**
@@ -237,6 +252,7 @@ declare class Client {
237
252
  recordsWithIterator: (params: {
238
253
  object_name: string;
239
254
  ids: string[];
255
+ limit?: number;
240
256
  }) => Promise<any[]>;
241
257
  };
242
258
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apaas-oapi-client",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "main": "dist/index.js",
5
5
  "exports": {
6
6
  ".": "./dist/index.js",
package/src/index.ts CHANGED
@@ -432,11 +432,11 @@ class Client {
432
432
  /**
433
433
  * 分批创建所有记录 - 支持超过 100 条数据,自动拆分
434
434
  * @description 创建多条记录到指定对象中,超过 100 条数据会自动拆分为多次请求
435
- * @param params 请求参数 { object_name, records }
435
+ * @param params 请求参数 { object_name, records, limit }
436
436
  * @returns { total, items }
437
437
  */
438
- recordsWithIterator: async (params: { object_name: string; records: any[] }): Promise<{ total: number; items: any[] }> => {
439
- const { object_name, records } = params;
438
+ recordsWithIterator: async (params: { object_name: string; records: any[]; limit?: number }): Promise<{ total: number; items: any[] }> => {
439
+ const { object_name, records, limit = 100 } = params;
440
440
 
441
441
  // 参数校验
442
442
  if (!records || !Array.isArray(records)) {
@@ -451,7 +451,7 @@ class Client {
451
451
 
452
452
  let results: any[] = [];
453
453
  let total = records.length;
454
- const chunkSize = 100;
454
+ const chunkSize = limit;
455
455
  let page = 0;
456
456
 
457
457
  const chunks: any[][] = [];
@@ -553,11 +553,17 @@ 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[] }): Promise<any[]> => {
560
- const { object_name, records } = params;
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
+ }> => {
566
+ const { object_name, records, limit = 100 } = params;
561
567
 
562
568
  // 参数校验
563
569
  if (!records || !Array.isArray(records)) {
@@ -567,10 +573,10 @@ 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
- const chunkSize = 100;
579
+ const chunkSize = limit;
574
580
  const chunks: any[][] = [];
575
581
  for (let i = 0; i < records.length; i += chunkSize) {
576
582
  chunks.push(records.slice(i, i + chunkSize));
@@ -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
 
@@ -667,8 +714,8 @@ class Client {
667
714
  * @param params 请求参数
668
715
  * @returns 所有子请求的返回结果数组
669
716
  */
670
- recordsWithIterator: async (params: { object_name: string; ids: string[] }): Promise<any[]> => {
671
- const { object_name, ids } = params;
717
+ recordsWithIterator: async (params: { object_name: string; ids: string[]; limit?: number }): Promise<any[]> => {
718
+ const { object_name, ids, limit = 100 } = params;
672
719
 
673
720
  // 参数校验
674
721
  if (!ids || !Array.isArray(ids)) {
@@ -683,7 +730,7 @@ class Client {
683
730
 
684
731
  const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
685
732
 
686
- const chunkSize = 100;
733
+ const chunkSize = limit;
687
734
  const chunks: string[][] = [];
688
735
  for (let i = 0; i < ids.length; i += chunkSize) {
689
736
  chunks.push(ids.slice(i, i + chunkSize));