triangle-utils 1.4.105 → 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,30 +500,43 @@ 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
- for (let i = 0; i < dynamodb_items.length; i += 100) {
505
- let next_dynamodb_requests = dynamodb_items.slice(i, i + 100).map(next_dynamodb_item => ({
507
+ for (let i = 0; i < dynamodb_items.length; i += 25) {
508
+ let next_dynamodb_requests = dynamodb_items.slice(i, i + 25).map(next_dynamodb_item => ({
506
509
  PutRequest: {
507
510
  Item: next_dynamodb_item
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
  }
520
535
  async batch_delete(table_name, keys) {
521
536
  const dynamodb_keys = keys.map(key => convert_input(key)?.M)
522
537
  .filter(dynamodb_key => dynamodb_key !== undefined);
523
- for (let i = 0; i < dynamodb_keys.length; i += 100) {
524
- let next_dynamodb_requests = dynamodb_keys.slice(i, i + 100).map(next_dynamodb_key => ({
538
+ for (let i = 0; i < dynamodb_keys.length; i += 25) {
539
+ let next_dynamodb_requests = dynamodb_keys.slice(i, i + 25).map(next_dynamodb_key => ({
525
540
  DeleteRequest: {
526
541
  Key: next_dynamodb_key
527
542
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.105",
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,24 +617,39 @@ 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
 
623
- for (let i = 0; i < dynamodb_items.length; i += 100) {
624
- let next_dynamodb_requests : WriteRequest[] = dynamodb_items.slice(i, i + 100).map(next_dynamodb_item => ({
629
+ for (let i = 0; i < dynamodb_items.length; i += 25) {
630
+ let next_dynamodb_requests : WriteRequest[] = dynamodb_items.slice(i, i + 25).map(next_dynamodb_item => ({
625
631
  PutRequest : {
626
632
  Item : next_dynamodb_item
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
  }
@@ -644,8 +661,8 @@ export class UtilsDynamoDB {
644
661
  const dynamodb_keys = keys.map(key => convert_input(key)?.M)
645
662
  .filter(dynamodb_key => dynamodb_key !== undefined)
646
663
 
647
- for (let i = 0; i < dynamodb_keys.length; i += 100) {
648
- let next_dynamodb_requests : WriteRequest[] = dynamodb_keys.slice(i, i + 100).map(next_dynamodb_key => ({
664
+ for (let i = 0; i < dynamodb_keys.length; i += 25) {
665
+ let next_dynamodb_requests : WriteRequest[] = dynamodb_keys.slice(i, i + 25).map(next_dynamodb_key => ({
649
666
  DeleteRequest : {
650
667
  Key : next_dynamodb_key
651
668
  }