triangle-utils 1.4.183 → 1.4.185

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,11 +1,14 @@
1
1
  export declare class UtilsRDS {
2
- private readonly rds_host;
2
+ private readonly rds_cluster_host;
3
+ private readonly rds_reader_host;
3
4
  private readonly rds_username;
4
5
  private readonly rds_password;
5
- private readonly pool;
6
- constructor(rds_host: string, rds_username: string, rds_password: string);
6
+ private readonly cluster_pool;
7
+ private readonly reader_pool;
8
+ constructor(rds_cluster_host: string, rds_reader_host: string, rds_username: string, rds_password: string);
7
9
  exec(sql: string, options?: {
8
10
  is_verbose?: boolean;
11
+ is_read?: boolean;
9
12
  }): Promise<any[]>;
10
13
  scan(table_id: string, options?: {
11
14
  filters?: Record<string, any>;
@@ -14,38 +17,48 @@ export declare class UtilsRDS {
14
17
  attribute_names?: string[];
15
18
  max_num_items?: number;
16
19
  is_verbose?: boolean;
20
+ is_read?: boolean;
17
21
  }): Promise<any[]>;
18
22
  get(table_id: string, primary_key: Record<string, any>, options?: {
19
23
  attribute_names?: string[];
20
24
  is_verbose?: boolean;
25
+ is_read?: boolean;
21
26
  }): Promise<Record<string, any> | undefined>;
22
27
  batch_get(table_id: string, primary_keys: Record<string, any>[], options?: {
23
28
  batch_num_items?: number;
24
29
  attribute_names?: string[];
25
30
  is_verbose?: boolean;
31
+ is_read?: boolean;
26
32
  }): Promise<any[]>;
27
33
  create(table_id: string, item: Record<string, any>, options?: {
28
34
  is_verbose?: boolean;
35
+ is_read?: boolean;
29
36
  }): Promise<any[] | undefined>;
30
37
  batch_create(table_id: string, items: Record<string, any>[], options?: {
31
38
  is_verbose?: boolean;
39
+ is_read?: boolean;
32
40
  batch_num_items?: number;
33
41
  }): Promise<void>;
34
42
  put(table_id: string, item: Record<string, any>, options?: {
35
43
  is_verbose?: boolean;
44
+ is_read?: boolean;
36
45
  }): Promise<any[] | undefined>;
37
46
  batch_put(table_id: string, items: Record<string, any>[], options?: {
38
47
  is_verbose?: boolean;
48
+ is_read?: boolean;
39
49
  batch_num_items?: number;
40
50
  }): Promise<void>;
41
51
  delete(table_id: string, primary_key: Record<string, any>, options?: {
42
52
  is_verbose?: boolean;
53
+ is_read?: boolean;
43
54
  }): Promise<any[]>;
44
55
  batch_delete(table_id: string, primary_keys: Record<string, any>[], options?: {
45
56
  is_verbose?: boolean;
57
+ is_read?: boolean;
46
58
  batch_num_items?: number;
47
59
  }): Promise<void>;
48
60
  set(table_id: string, primary_key: Record<string, any>, attributes: Record<string, any>, options?: {
49
61
  is_verbose?: boolean;
62
+ is_read?: boolean;
50
63
  }): Promise<any[]>;
51
64
  }
@@ -57,16 +57,29 @@ function convert_postgres_output(output, field) {
57
57
  return output[field.name];
58
58
  }
59
59
  export class UtilsRDS {
60
- rds_host;
60
+ rds_cluster_host;
61
+ rds_reader_host;
61
62
  rds_username;
62
63
  rds_password;
63
- pool;
64
- constructor(rds_host, rds_username, rds_password) {
65
- this.rds_host = rds_host;
64
+ cluster_pool;
65
+ reader_pool;
66
+ constructor(rds_cluster_host, rds_reader_host, rds_username, rds_password) {
67
+ this.rds_cluster_host = rds_cluster_host;
68
+ this.rds_reader_host = rds_reader_host;
66
69
  this.rds_username = rds_username;
67
70
  this.rds_password = rds_password;
68
- this.pool = new Pool({
69
- host: this.rds_host,
71
+ this.cluster_pool = new Pool({
72
+ host: this.rds_cluster_host,
73
+ user: this.rds_username,
74
+ password: this.rds_password,
75
+ database: "triangle",
76
+ port: 5432,
77
+ ssl: {
78
+ rejectUnauthorized: false
79
+ }
80
+ });
81
+ this.reader_pool = new Pool({
82
+ host: this.rds_reader_host,
70
83
  user: this.rds_username,
71
84
  password: this.rds_password,
72
85
  database: "triangle",
@@ -78,11 +91,18 @@ export class UtilsRDS {
78
91
  }
79
92
  async exec(sql, options = {}) {
80
93
  const is_verbose = Boolean(options.is_verbose);
94
+ const is_read = Boolean(options.is_read);
81
95
  if (is_verbose) {
82
96
  console.log(sql);
83
97
  }
84
- const response = await this.pool.query(sql);
85
- const items = response.rows.map(item => Object.fromEntries(response.fields.map(field => [field.name, convert_postgres_output(item, field)])
98
+ const pool = is_read ? this.reader_pool : this.cluster_pool;
99
+ const response = await pool.query(sql)
100
+ .then(response => Array.isArray(response) ? response.at(-1) : response);
101
+ if (response === undefined) {
102
+ return [];
103
+ }
104
+ const rows = response.rows;
105
+ const items = rows.map((item) => Object.fromEntries(response.fields.map(field => [field.name, convert_postgres_output(item, field)])
86
106
  .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)));
87
107
  if (!Array.isArray(items)) {
88
108
  if (is_verbose) {
package/dist/src/f.js CHANGED
@@ -83,5 +83,10 @@ const utils = new TriangleUtils(config);
83
83
  // .then(response => console.log(response))
84
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
85
  // .then(response => console.log(response))
86
- await utils.rds.scan("states")
86
+ await utils.rds.exec(`SELECT sv.voter_id FROM state_voters as sv
87
+ INNER JOIN states as s ON ST_CONTAINS(s.geom, sv.address_residence_geom) AND s.state_id = 'WA'
88
+ WHERE
89
+ sv.state_id = 'WA'
90
+ ORDER BY random()
91
+ LIMIT 1000`)
87
92
  .then(console.log);
package/dist/src/index.js CHANGED
@@ -31,7 +31,7 @@ export class TriangleUtils extends UtilsMisc {
31
31
  constructor(config) {
32
32
  super(config);
33
33
  this.dynamodb = new UtilsDynamoDB(config.region);
34
- this.rds = new UtilsRDS(config.rds_host, config.rds_username, config.rds_password);
34
+ this.rds = new UtilsRDS(config.rds_cluster_host, config.rds_reader_host, config.rds_username, config.rds_password);
35
35
  this.s3 = new UtilsS3(config.region);
36
36
  this.s3vectors = new UtilsS3Vectors(config.region);
37
37
  this.bedrock = new UtilsBedrock(config.region);
@@ -12,7 +12,8 @@ export declare class GlobalConfig {
12
12
  readonly s3_triage: string;
13
13
  readonly rds_cluster_arn: string;
14
14
  readonly rds_secret_arn: string;
15
- readonly rds_host: string;
15
+ readonly rds_cluster_host: string;
16
+ readonly rds_reader_host: string;
16
17
  readonly rds_username: string;
17
18
  readonly rds_password: string;
18
19
  readonly super_federal_gov_api_keys: string;
@@ -12,7 +12,8 @@ export class GlobalConfig {
12
12
  s3_triage;
13
13
  rds_cluster_arn;
14
14
  rds_secret_arn;
15
- rds_host;
15
+ rds_cluster_host;
16
+ rds_reader_host;
16
17
  rds_username;
17
18
  rds_password;
18
19
  super_federal_gov_api_keys;
@@ -43,7 +44,8 @@ export class GlobalConfig {
43
44
  this.s3_triage = global_config.s3_triage;
44
45
  this.rds_cluster_arn = global_config.rds_cluster_arn;
45
46
  this.rds_secret_arn = global_config.rds_secret_arn;
46
- this.rds_host = global_config.rds_host;
47
+ this.rds_cluster_host = global_config.rds_cluster_host;
48
+ this.rds_reader_host = global_config.rds_reader_host;
47
49
  this.rds_username = global_config.rds_username;
48
50
  this.rds_password = global_config.rds_password;
49
51
  this.super_federal_gov_api_keys = global_config.super_federal_gov_api_keys;
@@ -73,7 +75,8 @@ export class GlobalConfig {
73
75
  typeof global_config.s3_triage === "string" &&
74
76
  typeof global_config.rds_cluster_arn === "string" &&
75
77
  typeof global_config.rds_secret_arn === "string" &&
76
- typeof global_config.rds_host === "string" &&
78
+ typeof global_config.rds_cluster_host === "string" &&
79
+ typeof global_config.rds_reader_host === "string" &&
77
80
  typeof global_config.rds_username === "string" &&
78
81
  typeof global_config.rds_password === "string" &&
79
82
  typeof global_config.super_federal_gov_api_keys === "string" &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.183",
3
+ "version": "1.4.185",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
package/src/UtilsRDS.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { FieldDef, Pool } from "pg"
1
+ import { FieldDef, Pool, QueryResult } from "pg"
2
2
  import wkx from "wkx"
3
3
 
4
4
  function convert_postgres_input(input : string | boolean | number | any[] | Record<string, any> | undefined) : string {
@@ -57,21 +57,35 @@ function convert_postgres_output(output : any, field : FieldDef) {
57
57
 
58
58
  export class UtilsRDS {
59
59
 
60
- private readonly rds_host : string
60
+ private readonly rds_cluster_host : string
61
+ private readonly rds_reader_host : string
61
62
  private readonly rds_username : string
62
63
  private readonly rds_password : string
63
- private readonly pool : Pool
64
+ private readonly cluster_pool : Pool
65
+ private readonly reader_pool : Pool
64
66
 
65
67
  constructor(
66
- rds_host : string,
68
+ rds_cluster_host : string,
69
+ rds_reader_host : string,
67
70
  rds_username : string,
68
71
  rds_password : string
69
72
  ) {
70
- this.rds_host = rds_host
73
+ this.rds_cluster_host = rds_cluster_host
74
+ this.rds_reader_host = rds_reader_host
71
75
  this.rds_username = rds_username
72
76
  this.rds_password = rds_password
73
- this.pool = new Pool({
74
- host : this.rds_host,
77
+ this.cluster_pool = new Pool({
78
+ host : this.rds_cluster_host,
79
+ user : this.rds_username,
80
+ password : this.rds_password,
81
+ database : "triangle",
82
+ port : 5432,
83
+ ssl : {
84
+ rejectUnauthorized: false
85
+ }
86
+ })
87
+ this.reader_pool = new Pool({
88
+ host : this.rds_reader_host,
75
89
  user : this.rds_username,
76
90
  password : this.rds_password,
77
91
  database : "triangle",
@@ -85,15 +99,23 @@ export class UtilsRDS {
85
99
  async exec(
86
100
  sql : string,
87
101
  options : {
88
- is_verbose? : boolean
102
+ is_verbose? : boolean,
103
+ is_read? : boolean
89
104
  } = {}
90
105
  ) {
91
106
  const is_verbose = Boolean(options.is_verbose)
107
+ const is_read = Boolean(options.is_read)
92
108
  if (is_verbose) {
93
109
  console.log(sql)
94
110
  }
95
- const response = await this.pool.query(sql)
96
- const items = response.rows.map(item => Object.fromEntries(
111
+ const pool = is_read ? this.reader_pool : this.cluster_pool
112
+ const response : QueryResult<any> | undefined = await pool.query(sql)
113
+ .then(response => Array.isArray(response) ? response.at(-1) : response)
114
+ if (response === undefined) {
115
+ return []
116
+ }
117
+ const rows = response.rows
118
+ const items = rows.map((item : any) => Object.fromEntries(
97
119
  response.fields.map(field => [field.name, convert_postgres_output(item, field)])
98
120
  .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)
99
121
  ))
@@ -114,7 +136,8 @@ export class UtilsRDS {
114
136
  defined_attribute_names? : string[],
115
137
  attribute_names? : string[],
116
138
  max_num_items? : number,
117
- is_verbose? : boolean
139
+ is_verbose? : boolean,
140
+ is_read? : boolean
118
141
  } = {}
119
142
  ) {
120
143
  const undefined_attribute_names = options.undefined_attribute_names || []
@@ -140,7 +163,8 @@ export class UtilsRDS {
140
163
  primary_key : Record<string, any>,
141
164
  options : {
142
165
  attribute_names? : string[],
143
- is_verbose? : boolean
166
+ is_verbose? : boolean,
167
+ is_read? : boolean
144
168
  } = {}
145
169
  ) {
146
170
  const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + Object.entries(primary_key).map(([key_name, value]) => key_name + " = " + convert_postgres_input(value)).join(" AND ")
@@ -155,7 +179,8 @@ export class UtilsRDS {
155
179
  options : {
156
180
  batch_num_items? : number,
157
181
  attribute_names? : string[],
158
- is_verbose? : boolean
182
+ is_verbose? : boolean,
183
+ is_read? : boolean
159
184
  } = {}
160
185
  ) {
161
186
  const batch_num_items = options.batch_num_items || 100
@@ -173,7 +198,8 @@ export class UtilsRDS {
173
198
  table_id : string,
174
199
  item : Record<string, any>,
175
200
  options : {
176
- is_verbose? : boolean
201
+ is_verbose? : boolean,
202
+ is_read? : boolean
177
203
  } = {}
178
204
  ) {
179
205
  const attribute_names = Object.keys(item).sort()
@@ -194,7 +220,8 @@ export class UtilsRDS {
194
220
  table_id : string,
195
221
  items : Record<string, any>[],
196
222
  options : {
197
- is_verbose? : boolean
223
+ is_verbose? : boolean,
224
+ is_read? : boolean
198
225
  batch_num_items? : number
199
226
  } = {}
200
227
  ) {
@@ -221,7 +248,8 @@ export class UtilsRDS {
221
248
  table_id : string,
222
249
  item : Record<string, any>,
223
250
  options : {
224
- is_verbose? : boolean
251
+ is_verbose? : boolean,
252
+ is_read? : boolean
225
253
  } = {}
226
254
  ) {
227
255
  const attribute_names = Object.keys(item).sort()
@@ -243,6 +271,7 @@ export class UtilsRDS {
243
271
  items : Record<string, any>[],
244
272
  options : {
245
273
  is_verbose? : boolean,
274
+ is_read? : boolean,
246
275
  batch_num_items? : number
247
276
  } = {}
248
277
  ) {
@@ -269,7 +298,8 @@ export class UtilsRDS {
269
298
  table_id : string,
270
299
  primary_key : Record<string, any>,
271
300
  options : {
272
- is_verbose? : boolean
301
+ is_verbose? : boolean,
302
+ is_read? : boolean
273
303
  } = {}
274
304
  ) {
275
305
  const sql = "DELETE FROM " + table_id + " WHERE " + Object.entries(primary_key).map(([key_name, value]) => key_name + " = " + convert_postgres_input(value)).join(" AND ")
@@ -281,6 +311,7 @@ export class UtilsRDS {
281
311
  primary_keys : Record<string, any>[],
282
312
  options : {
283
313
  is_verbose? : boolean,
314
+ is_read? : boolean,
284
315
  batch_num_items? : number
285
316
  } = {}
286
317
  ) {
@@ -297,7 +328,8 @@ export class UtilsRDS {
297
328
  primary_key : Record<string, any>,
298
329
  attributes : Record<string, any>,
299
330
  options : {
300
- is_verbose? : boolean
331
+ is_verbose? : boolean,
332
+ is_read? : boolean
301
333
  } = {}
302
334
  ) {
303
335
  const attribute_names = Object.keys(attributes).sort()
package/src/f.ts CHANGED
@@ -107,5 +107,10 @@ const utils = new TriangleUtils(config)
107
107
  // .then(response => console.log(response))
108
108
 
109
109
 
110
- await utils.rds.scan("states")
110
+ await utils.rds.exec(`SELECT sv.voter_id FROM state_voters as sv
111
+ INNER JOIN states as s ON ST_CONTAINS(s.geom, sv.address_residence_geom) AND s.state_id = 'WA'
112
+ WHERE
113
+ sv.state_id = 'WA'
114
+ ORDER BY random()
115
+ LIMIT 1000`)
111
116
  .then(console.log)
package/src/index.ts CHANGED
@@ -37,7 +37,7 @@ export class TriangleUtils extends UtilsMisc {
37
37
  constructor(config : GlobalConfig) {
38
38
  super(config)
39
39
  this.dynamodb = new UtilsDynamoDB(config.region)
40
- this.rds = new UtilsRDS(config.rds_host, config.rds_username, config.rds_password)
40
+ this.rds = new UtilsRDS(config.rds_cluster_host, config.rds_reader_host, config.rds_username, config.rds_password)
41
41
  this.s3 = new UtilsS3(config.region)
42
42
  this.s3vectors = new UtilsS3Vectors(config.region)
43
43
  this.bedrock = new UtilsBedrock(config.region)
@@ -12,7 +12,8 @@ export class GlobalConfig {
12
12
  readonly s3_triage : string
13
13
  readonly rds_cluster_arn : string
14
14
  readonly rds_secret_arn : string
15
- readonly rds_host : string
15
+ readonly rds_cluster_host : string
16
+ readonly rds_reader_host : string
16
17
  readonly rds_username : string
17
18
  readonly rds_password : string
18
19
  readonly super_federal_gov_api_keys : string
@@ -45,7 +46,8 @@ export class GlobalConfig {
45
46
  this.s3_triage = global_config.s3_triage
46
47
  this.rds_cluster_arn = global_config.rds_cluster_arn
47
48
  this.rds_secret_arn = global_config.rds_secret_arn
48
- this.rds_host = global_config.rds_host
49
+ this.rds_cluster_host = global_config.rds_cluster_host
50
+ this.rds_reader_host = global_config.rds_reader_host
49
51
  this.rds_username = global_config.rds_username
50
52
  this.rds_password = global_config.rds_password
51
53
  this.super_federal_gov_api_keys = global_config.super_federal_gov_api_keys
@@ -77,7 +79,8 @@ export class GlobalConfig {
77
79
  typeof global_config.s3_triage === "string" &&
78
80
  typeof global_config.rds_cluster_arn === "string" &&
79
81
  typeof global_config.rds_secret_arn === "string" &&
80
- typeof global_config.rds_host === "string" &&
82
+ typeof global_config.rds_cluster_host === "string" &&
83
+ typeof global_config.rds_reader_host === "string" &&
81
84
  typeof global_config.rds_username === "string" &&
82
85
  typeof global_config.rds_password === "string" &&
83
86
  typeof global_config.super_federal_gov_api_keys === "string" &&