apaas-oapi-client 0.1.27 → 0.1.30
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/README.md +7 -0
- package/dist/index.js +111 -41
- package/dist/src/index.d.ts +30 -7
- package/package.json +1 -1
- package/src/index.ts +125 -45
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -261,9 +261,10 @@ class Client {
|
|
|
261
261
|
* 分批创建所有记录 - 支持超过 100 条数据,自动拆分
|
|
262
262
|
* @description 创建多条记录到指定对象中,超过 100 条数据会自动拆分为多次请求
|
|
263
263
|
* @param params 请求参数 { object_name, records, limit }
|
|
264
|
-
* @returns { total,
|
|
264
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
265
265
|
*/
|
|
266
266
|
recordsWithIterator: async (params) => {
|
|
267
|
+
var _a, _b, _c, _d;
|
|
267
268
|
const { object_name, records, limit = 100 } = params;
|
|
268
269
|
// 参数校验
|
|
269
270
|
if (!records || !Array.isArray(records)) {
|
|
@@ -272,43 +273,72 @@ class Client {
|
|
|
272
273
|
}
|
|
273
274
|
if (records.length === 0) {
|
|
274
275
|
this.log(LoggerLevel.warn, '[object.create.recordsWithIterator] Empty records array provided, returning empty result');
|
|
275
|
-
return { total: 0,
|
|
276
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
276
277
|
}
|
|
277
|
-
let results = [];
|
|
278
|
-
let total = records.length;
|
|
279
278
|
const chunkSize = limit;
|
|
280
|
-
let page = 0;
|
|
281
279
|
const chunks = [];
|
|
282
280
|
for (let i = 0; i < records.length; i += chunkSize) {
|
|
283
281
|
chunks.push(records.slice(i, i + chunkSize));
|
|
284
282
|
}
|
|
285
283
|
this.log(LoggerLevel.debug, `[object.create.recordsWithIterator] Chunking ${records.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
284
|
+
const successItems = [];
|
|
285
|
+
const failedItems = [];
|
|
286
286
|
for (const [index, chunk] of chunks.entries()) {
|
|
287
|
-
page += 1;
|
|
288
287
|
this.log(LoggerLevel.debug, `[object.create.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
288
|
+
try {
|
|
289
|
+
const res = await functionLimiter(async () => {
|
|
290
|
+
return await this.object.create.records({
|
|
291
|
+
object_name,
|
|
292
|
+
records: chunk
|
|
293
|
+
});
|
|
294
294
|
});
|
|
295
295
|
if (res.code !== '0') {
|
|
296
|
-
this.log(LoggerLevel.error, `[object.create.recordsWithIterator]
|
|
297
|
-
//
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
296
|
+
this.log(LoggerLevel.error, `[object.create.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
|
|
297
|
+
// 整个批次失败,将这批次的所有记录标记为失败
|
|
298
|
+
chunk.forEach((record) => {
|
|
299
|
+
failedItems.push({
|
|
300
|
+
_id: record._id || 'unknown',
|
|
301
|
+
success: false,
|
|
302
|
+
error: res.msg || `Creation failed with code ${res.code}`
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
continue;
|
|
301
306
|
}
|
|
307
|
+
// 处理响应中的 items
|
|
302
308
|
if (res.data && Array.isArray(res.data.items)) {
|
|
303
|
-
|
|
309
|
+
res.data.items.forEach((item) => {
|
|
310
|
+
if (item.success !== false) {
|
|
311
|
+
successItems.push(item);
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
failedItems.push(item);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
304
317
|
}
|
|
305
|
-
this.log(LoggerLevel.info, `[object.create.recordsWithIterator] Chunk ${
|
|
306
|
-
this.log(LoggerLevel.
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
318
|
+
this.log(LoggerLevel.info, `[object.create.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 !== false).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 === false).length}`);
|
|
319
|
+
this.log(LoggerLevel.trace, `[object.create.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
this.log(LoggerLevel.error, `[object.create.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
|
|
323
|
+
// 整个批次异常,将这批次的所有记录标记为失败
|
|
324
|
+
chunk.forEach((record) => {
|
|
325
|
+
failedItems.push({
|
|
326
|
+
_id: record._id || 'unknown',
|
|
327
|
+
success: false,
|
|
328
|
+
error: error instanceof Error ? error.message : String(error)
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
}
|
|
310
332
|
}
|
|
311
|
-
|
|
333
|
+
const result = {
|
|
334
|
+
total: records.length,
|
|
335
|
+
success: successItems,
|
|
336
|
+
failed: failedItems,
|
|
337
|
+
successCount: successItems.length,
|
|
338
|
+
failedCount: failedItems.length
|
|
339
|
+
};
|
|
340
|
+
this.log(LoggerLevel.info, `[object.create.recordsWithIterator] Create completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
|
|
341
|
+
return result;
|
|
312
342
|
}
|
|
313
343
|
},
|
|
314
344
|
update: {
|
|
@@ -438,7 +468,7 @@ class Client {
|
|
|
438
468
|
/**
|
|
439
469
|
* 单条删除
|
|
440
470
|
* @description 删除指定对象下的单条记录
|
|
441
|
-
* @param params
|
|
471
|
+
* @param params 请求参数,包含 object_name 和 record_id
|
|
442
472
|
* @returns 接口返回结果
|
|
443
473
|
*/
|
|
444
474
|
record: async (params) => {
|
|
@@ -460,7 +490,7 @@ class Client {
|
|
|
460
490
|
/**
|
|
461
491
|
* 多条删除 - 最多传入 100 条
|
|
462
492
|
* @description 删除指定对象下的多条记录
|
|
463
|
-
* @param params
|
|
493
|
+
* @param params 请求参数,包含 object_name 和 ids 数组
|
|
464
494
|
* @returns 接口返回结果
|
|
465
495
|
*/
|
|
466
496
|
records: async (params) => {
|
|
@@ -483,10 +513,11 @@ class Client {
|
|
|
483
513
|
/**
|
|
484
514
|
* 批量删除
|
|
485
515
|
* @description 删除指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
486
|
-
* @param params
|
|
487
|
-
* @returns
|
|
516
|
+
* @param params 请求参数,包含 object_name, ids 数组 和 可选的 limit
|
|
517
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
488
518
|
*/
|
|
489
519
|
recordsWithIterator: async (params) => {
|
|
520
|
+
var _a, _b, _c, _d;
|
|
490
521
|
const { object_name, ids, limit = 100 } = params;
|
|
491
522
|
// 参数校验
|
|
492
523
|
if (!ids || !Array.isArray(ids)) {
|
|
@@ -495,31 +526,70 @@ class Client {
|
|
|
495
526
|
}
|
|
496
527
|
if (ids.length === 0) {
|
|
497
528
|
this.log(LoggerLevel.warn, '[object.delete.recordsWithIterator] Empty ids array provided, returning empty result');
|
|
498
|
-
return [];
|
|
529
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
499
530
|
}
|
|
500
|
-
|
|
531
|
+
`/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
|
|
501
532
|
const chunkSize = limit;
|
|
502
533
|
const chunks = [];
|
|
503
534
|
for (let i = 0; i < ids.length; i += chunkSize) {
|
|
504
535
|
chunks.push(ids.slice(i, i + chunkSize));
|
|
505
536
|
}
|
|
506
537
|
this.log(LoggerLevel.debug, `[object.delete.recordsWithIterator] Chunking ${ids.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
507
|
-
const
|
|
538
|
+
const successItems = [];
|
|
539
|
+
const failedItems = [];
|
|
508
540
|
for (const [index, chunk] of chunks.entries()) {
|
|
509
541
|
this.log(LoggerLevel.info, `[object.delete.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
510
|
-
|
|
511
|
-
await this.
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
data: { ids: chunk }
|
|
542
|
+
try {
|
|
543
|
+
const res = await this.object.delete.records({
|
|
544
|
+
object_name,
|
|
545
|
+
ids: chunk
|
|
515
546
|
});
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
547
|
+
if (res.code !== '0') {
|
|
548
|
+
this.log(LoggerLevel.error, `[object.delete.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
|
|
549
|
+
// 整个批次失败,将这批次的所有 ID 标记为失败
|
|
550
|
+
chunk.forEach((id) => {
|
|
551
|
+
failedItems.push({
|
|
552
|
+
_id: id,
|
|
553
|
+
success: false,
|
|
554
|
+
error: res.msg || `Delete failed with code ${res.code}`
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
continue;
|
|
558
|
+
}
|
|
559
|
+
// 处理响应中的 items
|
|
560
|
+
if (res.data && Array.isArray(res.data.items)) {
|
|
561
|
+
res.data.items.forEach((item) => {
|
|
562
|
+
if (item.success) {
|
|
563
|
+
successItems.push(item);
|
|
564
|
+
}
|
|
565
|
+
else {
|
|
566
|
+
failedItems.push(item);
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
this.log(LoggerLevel.debug, `[object.delete.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}`);
|
|
571
|
+
}
|
|
572
|
+
catch (error) {
|
|
573
|
+
this.log(LoggerLevel.error, `[object.delete.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
|
|
574
|
+
// 整个批次异常,将这批次的所有 ID 标记为失败
|
|
575
|
+
chunk.forEach((id) => {
|
|
576
|
+
failedItems.push({
|
|
577
|
+
_id: id,
|
|
578
|
+
success: false,
|
|
579
|
+
error: error instanceof Error ? error.message : String(error)
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
}
|
|
521
583
|
}
|
|
522
|
-
|
|
584
|
+
const result = {
|
|
585
|
+
total: ids.length,
|
|
586
|
+
success: successItems,
|
|
587
|
+
failed: failedItems,
|
|
588
|
+
successCount: successItems.length,
|
|
589
|
+
failedCount: failedItems.length
|
|
590
|
+
};
|
|
591
|
+
this.log(LoggerLevel.info, `[object.delete.recordsWithIterator] Delete completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
|
|
592
|
+
return result;
|
|
523
593
|
}
|
|
524
594
|
}
|
|
525
595
|
};
|
package/dist/src/index.d.ts
CHANGED
|
@@ -164,7 +164,7 @@ declare class Client {
|
|
|
164
164
|
* 分批创建所有记录 - 支持超过 100 条数据,自动拆分
|
|
165
165
|
* @description 创建多条记录到指定对象中,超过 100 条数据会自动拆分为多次请求
|
|
166
166
|
* @param params 请求参数 { object_name, records, limit }
|
|
167
|
-
* @returns { total,
|
|
167
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
168
168
|
*/
|
|
169
169
|
recordsWithIterator: (params: {
|
|
170
170
|
object_name: string;
|
|
@@ -172,7 +172,17 @@ declare class Client {
|
|
|
172
172
|
limit?: number;
|
|
173
173
|
}) => Promise<{
|
|
174
174
|
total: number;
|
|
175
|
-
|
|
175
|
+
success: Array<{
|
|
176
|
+
_id: string;
|
|
177
|
+
success: true;
|
|
178
|
+
}>;
|
|
179
|
+
failed: Array<{
|
|
180
|
+
_id: string;
|
|
181
|
+
success: false;
|
|
182
|
+
error?: string;
|
|
183
|
+
}>;
|
|
184
|
+
successCount: number;
|
|
185
|
+
failedCount: number;
|
|
176
186
|
}>;
|
|
177
187
|
};
|
|
178
188
|
update: {
|
|
@@ -226,7 +236,7 @@ declare class Client {
|
|
|
226
236
|
/**
|
|
227
237
|
* 单条删除
|
|
228
238
|
* @description 删除指定对象下的单条记录
|
|
229
|
-
* @param params
|
|
239
|
+
* @param params 请求参数,包含 object_name 和 record_id
|
|
230
240
|
* @returns 接口返回结果
|
|
231
241
|
*/
|
|
232
242
|
record: (params: {
|
|
@@ -236,7 +246,7 @@ declare class Client {
|
|
|
236
246
|
/**
|
|
237
247
|
* 多条删除 - 最多传入 100 条
|
|
238
248
|
* @description 删除指定对象下的多条记录
|
|
239
|
-
* @param params
|
|
249
|
+
* @param params 请求参数,包含 object_name 和 ids 数组
|
|
240
250
|
* @returns 接口返回结果
|
|
241
251
|
*/
|
|
242
252
|
records: (params: {
|
|
@@ -246,14 +256,27 @@ declare class Client {
|
|
|
246
256
|
/**
|
|
247
257
|
* 批量删除
|
|
248
258
|
* @description 删除指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
249
|
-
* @param params
|
|
250
|
-
* @returns
|
|
259
|
+
* @param params 请求参数,包含 object_name, ids 数组 和 可选的 limit
|
|
260
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
251
261
|
*/
|
|
252
262
|
recordsWithIterator: (params: {
|
|
253
263
|
object_name: string;
|
|
254
264
|
ids: string[];
|
|
255
265
|
limit?: number;
|
|
256
|
-
}) => Promise<
|
|
266
|
+
}) => Promise<{
|
|
267
|
+
total: number;
|
|
268
|
+
success: Array<{
|
|
269
|
+
_id: string;
|
|
270
|
+
success: true;
|
|
271
|
+
}>;
|
|
272
|
+
failed: Array<{
|
|
273
|
+
_id: string;
|
|
274
|
+
success: false;
|
|
275
|
+
error?: string;
|
|
276
|
+
}>;
|
|
277
|
+
successCount: number;
|
|
278
|
+
failedCount: number;
|
|
279
|
+
}>;
|
|
257
280
|
};
|
|
258
281
|
};
|
|
259
282
|
/**
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -433,9 +433,15 @@ class Client {
|
|
|
433
433
|
* 分批创建所有记录 - 支持超过 100 条数据,自动拆分
|
|
434
434
|
* @description 创建多条记录到指定对象中,超过 100 条数据会自动拆分为多次请求
|
|
435
435
|
* @param params 请求参数 { object_name, records, limit }
|
|
436
|
-
* @returns { total,
|
|
436
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
437
437
|
*/
|
|
438
|
-
recordsWithIterator: async (params: { object_name: string; records: any[]; limit?: number }): Promise<{
|
|
438
|
+
recordsWithIterator: async (params: { object_name: string; records: any[]; limit?: number }): Promise<{
|
|
439
|
+
total: number;
|
|
440
|
+
success: Array<{ _id: string; success: true }>;
|
|
441
|
+
failed: Array<{ _id: string; success: false; error?: string }>;
|
|
442
|
+
successCount: number;
|
|
443
|
+
failedCount: number;
|
|
444
|
+
}> => {
|
|
439
445
|
const { object_name, records, limit = 100 } = params;
|
|
440
446
|
|
|
441
447
|
// 参数校验
|
|
@@ -446,14 +452,10 @@ class Client {
|
|
|
446
452
|
|
|
447
453
|
if (records.length === 0) {
|
|
448
454
|
this.log(LoggerLevel.warn, '[object.create.recordsWithIterator] Empty records array provided, returning empty result');
|
|
449
|
-
return { total: 0,
|
|
455
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
450
456
|
}
|
|
451
457
|
|
|
452
|
-
let results: any[] = [];
|
|
453
|
-
let total = records.length;
|
|
454
458
|
const chunkSize = limit;
|
|
455
|
-
let page = 0;
|
|
456
|
-
|
|
457
459
|
const chunks: any[][] = [];
|
|
458
460
|
for (let i = 0; i < records.length; i += chunkSize) {
|
|
459
461
|
chunks.push(records.slice(i, i + chunkSize));
|
|
@@ -461,38 +463,70 @@ class Client {
|
|
|
461
463
|
|
|
462
464
|
this.log(LoggerLevel.debug, `[object.create.recordsWithIterator] Chunking ${records.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
463
465
|
|
|
464
|
-
|
|
465
|
-
|
|
466
|
+
const successItems: Array<{ _id: string; success: true }> = [];
|
|
467
|
+
const failedItems: Array<{ _id: string; success: false; error?: string }> = [];
|
|
466
468
|
|
|
469
|
+
for (const [index, chunk] of chunks.entries()) {
|
|
467
470
|
this.log(LoggerLevel.debug, `[object.create.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
468
471
|
|
|
469
|
-
|
|
470
|
-
const res = await
|
|
471
|
-
|
|
472
|
-
|
|
472
|
+
try {
|
|
473
|
+
const res = await functionLimiter(async () => {
|
|
474
|
+
return await this.object.create.records({
|
|
475
|
+
object_name,
|
|
476
|
+
records: chunk
|
|
477
|
+
});
|
|
473
478
|
});
|
|
474
479
|
|
|
475
480
|
if (res.code !== '0') {
|
|
476
|
-
this.log(LoggerLevel.error, `[object.create.recordsWithIterator]
|
|
477
|
-
//
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
+
this.log(LoggerLevel.error, `[object.create.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
|
|
482
|
+
// 整个批次失败,将这批次的所有记录标记为失败
|
|
483
|
+
chunk.forEach((record: any) => {
|
|
484
|
+
failedItems.push({
|
|
485
|
+
_id: record._id || 'unknown',
|
|
486
|
+
success: false,
|
|
487
|
+
error: res.msg || `Creation failed with code ${res.code}`
|
|
488
|
+
});
|
|
489
|
+
});
|
|
490
|
+
continue;
|
|
481
491
|
}
|
|
482
492
|
|
|
493
|
+
// 处理响应中的 items
|
|
483
494
|
if (res.data && Array.isArray(res.data.items)) {
|
|
484
|
-
|
|
495
|
+
res.data.items.forEach((item: any) => {
|
|
496
|
+
if (item.success !== false) {
|
|
497
|
+
successItems.push(item);
|
|
498
|
+
} else {
|
|
499
|
+
failedItems.push(item);
|
|
500
|
+
}
|
|
501
|
+
});
|
|
485
502
|
}
|
|
486
503
|
|
|
487
|
-
this.log(LoggerLevel.info, `[object.create.recordsWithIterator] Chunk ${
|
|
488
|
-
this.log(LoggerLevel.
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
504
|
+
this.log(LoggerLevel.info, `[object.create.recordsWithIterator] Chunk ${index + 1} completed: ${object_name}, success=${res.data?.items?.filter((i: any) => i.success !== false).length}, failed=${res.data?.items?.filter((i: any) => i.success === false).length}`);
|
|
505
|
+
this.log(LoggerLevel.trace, `[object.create.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
|
|
506
|
+
} catch (error) {
|
|
507
|
+
this.log(LoggerLevel.error, `[object.create.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
|
|
508
|
+
// 整个批次异常,将这批次的所有记录标记为失败
|
|
509
|
+
chunk.forEach((record: any) => {
|
|
510
|
+
failedItems.push({
|
|
511
|
+
_id: record._id || 'unknown',
|
|
512
|
+
success: false,
|
|
513
|
+
error: error instanceof Error ? error.message : String(error)
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
}
|
|
493
517
|
}
|
|
494
518
|
|
|
495
|
-
|
|
519
|
+
const result = {
|
|
520
|
+
total: records.length,
|
|
521
|
+
success: successItems,
|
|
522
|
+
failed: failedItems,
|
|
523
|
+
successCount: successItems.length,
|
|
524
|
+
failedCount: failedItems.length
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
this.log(LoggerLevel.info, `[object.create.recordsWithIterator] Create completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
|
|
528
|
+
|
|
529
|
+
return result;
|
|
496
530
|
}
|
|
497
531
|
},
|
|
498
532
|
|
|
@@ -653,7 +687,7 @@ class Client {
|
|
|
653
687
|
/**
|
|
654
688
|
* 单条删除
|
|
655
689
|
* @description 删除指定对象下的单条记录
|
|
656
|
-
* @param params
|
|
690
|
+
* @param params 请求参数,包含 object_name 和 record_id
|
|
657
691
|
* @returns 接口返回结果
|
|
658
692
|
*/
|
|
659
693
|
record: async (params: { object_name: string; record_id: string }): Promise<any> => {
|
|
@@ -681,7 +715,7 @@ class Client {
|
|
|
681
715
|
/**
|
|
682
716
|
* 多条删除 - 最多传入 100 条
|
|
683
717
|
* @description 删除指定对象下的多条记录
|
|
684
|
-
* @param params
|
|
718
|
+
* @param params 请求参数,包含 object_name 和 ids 数组
|
|
685
719
|
* @returns 接口返回结果
|
|
686
720
|
*/
|
|
687
721
|
records: async (params: { object_name: string; ids: string[] }): Promise<any> => {
|
|
@@ -711,10 +745,16 @@ class Client {
|
|
|
711
745
|
/**
|
|
712
746
|
* 批量删除
|
|
713
747
|
* @description 删除指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
714
|
-
* @param params
|
|
715
|
-
* @returns
|
|
748
|
+
* @param params 请求参数,包含 object_name, ids 数组 和 可选的 limit
|
|
749
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
716
750
|
*/
|
|
717
|
-
recordsWithIterator: async (params: { object_name: string; ids: string[]; limit?: number }): Promise<
|
|
751
|
+
recordsWithIterator: async (params: { object_name: string; ids: string[]; limit?: number }): Promise<{
|
|
752
|
+
total: number;
|
|
753
|
+
success: Array<{ _id: string; success: true }>;
|
|
754
|
+
failed: Array<{ _id: string; success: false; error?: string }>;
|
|
755
|
+
successCount: number;
|
|
756
|
+
failedCount: number;
|
|
757
|
+
}> => {
|
|
718
758
|
const { object_name, ids, limit = 100 } = params;
|
|
719
759
|
|
|
720
760
|
// 参数校验
|
|
@@ -725,7 +765,7 @@ class Client {
|
|
|
725
765
|
|
|
726
766
|
if (ids.length === 0) {
|
|
727
767
|
this.log(LoggerLevel.warn, '[object.delete.recordsWithIterator] Empty ids array provided, returning empty result');
|
|
728
|
-
return [];
|
|
768
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
729
769
|
}
|
|
730
770
|
|
|
731
771
|
const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
|
|
@@ -738,27 +778,67 @@ class Client {
|
|
|
738
778
|
|
|
739
779
|
this.log(LoggerLevel.debug, `[object.delete.recordsWithIterator] Chunking ${ids.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
740
780
|
|
|
741
|
-
const
|
|
781
|
+
const successItems: Array<{ _id: string; success: true }> = [];
|
|
782
|
+
const failedItems: Array<{ _id: string; success: false; error?: string }> = [];
|
|
783
|
+
|
|
742
784
|
for (const [index, chunk] of chunks.entries()) {
|
|
743
785
|
this.log(LoggerLevel.info, `[object.delete.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
744
786
|
|
|
745
|
-
|
|
746
|
-
await this.
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
headers: { Authorization: `${this.accessToken}` },
|
|
750
|
-
data: { ids: chunk }
|
|
787
|
+
try {
|
|
788
|
+
const res = await this.object.delete.records({
|
|
789
|
+
object_name,
|
|
790
|
+
ids: chunk
|
|
751
791
|
});
|
|
752
792
|
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
793
|
+
if (res.code !== '0') {
|
|
794
|
+
this.log(LoggerLevel.error, `[object.delete.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
|
|
795
|
+
// 整个批次失败,将这批次的所有 ID 标记为失败
|
|
796
|
+
chunk.forEach((id: string) => {
|
|
797
|
+
failedItems.push({
|
|
798
|
+
_id: id,
|
|
799
|
+
success: false,
|
|
800
|
+
error: res.msg || `Delete failed with code ${res.code}`
|
|
801
|
+
});
|
|
802
|
+
});
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// 处理响应中的 items
|
|
807
|
+
if (res.data && Array.isArray(res.data.items)) {
|
|
808
|
+
res.data.items.forEach((item: any) => {
|
|
809
|
+
if (item.success) {
|
|
810
|
+
successItems.push(item);
|
|
811
|
+
} else {
|
|
812
|
+
failedItems.push(item);
|
|
813
|
+
}
|
|
814
|
+
});
|
|
815
|
+
}
|
|
757
816
|
|
|
758
|
-
|
|
817
|
+
this.log(LoggerLevel.debug, `[object.delete.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}`);
|
|
818
|
+
} catch (error) {
|
|
819
|
+
this.log(LoggerLevel.error, `[object.delete.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
|
|
820
|
+
// 整个批次异常,将这批次的所有 ID 标记为失败
|
|
821
|
+
chunk.forEach((id: string) => {
|
|
822
|
+
failedItems.push({
|
|
823
|
+
_id: id,
|
|
824
|
+
success: false,
|
|
825
|
+
error: error instanceof Error ? error.message : String(error)
|
|
826
|
+
});
|
|
827
|
+
});
|
|
828
|
+
}
|
|
759
829
|
}
|
|
760
830
|
|
|
761
|
-
|
|
831
|
+
const result = {
|
|
832
|
+
total: ids.length,
|
|
833
|
+
success: successItems,
|
|
834
|
+
failed: failedItems,
|
|
835
|
+
successCount: successItems.length,
|
|
836
|
+
failedCount: failedItems.length
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
this.log(LoggerLevel.info, `[object.delete.recordsWithIterator] Delete completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
|
|
840
|
+
|
|
841
|
+
return result;
|
|
762
842
|
}
|
|
763
843
|
}
|
|
764
844
|
};
|