triangle-utils 1.4.156 → 1.4.158

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.
@@ -1,5 +1,8 @@
1
1
  export declare class UtilsRDS {
2
- private readonly postgres;
2
+ private readonly rds_host;
3
+ private readonly rds_username;
4
+ private readonly rds_password;
5
+ private postgres?;
3
6
  private readonly rds_data;
4
7
  private readonly rds_cluster_arn;
5
8
  private readonly rds_secret_arn;
@@ -8,7 +11,7 @@ export declare class UtilsRDS {
8
11
  disconnect(): Promise<void>;
9
12
  query(sql: string, options?: {
10
13
  is_verbose?: boolean;
11
- }): Promise<any[]>;
14
+ }): Promise<any[] | undefined>;
12
15
  exec(sql: string, options?: {
13
16
  is_verbose?: boolean;
14
17
  is_forced?: boolean;
@@ -27,7 +30,8 @@ export declare class UtilsRDS {
27
30
  is_verbose?: boolean;
28
31
  is_forced?: boolean;
29
32
  }): Promise<Record<string, any> | undefined>;
30
- batch_get(table_id: string, primary_key: Record<string, any[]>, options?: {
33
+ batch_get(table_id: string, primary_keys: Record<string, any>[], options?: {
34
+ batch_num_items?: number;
31
35
  attribute_names?: string[];
32
36
  is_verbose?: boolean;
33
37
  is_forced?: boolean;
@@ -50,37 +50,56 @@ function convert_rds_output(input) {
50
50
  return undefined;
51
51
  }
52
52
  export class UtilsRDS {
53
+ rds_host;
54
+ rds_username;
55
+ rds_password;
53
56
  postgres;
54
57
  rds_data;
55
58
  rds_cluster_arn;
56
59
  rds_secret_arn;
57
60
  constructor(region, rds_cluster_arn, rds_secret_arn, rds_host, rds_username, rds_password) {
61
+ this.rds_host = rds_host;
62
+ this.rds_username = rds_username;
63
+ this.rds_password = rds_password;
64
+ this.rds_data = new RDSData({ region: region });
65
+ this.rds_cluster_arn = rds_cluster_arn,
66
+ this.rds_secret_arn = rds_secret_arn;
67
+ }
68
+ async connect() {
69
+ if (this.postgres !== undefined) {
70
+ await this.disconnect();
71
+ }
58
72
  this.postgres = new Client({
59
- host: rds_host,
60
- user: rds_username,
61
- password: rds_password,
73
+ host: this.rds_host,
74
+ user: this.rds_username,
75
+ password: this.rds_password,
62
76
  database: "triangle",
63
77
  port: 5432,
64
78
  ssl: {
65
79
  rejectUnauthorized: false
66
80
  }
67
81
  });
68
- this.rds_data = new RDSData({ region: region });
69
- this.rds_cluster_arn = rds_cluster_arn,
70
- this.rds_secret_arn = rds_secret_arn;
71
- }
72
- async connect() {
73
82
  await this.postgres.connect();
74
83
  }
75
84
  async disconnect() {
85
+ if (this.postgres === undefined) {
86
+ return;
87
+ }
76
88
  await this.postgres.end();
89
+ this.postgres = undefined;
77
90
  }
78
91
  async query(sql, options = {}) {
79
92
  const is_verbose = Boolean(options.is_verbose);
93
+ await this.connect();
80
94
  if (is_verbose) {
81
95
  console.log(sql);
82
96
  }
97
+ if (this.postgres === undefined) {
98
+ console.log("Failed to connect to Postgres.");
99
+ return;
100
+ }
83
101
  const response = await this.postgres.query(sql);
102
+ await this.disconnect();
84
103
  return response.rows.map(item => Object.fromEntries(response.fields.map(field => [field.name, field.dataTypeID === 1700 ? Number(item[field.name]) : item[field.name]])
85
104
  .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)));
86
105
  }
@@ -140,9 +159,15 @@ export class UtilsRDS {
140
159
  const item = items[0];
141
160
  return item;
142
161
  }
143
- async batch_get(table_id, primary_key, options = {}) {
144
- const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + Object.entries(primary_key).map(([key_name, values]) => key_name + " IN " + "(" + values.map(value => convert_rds_input(value)).join(", ") + ")").join(" AND ");
145
- const items = await this.exec(sql, options);
162
+ async batch_get(table_id, primary_keys, options = {}) {
163
+ const batch_num_items = options.batch_num_items || 100;
164
+ const attribute_names = Array.from(new Set(primary_keys.map(primary_key => Object.keys(primary_key)).flat())).sort();
165
+ const items = [];
166
+ for (let i = 0; i < primary_keys.length; i += batch_num_items) {
167
+ const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + "(" + attribute_names.join(", ") + ")" + " IN " + "(" + primary_keys.map(primary_key => "(" + attribute_names.map(attribute_name => convert_rds_input(primary_key[attribute_name])).join(", ") + ")").join(", ") + ")";
168
+ const new_items = await this.exec(sql, options);
169
+ items.push(...new_items);
170
+ }
146
171
  return items;
147
172
  }
148
173
  async create(table_id, item, options = {}) {
package/dist/src/f.js CHANGED
@@ -81,5 +81,7 @@ const utils = new TriangleUtils(config);
81
81
  // .then(user => console.log(user))
82
82
  // await utils.bedrock.cohere_invoke("cohere.embed-v4", "yyyyyuuuuuurrrrr")
83
83
  // .then(response => console.log(response))
84
- await utils.s3.get_download_url("s3://triangle-675045717295-us-east-1-an/tenant_documents/a3a9627f-a94d-4d47-a110-307a6f5563af/a146d789-da40-4e3c-a132-8b7110f3f7df.pdf", { name: "hye.pdf" })
85
- .then(response => console.log(response));
84
+ // await utils.s3.get_download_url("s3://triangle-675045717295-us-east-1-an/tenant_documents/a3a9627f-a94d-4d47-a110-307a6f5563af/a146d789-da40-4e3c-a132-8b7110f3f7df.pdf", { name : "hye.pdf" })
85
+ // .then(response => console.log(response))
86
+ const scouts = await utils.rds.batch_get("scouts", [], { is_verbose: true })
87
+ .then(console.log);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.156",
3
+ "version": "1.4.158",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
package/src/UtilsRDS.ts CHANGED
@@ -44,7 +44,10 @@ function convert_rds_output(input : Field) {
44
44
 
45
45
  export class UtilsRDS {
46
46
 
47
- private readonly postgres : Client
47
+ private readonly rds_host : string
48
+ private readonly rds_username : string
49
+ private readonly rds_password : string
50
+ private postgres? : Client
48
51
  private readonly rds_data : RDSData
49
52
  private readonly rds_cluster_arn : string
50
53
  private readonly rds_secret_arn : string
@@ -57,27 +60,37 @@ export class UtilsRDS {
57
60
  rds_username : string,
58
61
  rds_password : string
59
62
  ) {
63
+ this.rds_host = rds_host
64
+ this.rds_username = rds_username
65
+ this.rds_password = rds_password
66
+ this.rds_data = new RDSData({ region : region })
67
+ this.rds_cluster_arn = rds_cluster_arn,
68
+ this.rds_secret_arn = rds_secret_arn
69
+ }
70
+
71
+ async connect() {
72
+ if (this.postgres !== undefined) {
73
+ await this.disconnect()
74
+ }
60
75
  this.postgres = new Client({
61
- host : rds_host,
62
- user : rds_username,
63
- password : rds_password,
76
+ host : this.rds_host,
77
+ user : this.rds_username,
78
+ password : this.rds_password,
64
79
  database : "triangle",
65
80
  port : 5432,
66
81
  ssl : {
67
82
  rejectUnauthorized: false
68
83
  }
69
84
  })
70
- this.rds_data = new RDSData({ region : region })
71
- this.rds_cluster_arn = rds_cluster_arn,
72
- this.rds_secret_arn = rds_secret_arn
73
- }
74
-
75
- async connect() {
76
85
  await this.postgres.connect()
77
86
  }
78
87
 
79
88
  async disconnect() {
89
+ if (this.postgres === undefined) {
90
+ return
91
+ }
80
92
  await this.postgres.end()
93
+ this.postgres = undefined
81
94
  }
82
95
 
83
96
  async query(
@@ -87,10 +100,16 @@ export class UtilsRDS {
87
100
  } = {}
88
101
  ) {
89
102
  const is_verbose = Boolean(options.is_verbose)
103
+ await this.connect()
90
104
  if (is_verbose) {
91
105
  console.log(sql)
92
106
  }
107
+ if (this.postgres === undefined) {
108
+ console.log("Failed to connect to Postgres.")
109
+ return
110
+ }
93
111
  const response = await this.postgres.query(sql)
112
+ await this.disconnect()
94
113
  return response.rows.map(item => Object.fromEntries(
95
114
  response.fields.map(field => [field.name, field.dataTypeID === 1700 ? Number(item[field.name]) : item[field.name]])
96
115
  .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)
@@ -185,15 +204,22 @@ export class UtilsRDS {
185
204
 
186
205
  async batch_get(
187
206
  table_id : string,
188
- primary_key : Record<string, any[]>,
207
+ primary_keys : Record<string, any>[],
189
208
  options : {
209
+ batch_num_items? : number,
190
210
  attribute_names? : string[],
191
211
  is_verbose? : boolean,
192
212
  is_forced? : boolean
193
213
  } = {}
194
214
  ) {
195
- const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + Object.entries(primary_key).map(([key_name, values]) => key_name + " IN " + "(" + values.map(value => convert_rds_input(value)).join(", ") + ")").join(" AND ")
196
- const items = await this.exec(sql, options)
215
+ const batch_num_items = options.batch_num_items || 100
216
+ const attribute_names = Array.from(new Set(primary_keys.map(primary_key => Object.keys(primary_key)).flat())).sort()
217
+ const items = []
218
+ for (let i = 0; i < primary_keys.length; i += batch_num_items) {
219
+ const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + "(" + attribute_names.join(", ") + ")" + " IN " + "(" + primary_keys.map(primary_key => "(" + attribute_names.map(attribute_name => convert_rds_input(primary_key[attribute_name])).join(", ") + ")" ).join(", ") + ")"
220
+ const new_items = await this.exec(sql, options)
221
+ items.push(...new_items)
222
+ }
197
223
  return items
198
224
  }
199
225
 
package/src/f.ts CHANGED
@@ -103,5 +103,8 @@ const utils = new TriangleUtils(config)
103
103
  // await utils.bedrock.cohere_invoke("cohere.embed-v4", "yyyyyuuuuuurrrrr")
104
104
  // .then(response => console.log(response))
105
105
 
106
- await utils.s3.get_download_url("s3://triangle-675045717295-us-east-1-an/tenant_documents/a3a9627f-a94d-4d47-a110-307a6f5563af/a146d789-da40-4e3c-a132-8b7110f3f7df.pdf", { name : "hye.pdf" })
107
- .then(response => console.log(response))
106
+ // await utils.s3.get_download_url("s3://triangle-675045717295-us-east-1-an/tenant_documents/a3a9627f-a94d-4d47-a110-307a6f5563af/a146d789-da40-4e3c-a132-8b7110f3f7df.pdf", { name : "hye.pdf" })
107
+ // .then(response => console.log(response))
108
+
109
+ const scouts = await utils.rds.batch_get("scouts", [], { is_verbose : true })
110
+ .then(console.log)