triangle-utils 1.4.133 → 1.4.135

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,6 +1,5 @@
1
1
  export declare class UtilsRDS {
2
2
  private readonly postgres;
3
- private is_connected;
4
3
  private readonly rds_data;
5
4
  private readonly rds_cluster_arn;
6
5
  private readonly rds_secret_arn;
@@ -9,9 +8,7 @@ export declare class UtilsRDS {
9
8
  disconnect(): Promise<void>;
10
9
  query(sql: string, options?: {
11
10
  is_verbose?: boolean;
12
- }): Promise<{
13
- [k: string]: unknown;
14
- }[]>;
11
+ }): Promise<any[]>;
15
12
  exec(sql: string, options?: {
16
13
  is_verbose?: boolean;
17
14
  }): Promise<any[]>;
@@ -13,6 +13,9 @@ function convert_rds_input(input) {
13
13
  else if (typeof input === "boolean") {
14
14
  return input.toString().toUpperCase();
15
15
  }
16
+ else if (Array.isArray(input)) {
17
+ return "ARRAY[" + input.map(a => convert_rds_input(a)).join(", ") + "]";
18
+ }
16
19
  else if (typeof input === "object") {
17
20
  if (input.expression !== undefined) {
18
21
  return input.expression;
@@ -20,14 +23,35 @@ function convert_rds_input(input) {
20
23
  }
21
24
  return "NULL";
22
25
  }
26
+ function convert_rds_output(input) {
27
+ if (input.isNull) {
28
+ return undefined;
29
+ }
30
+ else if (input.stringValue !== undefined) {
31
+ return input.stringValue;
32
+ }
33
+ else if (input.longValue !== undefined) {
34
+ return input.longValue;
35
+ }
36
+ else if (input.booleanValue !== undefined) {
37
+ return input.booleanValue;
38
+ }
39
+ else if (input.arrayValue !== undefined) {
40
+ return [
41
+ ...(input.arrayValue.stringValues || []),
42
+ ...(input.arrayValue.longValues || []),
43
+ ...(input.arrayValue.booleanValues || [])
44
+ ]
45
+ .filter(value => value !== null);
46
+ }
47
+ return undefined;
48
+ }
23
49
  export class UtilsRDS {
24
50
  postgres;
25
- is_connected;
26
51
  rds_data;
27
52
  rds_cluster_arn;
28
53
  rds_secret_arn;
29
54
  constructor(region, rds_cluster_arn, rds_secret_arn, rds_host, rds_username, rds_password) {
30
- this.is_connected = false;
31
55
  this.postgres = new Client({
32
56
  host: rds_host,
33
57
  user: rds_username,
@@ -54,7 +78,8 @@ export class UtilsRDS {
54
78
  console.log(sql);
55
79
  }
56
80
  const response = await this.postgres.query(sql);
57
- return response.rows.map(item => Object.fromEntries(Object.entries(item).filter(([key, value]) => value !== null)));
81
+ return response.rows.map(item => Object.fromEntries(response.fields.map(field => [field.name, field.dataTypeID === 1700 ? Number(item[field.name]) : item[field.name]])
82
+ .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)));
58
83
  }
59
84
  async exec(sql, options = {}) {
60
85
  const is_verbose = Boolean(options.is_verbose);
@@ -67,10 +92,16 @@ export class UtilsRDS {
67
92
  secretArn: this.rds_secret_arn,
68
93
  database: "postgres",
69
94
  sql: sql,
70
- formatRecordsAs: "JSON"
95
+ includeResultMetadata: true
71
96
  })
72
- .then(response => JSON.parse(response.formattedRecords || "[]"))
73
- .then(items => items.map((item) => Object.fromEntries(Object.entries(item).filter(([key, value]) => value !== null))))
97
+ // .then(response => {
98
+ // console.log(response)
99
+ // return response
100
+ // })
101
+ .then(response => (response.records || [])
102
+ .map((item) => Object.fromEntries((response.columnMetadata || [])
103
+ .map((column, i) => [column.name || "", column.typeName === "numeric" ? Number(convert_rds_output(item[i])) : convert_rds_output(item[i])])
104
+ .filter(([key, value]) => value !== null))))
74
105
  :
75
106
  await this.query(sql, options);
76
107
  if (!Array.isArray(items)) {
package/dist/src/f.js CHANGED
@@ -67,5 +67,10 @@ const utils = new TriangleUtils(config);
67
67
  // }, { is_verbose : true })
68
68
  // console.log(response)
69
69
  // }
70
- const lal_file_voters = await utils.rds.query("SELECT * FROM lal_file_voters LIMIT 3;");
71
- console.log(lal_file_voters);
70
+ // console.log(await utils.rds.put("prisms", {
71
+ // prism_id : crypto.randomUUID(),
72
+ // state_id : "ND",
73
+ // name : "2026 Election in North Dakota",
74
+ // target_ids : ["TURNOUT-2026-11-03"]
75
+ // }, { is_verbose : true }))
76
+ // console.log(await utils.rds.exec("SELECT * FROM prisms", { is_verbose : true }))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.133",
3
+ "version": "1.4.135",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
package/src/UtilsRDS.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Field, RDSData } from "@aws-sdk/client-rds-data"
2
2
  import { Client } from "pg"
3
3
 
4
- function convert_rds_input(input : string | boolean | number | Record<string, any> | undefined) : string {
4
+ function convert_rds_input(input : string | boolean | number | any[] | Record<string, any> | undefined) : string {
5
5
  if (input === undefined) {
6
6
  return "NULL"
7
7
  } else if (typeof input === "string") {
@@ -10,6 +10,8 @@ function convert_rds_input(input : string | boolean | number | Record<string, an
10
10
  return input.toString()
11
11
  } else if (typeof input === "boolean") {
12
12
  return input.toString().toUpperCase()
13
+ } else if (Array.isArray(input)) {
14
+ return "ARRAY[" + input.map(a => convert_rds_input(a)).join(", ") + "]"
13
15
  } else if (typeof input === "object") {
14
16
  if (input.expression !== undefined) {
15
17
  return input.expression
@@ -18,10 +20,29 @@ function convert_rds_input(input : string | boolean | number | Record<string, an
18
20
  return "NULL"
19
21
  }
20
22
 
23
+ function convert_rds_output(input : Field) {
24
+ if (input.isNull) {
25
+ return undefined
26
+ } else if (input.stringValue !== undefined) {
27
+ return input.stringValue
28
+ } else if (input.longValue !== undefined) {
29
+ return input.longValue
30
+ } else if (input.booleanValue !== undefined) {
31
+ return input.booleanValue
32
+ } else if (input.arrayValue !== undefined) {
33
+ return [
34
+ ...(input.arrayValue.stringValues || []),
35
+ ...(input.arrayValue.longValues || []),
36
+ ...(input.arrayValue.booleanValues || [])
37
+ ]
38
+ .filter(value => value !== null)
39
+ }
40
+ return undefined
41
+ }
42
+
21
43
  export class UtilsRDS {
22
44
 
23
45
  private readonly postgres : Client
24
- private is_connected : boolean
25
46
  private readonly rds_data : RDSData
26
47
  private readonly rds_cluster_arn : string
27
48
  private readonly rds_secret_arn : string
@@ -34,7 +55,6 @@ export class UtilsRDS {
34
55
  rds_username : string,
35
56
  rds_password : string
36
57
  ) {
37
- this.is_connected = false
38
58
  this.postgres = new Client({
39
59
  host : rds_host,
40
60
  user : rds_username,
@@ -69,7 +89,10 @@ export class UtilsRDS {
69
89
  console.log(sql)
70
90
  }
71
91
  const response = await this.postgres.query(sql)
72
- return response.rows.map(item => Object.fromEntries(Object.entries(item).filter(([key, value]) => value !== null)))
92
+ return response.rows.map(item => Object.fromEntries(
93
+ response.fields.map(field => [field.name, field.dataTypeID === 1700 ? Number(item[field.name]) : item[field.name]])
94
+ .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)
95
+ ))
73
96
  }
74
97
 
75
98
  async exec(
@@ -88,10 +111,18 @@ export class UtilsRDS {
88
111
  secretArn : this.rds_secret_arn,
89
112
  database : "postgres",
90
113
  sql : sql,
91
- formatRecordsAs : "JSON"
114
+ includeResultMetadata : true
92
115
  })
93
- .then(response => JSON.parse(response.formattedRecords || "[]"))
94
- .then(items => items.map((item : any) => Object.fromEntries(Object.entries(item).filter(([key, value]) => value !== null))))
116
+ // .then(response => {
117
+ // console.log(response)
118
+ // return response
119
+ // })
120
+ .then(response => (response.records || [])
121
+ .map((item : any) => Object.fromEntries((response.columnMetadata || [])
122
+ .map((column, i) => [column.name || "", column.typeName === "numeric" ? Number(convert_rds_output(item[i])) : convert_rds_output(item[i])])
123
+ .filter(([key, value]) => value !== null)
124
+ ))
125
+ )
95
126
  :
96
127
  await this.query(sql, options)
97
128
  if (!Array.isArray(items)) {
package/src/f.ts CHANGED
@@ -84,5 +84,11 @@ const utils = new TriangleUtils(config)
84
84
  // console.log(response)
85
85
  // }
86
86
 
87
- const lal_file_voters = await utils.rds.query("SELECT * FROM lal_file_voters LIMIT 3;")
88
- console.log(lal_file_voters)
87
+ // console.log(await utils.rds.put("prisms", {
88
+ // prism_id : crypto.randomUUID(),
89
+ // state_id : "ND",
90
+ // name : "2026 Election in North Dakota",
91
+ // target_ids : ["TURNOUT-2026-11-03"]
92
+ // }, { is_verbose : true }))
93
+
94
+ // console.log(await utils.rds.exec("SELECT * FROM prisms", { is_verbose : true }))