triangle-utils 1.4.184 → 1.4.186

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.
@@ -7,12 +7,12 @@ interface Encryption {
7
7
  export declare class UtilsMisc {
8
8
  readonly config: GlobalConfig;
9
9
  constructor(config: GlobalConfig);
10
- safe_run<T>(f: () => Promise<T>): Promise<T | undefined>;
11
- iterate<T, U>(items: T[], f: (item: T, iterator_id: number, index: number) => Promise<U>, options?: {
10
+ static safe_run<T>(f: () => Promise<T>): Promise<T | undefined>;
11
+ static iterate<T, U>(items: T[], f: (item: T, iterator_id: number, index: number) => Promise<U>, options?: {
12
12
  num_iterators?: number;
13
13
  verbosity?: number | boolean;
14
14
  }): Promise<(U | undefined)[]>;
15
- batch_iterate<T, U>(items: T[], f: (items: T[], iterator_id: number, index: number) => Promise<U>, options: {
15
+ static batch_iterate<T, U>(items: T[], f: (items: T[], iterator_id: number, index: number) => Promise<U>, options: {
16
16
  batch_num_items?: number;
17
17
  num_iterators?: number;
18
18
  verbosity?: number | boolean;
@@ -5,7 +5,7 @@ export class UtilsMisc {
5
5
  constructor(config) {
6
6
  this.config = config;
7
7
  }
8
- async safe_run(f) {
8
+ static async safe_run(f) {
9
9
  try {
10
10
  return await f();
11
11
  }
@@ -22,7 +22,7 @@ export class UtilsMisc {
22
22
  }
23
23
  return undefined;
24
24
  }
25
- async iterate(items, f, options = {}) {
25
+ static async iterate(items, f, options = {}) {
26
26
  const num_iterators = options.num_iterators || 1;
27
27
  const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
28
28
  let index = 0;
@@ -44,7 +44,7 @@ export class UtilsMisc {
44
44
  await Promise.all(iterators);
45
45
  return outputs;
46
46
  }
47
- async batch_iterate(items, f, options) {
47
+ static async batch_iterate(items, f, options) {
48
48
  const batch_num_items = options.batch_num_items || 1;
49
49
  const num_iterators = options.num_iterators || 1;
50
50
  const verbosity = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0);
@@ -25,11 +25,12 @@ export declare class UtilsRDS {
25
25
  is_read?: boolean;
26
26
  }): Promise<Record<string, any> | undefined>;
27
27
  batch_get(table_id: string, primary_keys: Record<string, any>[], options?: {
28
+ num_iterators?: number;
28
29
  batch_num_items?: number;
29
30
  attribute_names?: string[];
30
31
  is_verbose?: boolean;
31
32
  is_read?: boolean;
32
- }): Promise<any[]>;
33
+ }): Promise<any[][]>;
33
34
  create(table_id: string, item: Record<string, any>, options?: {
34
35
  is_verbose?: boolean;
35
36
  is_read?: boolean;
@@ -1,5 +1,6 @@
1
1
  import { Pool } from "pg";
2
2
  import wkx from "wkx";
3
+ import { UtilsMisc } from "./UtilsMisc.js";
3
4
  function convert_postgres_input(input) {
4
5
  if (input === undefined) {
5
6
  return "NULL";
@@ -96,8 +97,13 @@ export class UtilsRDS {
96
97
  console.log(sql);
97
98
  }
98
99
  const pool = is_read ? this.reader_pool : this.cluster_pool;
99
- const response = await pool.query(sql);
100
- const items = response.rows.map(item => Object.fromEntries(response.fields.map(field => [field.name, convert_postgres_output(item, field)])
100
+ const response = await pool.query(sql)
101
+ .then(response => Array.isArray(response) ? response.at(-1) : response);
102
+ if (response === undefined) {
103
+ return [];
104
+ }
105
+ const rows = response.rows;
106
+ const items = rows.map((item) => Object.fromEntries(response.fields.map(field => [field.name, convert_postgres_output(item, field)])
101
107
  .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)));
102
108
  if (!Array.isArray(items)) {
103
109
  if (is_verbose) {
@@ -134,13 +140,13 @@ export class UtilsRDS {
134
140
  async batch_get(table_id, primary_keys, options = {}) {
135
141
  const batch_num_items = options.batch_num_items || 100;
136
142
  const attribute_names = Array.from(new Set(primary_keys.map(primary_key => Object.keys(primary_key)).flat())).sort();
137
- const items = [];
138
- for (let i = 0; i < primary_keys.length; i += batch_num_items) {
139
- const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + "(" + attribute_names.join(", ") + ")" + " IN " + "(" + primary_keys.slice(i, i + batch_num_items).map(primary_key => "(" + attribute_names.map(attribute_name => convert_postgres_input(primary_key[attribute_name])).join(", ") + ")").join(", ") + ")";
140
- const new_items = await this.exec(sql, options);
141
- items.push(...new_items);
142
- }
143
- return items;
143
+ const new_items = await UtilsMisc.batch_iterate(primary_keys, async (batch_primary_keys) => {
144
+ const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + "(" + attribute_names.join(", ") + ")" + " IN " + "(" + batch_primary_keys.map(primary_key => "(" + attribute_names.map(attribute_name => convert_postgres_input(primary_key[attribute_name])).join(", ") + ")").join(", ") + ")";
145
+ const batch_new_items = await this.exec(sql, options);
146
+ return batch_new_items;
147
+ }, { num_iterators: options.num_iterators, batch_num_items: batch_num_items })
148
+ .then(new_items => new_items.filter(new_item => new_item !== undefined));
149
+ return new_items;
144
150
  }
145
151
  async create(table_id, item, options = {}) {
146
152
  const attribute_names = Object.keys(item).sort();
package/dist/src/f.js CHANGED
@@ -83,5 +83,6 @@ 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("federal_census_counties", { max_num_items: 1 })
86
+ const prisms = await utils.rds.scan("prisms");
87
+ await utils.rds.batch_get("prisms", prisms.map(prism => ({ prism_id: prism.prism_id })), { is_verbose: true })
87
88
  .then(console.log);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.184",
3
+ "version": "1.4.186",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
package/src/UtilsMisc.ts CHANGED
@@ -17,7 +17,7 @@ export class UtilsMisc {
17
17
  this.config = config
18
18
  }
19
19
 
20
- async safe_run<T>(f : () => Promise<T>) : Promise<T | undefined> {
20
+ static async safe_run<T>(f : () => Promise<T>) : Promise<T | undefined> {
21
21
  try {
22
22
  return await f()
23
23
  } catch (error) {
@@ -33,7 +33,7 @@ export class UtilsMisc {
33
33
  return undefined
34
34
  }
35
35
 
36
- async iterate<T, U>(items : T[], f : (item : T, iterator_id : number, index : number) => Promise<U>, options : { num_iterators? : number, verbosity? : number | boolean } = {}) {
36
+ static async iterate<T, U>(items : T[], f : (item : T, iterator_id : number, index : number) => Promise<U>, options : { num_iterators? : number, verbosity? : number | boolean } = {}) {
37
37
  const num_iterators = options.num_iterators || 1
38
38
  const verbosity : number = typeof options.verbosity === "boolean" ? (options.verbosity === true ? 1 : 0) : (options.verbosity || 0)
39
39
  let index = 0
@@ -56,7 +56,7 @@ export class UtilsMisc {
56
56
  return outputs
57
57
  }
58
58
 
59
- async batch_iterate<T, U>(items : T[], f : (items : T[], iterator_id : number, index : number) => Promise<U>,
59
+ static async batch_iterate<T, U>(items : T[], f : (items : T[], iterator_id : number, index : number) => Promise<U>,
60
60
  options : { batch_num_items? : number, num_iterators? : number, verbosity? : number | boolean }) {
61
61
  const batch_num_items = options.batch_num_items || 1
62
62
  const num_iterators = options.num_iterators || 1
package/src/UtilsRDS.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { FieldDef, Pool } from "pg"
1
+ import { FieldDef, Pool, QueryResult } from "pg"
2
2
  import wkx from "wkx"
3
+ import { TriangleUtils } from "."
4
+ import { UtilsMisc } from "./UtilsMisc"
3
5
 
4
6
  function convert_postgres_input(input : string | boolean | number | any[] | Record<string, any> | undefined) : string {
5
7
  if (input === undefined) {
@@ -109,8 +111,13 @@ export class UtilsRDS {
109
111
  console.log(sql)
110
112
  }
111
113
  const pool = is_read ? this.reader_pool : this.cluster_pool
112
- const response = await pool.query(sql)
113
- const items = response.rows.map(item => Object.fromEntries(
114
+ const response : QueryResult<any> | undefined = await pool.query(sql)
115
+ .then(response => Array.isArray(response) ? response.at(-1) : response)
116
+ if (response === undefined) {
117
+ return []
118
+ }
119
+ const rows = response.rows
120
+ const items = rows.map((item : any) => Object.fromEntries(
114
121
  response.fields.map(field => [field.name, convert_postgres_output(item, field)])
115
122
  .filter(([attribute_name, attribute_value]) => attribute_value !== null && attribute_value !== undefined)
116
123
  ))
@@ -172,6 +179,7 @@ export class UtilsRDS {
172
179
  table_id : string,
173
180
  primary_keys : Record<string, any>[],
174
181
  options : {
182
+ num_iterators? : number,
175
183
  batch_num_items? : number,
176
184
  attribute_names? : string[],
177
185
  is_verbose? : boolean,
@@ -180,13 +188,13 @@ export class UtilsRDS {
180
188
  ) {
181
189
  const batch_num_items = options.batch_num_items || 100
182
190
  const attribute_names = Array.from(new Set(primary_keys.map(primary_key => Object.keys(primary_key)).flat())).sort()
183
- const items = []
184
- for (let i = 0; i < primary_keys.length; i += batch_num_items) {
185
- const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + "(" + attribute_names.join(", ") + ")" + " IN " + "(" + primary_keys.slice(i, i + batch_num_items).map(primary_key => "(" + attribute_names.map(attribute_name => convert_postgres_input(primary_key[attribute_name])).join(", ") + ")" ).join(", ") + ")"
186
- const new_items = await this.exec(sql, options)
187
- items.push(...new_items)
188
- }
189
- return items
191
+ const new_items = await UtilsMisc.batch_iterate(primary_keys, async (batch_primary_keys) => {
192
+ const sql = "SELECT " + (options.attribute_names === undefined ? "*" : options.attribute_names.join(", ")) + " FROM " + table_id + " WHERE " + "(" + attribute_names.join(", ") + ")" + " IN " + "(" + batch_primary_keys.map(primary_key => "(" + attribute_names.map(attribute_name => convert_postgres_input(primary_key[attribute_name])).join(", ") + ")" ).join(", ") + ")"
193
+ const batch_new_items = await this.exec(sql, options)
194
+ return batch_new_items
195
+ }, { num_iterators : options.num_iterators, batch_num_items : batch_num_items })
196
+ .then(new_items => new_items.filter(new_item => new_item !== undefined))
197
+ return new_items
190
198
  }
191
199
 
192
200
  async create(
package/src/f.ts CHANGED
@@ -106,6 +106,7 @@ const utils = new TriangleUtils(config)
106
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
107
  // .then(response => console.log(response))
108
108
 
109
+ const prisms = await utils.rds.scan("prisms")
109
110
 
110
- await utils.rds.scan("federal_census_counties", { max_num_items : 1 })
111
+ await utils.rds.batch_get("prisms", prisms.map(prism => ({ prism_id : prism.prism_id })), { is_verbose : true })
111
112
  .then(console.log)