triangle-utils 1.4.106 → 1.4.107

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.
@@ -58,7 +58,9 @@ export declare class UtilsDynamoDB {
58
58
  remove(table_name: string, key: Record<string, any>, attributes: string[]): Promise<void>;
59
59
  create(table_name: string, key: Record<string, any>, attributes?: Record<string, any>): Promise<void>;
60
60
  delete(table_name: string, key: Record<string, any>): Promise<void>;
61
- batch_put(table_name: string, items: Record<string, any>[]): Promise<void>;
61
+ batch_put(table_name: string, items: Record<string, any>[], options?: {
62
+ is_verbose?: boolean;
63
+ }): Promise<void>;
62
64
  batch_delete(table_name: string, keys: Record<string, any>[]): Promise<void>;
63
65
  duplicate_attribute(table_name: string, attribute_name: string, new_attribute_name: string): Promise<void>;
64
66
  remove_attribute(table_name: string, attribute_name: string): Promise<undefined>;
@@ -1,4 +1,6 @@
1
+ import { ThrottlingException } from "@aws-sdk/client-bedrock-runtime";
1
2
  import { DynamoDB } from "@aws-sdk/client-dynamodb";
3
+ import { UtilsMisc } from "./UtilsMisc.js";
2
4
  function convert_output(dynamodb_output) {
3
5
  if (dynamodb_output.S !== undefined) {
4
6
  return dynamodb_output.S;
@@ -498,7 +500,8 @@ export class UtilsDynamoDB {
498
500
  Key: converted_key
499
501
  });
500
502
  }
501
- async batch_put(table_name, items) {
503
+ async batch_put(table_name, items, options = {}) {
504
+ const is_verbose = options.is_verbose !== undefined ? options.is_verbose : false;
502
505
  const dynamodb_items = items.map(item => convert_input(item)?.M)
503
506
  .filter(dynamodb_item => dynamodb_item !== undefined);
504
507
  for (let i = 0; i < dynamodb_items.length; i += 25) {
@@ -508,12 +511,24 @@ export class UtilsDynamoDB {
508
511
  }
509
512
  }));
510
513
  while (next_dynamodb_requests.length > 0) {
511
- const response = await this.dynamodb.batchWriteItem({
512
- RequestItems: {
513
- [table_name]: next_dynamodb_requests
514
+ try {
515
+ const response = await this.dynamodb.batchWriteItem({
516
+ RequestItems: {
517
+ [table_name]: next_dynamodb_requests
518
+ }
519
+ });
520
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
521
+ }
522
+ catch (error) {
523
+ if (error instanceof ThrottlingException) {
524
+ if (is_verbose) {
525
+ console.log("Throttled.");
526
+ }
527
+ await UtilsMisc.wait(1000);
528
+ continue;
514
529
  }
515
- });
516
- next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
530
+ throw error;
531
+ }
517
532
  }
518
533
  }
519
534
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.106",
3
+ "version": "1.4.107",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -1,4 +1,6 @@
1
+ import { ThrottlingException } from "@aws-sdk/client-bedrock-runtime"
1
2
  import { AttributeValue, DynamoDB, QueryCommandInput, QueryCommandOutput, ScanCommandInput, ScanCommandOutput, UpdateItemCommandInput, WriteRequest } from "@aws-sdk/client-dynamodb"
3
+ import { UtilsMisc } from "./UtilsMisc"
2
4
 
3
5
  function convert_output(dynamodb_output : AttributeValue) : string | boolean | number | any[] | Set<any> | Record<string, any> | undefined {
4
6
  if (dynamodb_output.S !== undefined) {
@@ -615,8 +617,12 @@ export class UtilsDynamoDB {
615
617
 
616
618
  async batch_put(
617
619
  table_name : string,
618
- items : Record<string, any>[]
620
+ items : Record<string, any>[],
621
+ options : {
622
+ is_verbose? : boolean
623
+ } = {}
619
624
  ) : Promise<void> {
625
+ const is_verbose = options.is_verbose !== undefined ? options.is_verbose : false
620
626
  const dynamodb_items = items.map(item => convert_input(item)?.M)
621
627
  .filter(dynamodb_item => dynamodb_item !== undefined)
622
628
 
@@ -627,12 +633,23 @@ export class UtilsDynamoDB {
627
633
  }
628
634
  }))
629
635
  while (next_dynamodb_requests.length > 0) {
630
- const response = await this.dynamodb.batchWriteItem({
631
- RequestItems : {
632
- [table_name] : next_dynamodb_requests
636
+ try {
637
+ const response = await this.dynamodb.batchWriteItem({
638
+ RequestItems : {
639
+ [table_name] : next_dynamodb_requests
640
+ }
641
+ })
642
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
643
+ } catch (error : any) {
644
+ if (error instanceof ThrottlingException) {
645
+ if (is_verbose) {
646
+ console.log("Throttled.")
647
+ }
648
+ await UtilsMisc.wait(1000)
649
+ continue
633
650
  }
634
- })
635
- next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
651
+ throw error
652
+ }
636
653
  }
637
654
  }
638
655
  }