triangle-utils 1.4.103 → 1.4.105

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.
@@ -14,9 +14,10 @@ export declare class UtilsDynamoDB {
14
14
  last_key?: Record<string, any>;
15
15
  print?: boolean;
16
16
  }): Promise<Record<string, any>[]>;
17
- get(table: string, key: Record<string, any>, options?: {
17
+ get(table_name: string, key: Record<string, any>, options?: {
18
18
  consistent?: boolean;
19
19
  }): Promise<Record<string, any> | undefined>;
20
+ batch_get(table_name: string, keys: Record<string, any>[]): Promise<Record<string, any>[]>;
20
21
  get_max(table_index_name: string, primary_key: Record<string, string | number>): Promise<Record<string, any> | undefined>;
21
22
  query(table_index_name: string, primary_key: Record<string, any>, options?: {
22
23
  reverse?: boolean;
@@ -57,6 +58,8 @@ export declare class UtilsDynamoDB {
57
58
  remove(table_name: string, key: Record<string, any>, attributes: string[]): Promise<void>;
58
59
  create(table_name: string, key: Record<string, any>, attributes?: Record<string, any>): Promise<void>;
59
60
  delete(table_name: string, key: Record<string, any>): Promise<void>;
61
+ batch_put(table_name: string, items: Record<string, any>[]): Promise<void>;
62
+ batch_delete(table_name: string, keys: Record<string, any>[]): Promise<void>;
60
63
  duplicate_attribute(table_name: string, attribute_name: string, new_attribute_name: string): Promise<void>;
61
64
  remove_attribute(table_name: string, attribute_name: string): Promise<undefined>;
62
65
  }
@@ -147,7 +147,7 @@ export class UtilsDynamoDB {
147
147
  const items = segments.flat().filter(is_item);
148
148
  return items;
149
149
  }
150
- async get(table, key, options = {}) {
150
+ async get(table_name, key, options = {}) {
151
151
  const consistent = options.consistent !== undefined ? options.consistent : false;
152
152
  const converted_key = convert_input(key)?.M;
153
153
  if (converted_key === undefined) {
@@ -155,7 +155,7 @@ export class UtilsDynamoDB {
155
155
  }
156
156
  const item = await this.dynamodb.getItem({
157
157
  ConsistentRead: consistent,
158
- TableName: table,
158
+ TableName: table_name,
159
159
  Key: converted_key
160
160
  })
161
161
  .then(response => response.Item);
@@ -168,6 +168,27 @@ export class UtilsDynamoDB {
168
168
  }
169
169
  return converted_output;
170
170
  }
171
+ async batch_get(table_name, keys) {
172
+ const dynamodb_keys = keys.map(key => convert_input(key)?.M)
173
+ .filter(dynamodb_key => dynamodb_key !== undefined);
174
+ const items = [];
175
+ for (let i = 0; i < dynamodb_keys.length; i += 100) {
176
+ let next_dynamodb_keys = dynamodb_keys.slice(i, i + 100);
177
+ while (next_dynamodb_keys.length > 0) {
178
+ const response = await this.dynamodb.batchGetItem({
179
+ RequestItems: {
180
+ [table_name]: {
181
+ Keys: next_dynamodb_keys
182
+ }
183
+ }
184
+ });
185
+ const new_items = response.Responses?.[table_name]?.map(item => convert_output({ M: item })) || [];
186
+ items.push(...new_items);
187
+ next_dynamodb_keys = response.UnprocessedKeys?.[table_name]?.Keys || [];
188
+ }
189
+ }
190
+ return items;
191
+ }
171
192
  async get_max(table_index_name, primary_key) {
172
193
  const table_name = table_index_name.split(":")[0];
173
194
  const index_name = table_index_name.split(":")[1];
@@ -477,6 +498,44 @@ export class UtilsDynamoDB {
477
498
  Key: converted_key
478
499
  });
479
500
  }
501
+ async batch_put(table_name, items) {
502
+ const dynamodb_items = items.map(item => convert_input(item)?.M)
503
+ .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 => ({
506
+ PutRequest: {
507
+ Item: next_dynamodb_item
508
+ }
509
+ }));
510
+ while (next_dynamodb_requests.length > 0) {
511
+ const response = await this.dynamodb.batchWriteItem({
512
+ RequestItems: {
513
+ [table_name]: next_dynamodb_requests
514
+ }
515
+ });
516
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
517
+ }
518
+ }
519
+ }
520
+ async batch_delete(table_name, keys) {
521
+ const dynamodb_keys = keys.map(key => convert_input(key)?.M)
522
+ .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 => ({
525
+ DeleteRequest: {
526
+ Key: next_dynamodb_key
527
+ }
528
+ }));
529
+ while (next_dynamodb_requests.length > 0) {
530
+ const response = await this.dynamodb.batchWriteItem({
531
+ RequestItems: {
532
+ [table_name]: next_dynamodb_requests
533
+ }
534
+ });
535
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
536
+ }
537
+ }
538
+ }
480
539
  async duplicate_attribute(table_name, attribute_name, new_attribute_name) {
481
540
  const table_metadata = await this.dynamodb.describeTable({
482
541
  TableName: table_name
package/dist/src/f.js CHANGED
@@ -232,5 +232,9 @@ const utils = new TriangleUtils(config);
232
232
  // }
233
233
  // )
234
234
  // console.log(response)
235
- const coordinates = await utils.bedrock.cohere_invoke("global.cohere.embed-v4:0", "yerrp");
236
- console.log(coordinates);
235
+ // const coordinates = await utils.bedrock.cohere_invoke("global.cohere.embed-v4:0", "yerrp")
236
+ // console.log(coordinates)
237
+ const scout_ids = await utils.dynamodb.scan("scouts")
238
+ .then(scouts => scouts.map(scout => scout.scout_id));
239
+ const scouts = await utils.dynamodb.batch_get("scouts", scout_ids.slice(0, 150).map(scout_id => ({ scout_id: scout_id })));
240
+ console.log(scouts.length);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.103",
3
+ "version": "1.4.105",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
package/src/UtilsBee.ts CHANGED
@@ -131,7 +131,7 @@ export class UtilsBee {
131
131
  console.log("Failed to get YouTube Subtitles:", response.status)
132
132
  continue
133
133
  }
134
- const data : Record<string, any> = await response.json()
134
+ const data : any = await response.json()
135
135
  const subtitles = data.subtitles
136
136
 
137
137
  if (subtitles === undefined) {
@@ -1,4 +1,4 @@
1
- import { AttributeValue, DynamoDB, QueryCommandInput, QueryCommandOutput, ScanCommandInput, ScanCommandOutput, UpdateItemCommandInput } from "@aws-sdk/client-dynamodb"
1
+ import { AttributeValue, DynamoDB, QueryCommandInput, QueryCommandOutput, ScanCommandInput, ScanCommandOutput, UpdateItemCommandInput, WriteRequest } from "@aws-sdk/client-dynamodb"
2
2
 
3
3
  function convert_output(dynamodb_output : AttributeValue) : string | boolean | number | any[] | Set<any> | Record<string, any> | undefined {
4
4
  if (dynamodb_output.S !== undefined) {
@@ -173,7 +173,7 @@ export class UtilsDynamoDB {
173
173
  }
174
174
 
175
175
  async get(
176
- table : string,
176
+ table_name : string,
177
177
  key : Record<string, any>,
178
178
  options : {
179
179
  consistent? : boolean
@@ -186,7 +186,7 @@ export class UtilsDynamoDB {
186
186
  }
187
187
  const item = await this.dynamodb.getItem({
188
188
  ConsistentRead : consistent,
189
- TableName : table,
189
+ TableName : table_name,
190
190
  Key : converted_key
191
191
  })
192
192
  .then(response => response.Item)
@@ -200,6 +200,32 @@ export class UtilsDynamoDB {
200
200
  return converted_output
201
201
  }
202
202
 
203
+ async batch_get(
204
+ table_name : string,
205
+ keys : Record<string, any>[]
206
+ ) : Promise<Record<string, any>[]> {
207
+ const dynamodb_keys = keys.map(key => convert_input(key)?.M)
208
+ .filter(dynamodb_key => dynamodb_key !== undefined)
209
+
210
+ const items = []
211
+ for (let i = 0; i < dynamodb_keys.length; i += 100) {
212
+ let next_dynamodb_keys = dynamodb_keys.slice(i, i + 100)
213
+ while (next_dynamodb_keys.length > 0) {
214
+ const response = await this.dynamodb.batchGetItem({
215
+ RequestItems : {
216
+ [table_name] : {
217
+ Keys : next_dynamodb_keys
218
+ }
219
+ }
220
+ })
221
+ const new_items : any[] = response.Responses?.[table_name]?.map(item => convert_output({ M : item })) || []
222
+ items.push(...new_items)
223
+ next_dynamodb_keys = response.UnprocessedKeys?.[table_name]?.Keys || []
224
+ }
225
+ }
226
+ return items
227
+ }
228
+
203
229
  async get_max(
204
230
  table_index_name : string,
205
231
  primary_key : Record<string, string | number>
@@ -587,6 +613,54 @@ export class UtilsDynamoDB {
587
613
  })
588
614
  }
589
615
 
616
+ async batch_put(
617
+ table_name : string,
618
+ items : Record<string, any>[]
619
+ ) : Promise<void> {
620
+ const dynamodb_items = items.map(item => convert_input(item)?.M)
621
+ .filter(dynamodb_item => dynamodb_item !== undefined)
622
+
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 => ({
625
+ PutRequest : {
626
+ Item : next_dynamodb_item
627
+ }
628
+ }))
629
+ while (next_dynamodb_requests.length > 0) {
630
+ const response = await this.dynamodb.batchWriteItem({
631
+ RequestItems : {
632
+ [table_name] : next_dynamodb_requests
633
+ }
634
+ })
635
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
636
+ }
637
+ }
638
+ }
639
+
640
+ async batch_delete(
641
+ table_name : string,
642
+ keys : Record<string, any>[]
643
+ ) : Promise<void> {
644
+ const dynamodb_keys = keys.map(key => convert_input(key)?.M)
645
+ .filter(dynamodb_key => dynamodb_key !== undefined)
646
+
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 => ({
649
+ DeleteRequest : {
650
+ Key : next_dynamodb_key
651
+ }
652
+ }))
653
+ while (next_dynamodb_requests.length > 0) {
654
+ const response = await this.dynamodb.batchWriteItem({
655
+ RequestItems : {
656
+ [table_name] : next_dynamodb_requests
657
+ }
658
+ })
659
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
660
+ }
661
+ }
662
+ }
663
+
590
664
  async duplicate_attribute(table_name : string, attribute_name : string, new_attribute_name : string) : Promise<void>{
591
665
  const table_metadata = await this.dynamodb.describeTable({
592
666
  TableName : table_name
package/src/f.ts CHANGED
@@ -270,6 +270,12 @@ const utils = new TriangleUtils(config)
270
270
  // )
271
271
  // console.log(response)
272
272
 
273
- const coordinates = await utils.bedrock.cohere_invoke("global.cohere.embed-v4:0", "yerrp")
273
+ // const coordinates = await utils.bedrock.cohere_invoke("global.cohere.embed-v4:0", "yerrp")
274
274
 
275
- console.log(coordinates)
275
+ // console.log(coordinates)
276
+
277
+ const scout_ids = await utils.dynamodb.scan("scouts")
278
+ .then(scouts => scouts.map(scout => scout.scout_id))
279
+
280
+ const scouts = await utils.dynamodb.batch_get("scouts", scout_ids.slice(0, 150).map(scout_id => ({ scout_id : scout_id })))
281
+ console.log(scouts.length)