apaas-oapi-client 0.1.26 → 0.1.29
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 +167 -56
- package/dist/src/index.d.ts +46 -10
- package/package.json +1 -1
- package/src/index.ts +189 -62
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: {
|
|
@@ -355,10 +385,11 @@ class Client {
|
|
|
355
385
|
/**
|
|
356
386
|
* 批量更新 - 支持超过 100 条数据,自动拆分
|
|
357
387
|
* @description 更新指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
358
|
-
* @param params
|
|
359
|
-
* @returns
|
|
388
|
+
* @param params 请求参数,包含 object_name, records, limit
|
|
389
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
360
390
|
*/
|
|
361
391
|
recordsWithIterator: async (params) => {
|
|
392
|
+
var _a, _b, _c, _d;
|
|
362
393
|
const { object_name, records, limit = 100 } = params;
|
|
363
394
|
// 参数校验
|
|
364
395
|
if (!records || !Array.isArray(records)) {
|
|
@@ -367,7 +398,7 @@ class Client {
|
|
|
367
398
|
}
|
|
368
399
|
if (records.length === 0) {
|
|
369
400
|
this.log(LoggerLevel.warn, '[object.update.recordsWithIterator] Empty records array provided, returning empty result');
|
|
370
|
-
return [];
|
|
401
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
371
402
|
}
|
|
372
403
|
const chunkSize = limit;
|
|
373
404
|
const chunks = [];
|
|
@@ -375,29 +406,69 @@ class Client {
|
|
|
375
406
|
chunks.push(records.slice(i, i + chunkSize));
|
|
376
407
|
}
|
|
377
408
|
this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunking ${records.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
378
|
-
const
|
|
409
|
+
const successItems = [];
|
|
410
|
+
const failedItems = [];
|
|
379
411
|
for (const [index, chunk] of chunks.entries()) {
|
|
380
412
|
this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
413
|
+
try {
|
|
414
|
+
const res = await this.object.update.records({
|
|
415
|
+
object_name,
|
|
416
|
+
records: chunk
|
|
417
|
+
});
|
|
418
|
+
if (res.code !== '0') {
|
|
419
|
+
this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
|
|
420
|
+
// 整个批次失败,将这批次的所有记录标记为失败
|
|
421
|
+
chunk.forEach((record) => {
|
|
422
|
+
failedItems.push({
|
|
423
|
+
_id: record._id || 'unknown',
|
|
424
|
+
success: false,
|
|
425
|
+
error: res.msg || `Update failed with code ${res.code}`
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
// 处理响应中的 items
|
|
431
|
+
if (res.data && Array.isArray(res.data.items)) {
|
|
432
|
+
res.data.items.forEach((item) => {
|
|
433
|
+
if (item.success) {
|
|
434
|
+
successItems.push(item);
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
failedItems.push(item);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
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}`);
|
|
442
|
+
this.log(LoggerLevel.trace, `[object.update.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
|
|
443
|
+
}
|
|
444
|
+
catch (error) {
|
|
445
|
+
this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
|
|
446
|
+
// 整个批次异常,将这批次的所有记录标记为失败
|
|
447
|
+
chunk.forEach((record) => {
|
|
448
|
+
failedItems.push({
|
|
449
|
+
_id: record._id || 'unknown',
|
|
450
|
+
success: false,
|
|
451
|
+
error: error instanceof Error ? error.message : String(error)
|
|
452
|
+
});
|
|
453
|
+
});
|
|
388
454
|
}
|
|
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
455
|
}
|
|
393
|
-
|
|
456
|
+
const result = {
|
|
457
|
+
total: records.length,
|
|
458
|
+
success: successItems,
|
|
459
|
+
failed: failedItems,
|
|
460
|
+
successCount: successItems.length,
|
|
461
|
+
failedCount: failedItems.length
|
|
462
|
+
};
|
|
463
|
+
this.log(LoggerLevel.info, `[object.update.recordsWithIterator] Update completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
|
|
464
|
+
return result;
|
|
394
465
|
}
|
|
395
466
|
},
|
|
396
467
|
delete: {
|
|
397
468
|
/**
|
|
398
469
|
* 单条删除
|
|
399
470
|
* @description 删除指定对象下的单条记录
|
|
400
|
-
* @param params
|
|
471
|
+
* @param params 请求参数,包含 object_name 和 record_id
|
|
401
472
|
* @returns 接口返回结果
|
|
402
473
|
*/
|
|
403
474
|
record: async (params) => {
|
|
@@ -419,7 +490,7 @@ class Client {
|
|
|
419
490
|
/**
|
|
420
491
|
* 多条删除 - 最多传入 100 条
|
|
421
492
|
* @description 删除指定对象下的多条记录
|
|
422
|
-
* @param params
|
|
493
|
+
* @param params 请求参数,包含 object_name 和 ids 数组
|
|
423
494
|
* @returns 接口返回结果
|
|
424
495
|
*/
|
|
425
496
|
records: async (params) => {
|
|
@@ -442,10 +513,11 @@ class Client {
|
|
|
442
513
|
/**
|
|
443
514
|
* 批量删除
|
|
444
515
|
* @description 删除指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
445
|
-
* @param params
|
|
446
|
-
* @returns
|
|
516
|
+
* @param params 请求参数,包含 object_name, ids 数组 和 可选的 limit
|
|
517
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
447
518
|
*/
|
|
448
519
|
recordsWithIterator: async (params) => {
|
|
520
|
+
var _a, _b, _c, _d;
|
|
449
521
|
const { object_name, ids, limit = 100 } = params;
|
|
450
522
|
// 参数校验
|
|
451
523
|
if (!ids || !Array.isArray(ids)) {
|
|
@@ -454,31 +526,70 @@ class Client {
|
|
|
454
526
|
}
|
|
455
527
|
if (ids.length === 0) {
|
|
456
528
|
this.log(LoggerLevel.warn, '[object.delete.recordsWithIterator] Empty ids array provided, returning empty result');
|
|
457
|
-
return [];
|
|
529
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
458
530
|
}
|
|
459
|
-
|
|
531
|
+
`/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
|
|
460
532
|
const chunkSize = limit;
|
|
461
533
|
const chunks = [];
|
|
462
534
|
for (let i = 0; i < ids.length; i += chunkSize) {
|
|
463
535
|
chunks.push(ids.slice(i, i + chunkSize));
|
|
464
536
|
}
|
|
465
537
|
this.log(LoggerLevel.debug, `[object.delete.recordsWithIterator] Chunking ${ids.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
466
|
-
const
|
|
538
|
+
const successItems = [];
|
|
539
|
+
const failedItems = [];
|
|
467
540
|
for (const [index, chunk] of chunks.entries()) {
|
|
468
541
|
this.log(LoggerLevel.info, `[object.delete.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
469
|
-
|
|
470
|
-
await this.
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
data: { ids: chunk }
|
|
542
|
+
try {
|
|
543
|
+
const res = await this.object.delete.records({
|
|
544
|
+
object_name,
|
|
545
|
+
ids: chunk
|
|
474
546
|
});
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
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
|
+
}
|
|
480
583
|
}
|
|
481
|
-
|
|
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;
|
|
482
593
|
}
|
|
483
594
|
}
|
|
484
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: {
|
|
@@ -200,20 +210,33 @@ declare class Client {
|
|
|
200
210
|
/**
|
|
201
211
|
* 批量更新 - 支持超过 100 条数据,自动拆分
|
|
202
212
|
* @description 更新指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
203
|
-
* @param params
|
|
204
|
-
* @returns
|
|
213
|
+
* @param params 请求参数,包含 object_name, records, limit
|
|
214
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
205
215
|
*/
|
|
206
216
|
recordsWithIterator: (params: {
|
|
207
217
|
object_name: string;
|
|
208
218
|
records: any[];
|
|
209
219
|
limit?: number;
|
|
210
|
-
}) => Promise<
|
|
220
|
+
}) => Promise<{
|
|
221
|
+
total: number;
|
|
222
|
+
success: Array<{
|
|
223
|
+
_id: string;
|
|
224
|
+
success: true;
|
|
225
|
+
}>;
|
|
226
|
+
failed: Array<{
|
|
227
|
+
_id: string;
|
|
228
|
+
success: false;
|
|
229
|
+
error?: string;
|
|
230
|
+
}>;
|
|
231
|
+
successCount: number;
|
|
232
|
+
failedCount: number;
|
|
233
|
+
}>;
|
|
211
234
|
};
|
|
212
235
|
delete: {
|
|
213
236
|
/**
|
|
214
237
|
* 单条删除
|
|
215
238
|
* @description 删除指定对象下的单条记录
|
|
216
|
-
* @param params
|
|
239
|
+
* @param params 请求参数,包含 object_name 和 record_id
|
|
217
240
|
* @returns 接口返回结果
|
|
218
241
|
*/
|
|
219
242
|
record: (params: {
|
|
@@ -223,7 +246,7 @@ declare class Client {
|
|
|
223
246
|
/**
|
|
224
247
|
* 多条删除 - 最多传入 100 条
|
|
225
248
|
* @description 删除指定对象下的多条记录
|
|
226
|
-
* @param params
|
|
249
|
+
* @param params 请求参数,包含 object_name 和 ids 数组
|
|
227
250
|
* @returns 接口返回结果
|
|
228
251
|
*/
|
|
229
252
|
records: (params: {
|
|
@@ -233,14 +256,27 @@ declare class Client {
|
|
|
233
256
|
/**
|
|
234
257
|
* 批量删除
|
|
235
258
|
* @description 删除指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
236
|
-
* @param params
|
|
237
|
-
* @returns
|
|
259
|
+
* @param params 请求参数,包含 object_name, ids 数组 和 可选的 limit
|
|
260
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
238
261
|
*/
|
|
239
262
|
recordsWithIterator: (params: {
|
|
240
263
|
object_name: string;
|
|
241
264
|
ids: string[];
|
|
242
265
|
limit?: number;
|
|
243
|
-
}) => 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
|
+
}>;
|
|
244
280
|
};
|
|
245
281
|
};
|
|
246
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
|
|
|
@@ -553,10 +587,16 @@ class Client {
|
|
|
553
587
|
/**
|
|
554
588
|
* 批量更新 - 支持超过 100 条数据,自动拆分
|
|
555
589
|
* @description 更新指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
556
|
-
* @param params
|
|
557
|
-
* @returns
|
|
590
|
+
* @param params 请求参数,包含 object_name, records, limit
|
|
591
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
558
592
|
*/
|
|
559
|
-
recordsWithIterator: async (params: { object_name: string; records: any[]; limit?: number }): Promise<
|
|
593
|
+
recordsWithIterator: async (params: { object_name: string; records: any[]; limit?: number }): Promise<{
|
|
594
|
+
total: number;
|
|
595
|
+
success: Array<{ _id: string; success: true }>;
|
|
596
|
+
failed: Array<{ _id: string; success: false; error?: string }>;
|
|
597
|
+
successCount: number;
|
|
598
|
+
failedCount: number;
|
|
599
|
+
}> => {
|
|
560
600
|
const { object_name, records, limit = 100 } = params;
|
|
561
601
|
|
|
562
602
|
// 参数校验
|
|
@@ -567,7 +607,7 @@ class Client {
|
|
|
567
607
|
|
|
568
608
|
if (records.length === 0) {
|
|
569
609
|
this.log(LoggerLevel.warn, '[object.update.recordsWithIterator] Empty records array provided, returning empty result');
|
|
570
|
-
return [];
|
|
610
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
571
611
|
}
|
|
572
612
|
|
|
573
613
|
const chunkSize = limit;
|
|
@@ -578,27 +618,68 @@ class Client {
|
|
|
578
618
|
|
|
579
619
|
this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Chunking ${records.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
580
620
|
|
|
581
|
-
const
|
|
621
|
+
const successItems: Array<{ _id: string; success: true }> = [];
|
|
622
|
+
const failedItems: Array<{ _id: string; success: false; error?: string }> = [];
|
|
623
|
+
|
|
582
624
|
for (const [index, chunk] of chunks.entries()) {
|
|
583
625
|
this.log(LoggerLevel.debug, `[object.update.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
584
626
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
627
|
+
try {
|
|
628
|
+
const res = await this.object.update.records({
|
|
629
|
+
object_name,
|
|
630
|
+
records: chunk
|
|
631
|
+
});
|
|
589
632
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
633
|
+
if (res.code !== '0') {
|
|
634
|
+
this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} failed: code=${res.code}, msg=${res.msg}`);
|
|
635
|
+
// 整个批次失败,将这批次的所有记录标记为失败
|
|
636
|
+
chunk.forEach((record: any) => {
|
|
637
|
+
failedItems.push({
|
|
638
|
+
_id: record._id || 'unknown',
|
|
639
|
+
success: false,
|
|
640
|
+
error: res.msg || `Update failed with code ${res.code}`
|
|
641
|
+
});
|
|
642
|
+
});
|
|
643
|
+
continue;
|
|
644
|
+
}
|
|
594
645
|
|
|
595
|
-
|
|
596
|
-
|
|
646
|
+
// 处理响应中的 items
|
|
647
|
+
if (res.data && Array.isArray(res.data.items)) {
|
|
648
|
+
res.data.items.forEach((item: any) => {
|
|
649
|
+
if (item.success) {
|
|
650
|
+
successItems.push(item);
|
|
651
|
+
} else {
|
|
652
|
+
failedItems.push(item);
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
}
|
|
597
656
|
|
|
598
|
-
|
|
657
|
+
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}`);
|
|
658
|
+
this.log(LoggerLevel.trace, `[object.update.recordsWithIterator] Chunk ${index + 1} response: ${JSON.stringify(res)}`);
|
|
659
|
+
} catch (error) {
|
|
660
|
+
this.log(LoggerLevel.error, `[object.update.recordsWithIterator] Chunk ${index + 1} threw error: ${error}`);
|
|
661
|
+
// 整个批次异常,将这批次的所有记录标记为失败
|
|
662
|
+
chunk.forEach((record: any) => {
|
|
663
|
+
failedItems.push({
|
|
664
|
+
_id: record._id || 'unknown',
|
|
665
|
+
success: false,
|
|
666
|
+
error: error instanceof Error ? error.message : String(error)
|
|
667
|
+
});
|
|
668
|
+
});
|
|
669
|
+
}
|
|
599
670
|
}
|
|
600
671
|
|
|
601
|
-
|
|
672
|
+
const result = {
|
|
673
|
+
total: records.length,
|
|
674
|
+
success: successItems,
|
|
675
|
+
failed: failedItems,
|
|
676
|
+
successCount: successItems.length,
|
|
677
|
+
failedCount: failedItems.length
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
this.log(LoggerLevel.info, `[object.update.recordsWithIterator] Update completed: total=${result.total}, success=${result.successCount}, failed=${result.failedCount}`);
|
|
681
|
+
|
|
682
|
+
return result;
|
|
602
683
|
}
|
|
603
684
|
},
|
|
604
685
|
|
|
@@ -606,7 +687,7 @@ class Client {
|
|
|
606
687
|
/**
|
|
607
688
|
* 单条删除
|
|
608
689
|
* @description 删除指定对象下的单条记录
|
|
609
|
-
* @param params
|
|
690
|
+
* @param params 请求参数,包含 object_name 和 record_id
|
|
610
691
|
* @returns 接口返回结果
|
|
611
692
|
*/
|
|
612
693
|
record: async (params: { object_name: string; record_id: string }): Promise<any> => {
|
|
@@ -634,7 +715,7 @@ class Client {
|
|
|
634
715
|
/**
|
|
635
716
|
* 多条删除 - 最多传入 100 条
|
|
636
717
|
* @description 删除指定对象下的多条记录
|
|
637
|
-
* @param params
|
|
718
|
+
* @param params 请求参数,包含 object_name 和 ids 数组
|
|
638
719
|
* @returns 接口返回结果
|
|
639
720
|
*/
|
|
640
721
|
records: async (params: { object_name: string; ids: string[] }): Promise<any> => {
|
|
@@ -664,10 +745,16 @@ class Client {
|
|
|
664
745
|
/**
|
|
665
746
|
* 批量删除
|
|
666
747
|
* @description 删除指定对象下的多条记录,超过 100 条数据会自动拆分为多次请求
|
|
667
|
-
* @param params
|
|
668
|
-
* @returns
|
|
748
|
+
* @param params 请求参数,包含 object_name, ids 数组 和 可选的 limit
|
|
749
|
+
* @returns { total, success, failed, successCount, failedCount }
|
|
669
750
|
*/
|
|
670
|
-
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
|
+
}> => {
|
|
671
758
|
const { object_name, ids, limit = 100 } = params;
|
|
672
759
|
|
|
673
760
|
// 参数校验
|
|
@@ -678,7 +765,7 @@ class Client {
|
|
|
678
765
|
|
|
679
766
|
if (ids.length === 0) {
|
|
680
767
|
this.log(LoggerLevel.warn, '[object.delete.recordsWithIterator] Empty ids array provided, returning empty result');
|
|
681
|
-
return [];
|
|
768
|
+
return { total: 0, success: [], failed: [], successCount: 0, failedCount: 0 };
|
|
682
769
|
}
|
|
683
770
|
|
|
684
771
|
const url = `/v1/data/namespaces/${this.namespace}/objects/${object_name}/records_batch`;
|
|
@@ -691,27 +778,67 @@ class Client {
|
|
|
691
778
|
|
|
692
779
|
this.log(LoggerLevel.debug, `[object.delete.recordsWithIterator] Chunking ${ids.length} records into ${chunks.length} groups of ${chunkSize}`);
|
|
693
780
|
|
|
694
|
-
const
|
|
781
|
+
const successItems: Array<{ _id: string; success: true }> = [];
|
|
782
|
+
const failedItems: Array<{ _id: string; success: false; error?: string }> = [];
|
|
783
|
+
|
|
695
784
|
for (const [index, chunk] of chunks.entries()) {
|
|
696
785
|
this.log(LoggerLevel.info, `[object.delete.recordsWithIterator] Processing chunk ${index + 1}/${chunks.length}: ${chunk.length} records`);
|
|
697
786
|
|
|
698
|
-
|
|
699
|
-
await this.
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
headers: { Authorization: `${this.accessToken}` },
|
|
703
|
-
data: { ids: chunk }
|
|
787
|
+
try {
|
|
788
|
+
const res = await this.object.delete.records({
|
|
789
|
+
object_name,
|
|
790
|
+
ids: chunk
|
|
704
791
|
});
|
|
705
792
|
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
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
|
+
}
|
|
710
805
|
|
|
711
|
-
|
|
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
|
+
}
|
|
816
|
+
|
|
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
|
+
}
|
|
712
829
|
}
|
|
713
830
|
|
|
714
|
-
|
|
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;
|
|
715
842
|
}
|
|
716
843
|
}
|
|
717
844
|
};
|