triangle-utils 1.4.185 → 1.4.187

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,6 +25,7 @@ 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;
@@ -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";
@@ -139,13 +140,13 @@ export class UtilsRDS {
139
140
  async batch_get(table_id, primary_keys, options = {}) {
140
141
  const batch_num_items = options.batch_num_items || 100;
141
142
  const attribute_names = Array.from(new Set(primary_keys.map(primary_key => Object.keys(primary_key)).flat())).sort();
142
- const items = [];
143
- for (let i = 0; i < primary_keys.length; i += batch_num_items) {
144
- 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(", ") + ")";
145
- const new_items = await this.exec(sql, options);
146
- items.push(...new_items);
147
- }
148
- 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.flat();
149
150
  }
150
151
  async create(table_id, item, options = {}) {
151
152
  const attribute_names = Object.keys(item).sort();
package/dist/src/f.js CHANGED
@@ -83,10 +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.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`)
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 })
92
88
  .then(console.log);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.185",
3
+ "version": "1.4.187",
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
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) {
@@ -177,6 +179,7 @@ export class UtilsRDS {
177
179
  table_id : string,
178
180
  primary_keys : Record<string, any>[],
179
181
  options : {
182
+ num_iterators? : number,
180
183
  batch_num_items? : number,
181
184
  attribute_names? : string[],
182
185
  is_verbose? : boolean,
@@ -185,13 +188,13 @@ export class UtilsRDS {
185
188
  ) {
186
189
  const batch_num_items = options.batch_num_items || 100
187
190
  const attribute_names = Array.from(new Set(primary_keys.map(primary_key => Object.keys(primary_key)).flat())).sort()
188
- const items = []
189
- for (let i = 0; i < primary_keys.length; i += batch_num_items) {
190
- 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(", ") + ")"
191
- const new_items = await this.exec(sql, options)
192
- items.push(...new_items)
193
- }
194
- 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.flat()
195
198
  }
196
199
 
197
200
  async create(
package/src/f.ts CHANGED
@@ -106,11 +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.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
+ await utils.rds.batch_get("prisms", prisms.map(prism => ({ prism_id : prism.prism_id })), { is_verbose : true })
116
112
  .then(console.log)