triangle-utils 1.4.103 → 1.4.104

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,28 @@ 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
+ console.log(response);
186
+ const new_items = response.Responses?.[table_name]?.map(item => convert_output({ M: item })) || [];
187
+ items.push(...new_items);
188
+ next_dynamodb_keys = response.UnprocessedKeys?.[table_name]?.Keys || [];
189
+ }
190
+ }
191
+ return items;
192
+ }
171
193
  async get_max(table_index_name, primary_key) {
172
194
  const table_name = table_index_name.split(":")[0];
173
195
  const index_name = table_index_name.split(":")[1];
@@ -477,6 +499,44 @@ export class UtilsDynamoDB {
477
499
  Key: converted_key
478
500
  });
479
501
  }
502
+ async batch_put(table_name, items) {
503
+ const dynamodb_items = items.map(item => convert_input(item)?.M)
504
+ .filter(dynamodb_item => dynamodb_item !== undefined);
505
+ for (let i = 0; i < dynamodb_items.length; i += 100) {
506
+ let next_dynamodb_requests = dynamodb_items.slice(i, i + 100).map(next_dynamodb_item => ({
507
+ PutRequest: {
508
+ Item: next_dynamodb_item
509
+ }
510
+ }));
511
+ while (next_dynamodb_requests.length > 0) {
512
+ const response = await this.dynamodb.batchWriteItem({
513
+ RequestItems: {
514
+ [table_name]: next_dynamodb_requests
515
+ }
516
+ });
517
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
518
+ }
519
+ }
520
+ }
521
+ async batch_delete(table_name, keys) {
522
+ const dynamodb_keys = keys.map(key => convert_input(key)?.M)
523
+ .filter(dynamodb_key => dynamodb_key !== undefined);
524
+ for (let i = 0; i < dynamodb_keys.length; i += 100) {
525
+ let next_dynamodb_requests = dynamodb_keys.slice(i, i + 100).map(next_dynamodb_key => ({
526
+ DeleteRequest: {
527
+ Key: next_dynamodb_key
528
+ }
529
+ }));
530
+ while (next_dynamodb_requests.length > 0) {
531
+ const response = await this.dynamodb.batchWriteItem({
532
+ RequestItems: {
533
+ [table_name]: next_dynamodb_requests
534
+ }
535
+ });
536
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || [];
537
+ }
538
+ }
539
+ }
480
540
  async duplicate_attribute(table_name, attribute_name, new_attribute_name) {
481
541
  const table_metadata = await this.dynamodb.describeTable({
482
542
  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.104",
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,33 @@ 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
+ console.log(response)
222
+ const new_items : any[] = response.Responses?.[table_name]?.map(item => convert_output({ M : item })) || []
223
+ items.push(...new_items)
224
+ next_dynamodb_keys = response.UnprocessedKeys?.[table_name]?.Keys || []
225
+ }
226
+ }
227
+ return items
228
+ }
229
+
203
230
  async get_max(
204
231
  table_index_name : string,
205
232
  primary_key : Record<string, string | number>
@@ -587,6 +614,54 @@ export class UtilsDynamoDB {
587
614
  })
588
615
  }
589
616
 
617
+ async batch_put(
618
+ table_name : string,
619
+ items : Record<string, any>[]
620
+ ) : Promise<void> {
621
+ const dynamodb_items = items.map(item => convert_input(item)?.M)
622
+ .filter(dynamodb_item => dynamodb_item !== undefined)
623
+
624
+ for (let i = 0; i < dynamodb_items.length; i += 100) {
625
+ let next_dynamodb_requests : WriteRequest[] = dynamodb_items.slice(i, i + 100).map(next_dynamodb_item => ({
626
+ PutRequest : {
627
+ Item : next_dynamodb_item
628
+ }
629
+ }))
630
+ while (next_dynamodb_requests.length > 0) {
631
+ const response = await this.dynamodb.batchWriteItem({
632
+ RequestItems : {
633
+ [table_name] : next_dynamodb_requests
634
+ }
635
+ })
636
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
637
+ }
638
+ }
639
+ }
640
+
641
+ async batch_delete(
642
+ table_name : string,
643
+ keys : Record<string, any>[]
644
+ ) : Promise<void> {
645
+ const dynamodb_keys = keys.map(key => convert_input(key)?.M)
646
+ .filter(dynamodb_key => dynamodb_key !== undefined)
647
+
648
+ for (let i = 0; i < dynamodb_keys.length; i += 100) {
649
+ let next_dynamodb_requests : WriteRequest[] = dynamodb_keys.slice(i, i + 100).map(next_dynamodb_key => ({
650
+ DeleteRequest : {
651
+ Key : next_dynamodb_key
652
+ }
653
+ }))
654
+ while (next_dynamodb_requests.length > 0) {
655
+ const response = await this.dynamodb.batchWriteItem({
656
+ RequestItems : {
657
+ [table_name] : next_dynamodb_requests
658
+ }
659
+ })
660
+ next_dynamodb_requests = response.UnprocessedItems?.[table_name] || []
661
+ }
662
+ }
663
+ }
664
+
590
665
  async duplicate_attribute(table_name : string, attribute_name : string, new_attribute_name : string) : Promise<void>{
591
666
  const table_metadata = await this.dynamodb.describeTable({
592
667
  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)