triangle-utils 1.4.161 → 1.4.163

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,4 +1,5 @@
1
1
  import { GlobalConfig } from "./types/GlobalConfig.js";
2
+ import { Readable } from "stream";
2
3
  interface Encryption {
3
4
  iv: string;
4
5
  ciphertext: string;
@@ -25,5 +26,10 @@ export declare class UtilsMisc {
25
26
  static encrypt(secret_key: string, text: string): Encryption;
26
27
  static decrypt(secret_key: string, encryption: Encryption): string;
27
28
  static get_secret_hash(username: string, cognito_client_id: string, cognito_client_secret: string): string;
29
+ static batch_import_csv<T>(stream: Readable, import_new_items: (new_items: any[]) => Promise<void>, options: {
30
+ delimiter?: string;
31
+ batch_num_items: number;
32
+ max_num_items?: number;
33
+ }): Promise<void>;
28
34
  }
29
35
  export {};
@@ -1,4 +1,5 @@
1
1
  import * as crypto from "crypto";
2
+ import csv_parser from "csv-parser";
2
3
  export class UtilsMisc {
3
4
  config;
4
5
  constructor(config) {
@@ -114,4 +115,35 @@ export class UtilsMisc {
114
115
  .digest("base64");
115
116
  return secret_hash;
116
117
  }
118
+ static async batch_import_csv(stream, import_new_items, options) {
119
+ const delimiter = options.delimiter || ",";
120
+ const max_num_items = options.max_num_items;
121
+ const batch_num_items = options.batch_num_items;
122
+ let new_items = [];
123
+ let i = 0;
124
+ const csv_stream = stream.pipe(csv_parser({ separator: delimiter }));
125
+ await new Promise(resolve => {
126
+ csv_stream
127
+ .on("data", async (item) => {
128
+ i += 1;
129
+ if (max_num_items !== undefined && i > max_num_items) {
130
+ csv_stream.end();
131
+ return;
132
+ }
133
+ new_items.push(item);
134
+ if (i % batch_num_items === 0) {
135
+ csv_stream.pause();
136
+ console.log(i);
137
+ await import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()));
138
+ new_items = [];
139
+ csv_stream.resume();
140
+ }
141
+ })
142
+ .on("end", async () => {
143
+ await import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()));
144
+ new_items = [];
145
+ resolve();
146
+ });
147
+ });
148
+ }
117
149
  }
@@ -32,7 +32,6 @@ export declare class TriangleUtils extends UtilsMisc {
32
32
  }
33
33
  export * from "./types/ApplicationConfig.js";
34
34
  export * from "./types/GlobalConfig.js";
35
- export * from "./UtilsMisc.js";
36
35
  export * from "./UtilsAnthropic.js";
37
36
  export * from "./UtilsDynamoDB.js";
38
37
  export * from "./UtilsRDS.js";
package/dist/src/index.js CHANGED
@@ -45,7 +45,6 @@ export class TriangleUtils extends UtilsMisc {
45
45
  }
46
46
  export * from "./types/ApplicationConfig.js";
47
47
  export * from "./types/GlobalConfig.js";
48
- export * from "./UtilsMisc.js";
49
48
  export * from "./UtilsAnthropic.js";
50
49
  export * from "./UtilsDynamoDB.js";
51
50
  export * from "./UtilsRDS.js";
@@ -21,6 +21,7 @@ export declare class GlobalConfig {
21
21
  readonly twitter_api_key: string;
22
22
  readonly youtube_api_key: string;
23
23
  readonly xai_api_key: string;
24
+ readonly ballotpedia_api_key: string;
24
25
  readonly [x: string]: any;
25
26
  constructor(global_config: GlobalConfig);
26
27
  static is(global_config: any): global_config is GlobalConfig;
@@ -21,6 +21,7 @@ export class GlobalConfig {
21
21
  twitter_api_key;
22
22
  youtube_api_key;
23
23
  xai_api_key;
24
+ ballotpedia_api_key;
24
25
  constructor(global_config) {
25
26
  if (!GlobalConfig.is(global_config)) {
26
27
  throw Error("Invalid input.");
@@ -47,6 +48,7 @@ export class GlobalConfig {
47
48
  this.twitter_api_key = global_config.twitter_api_key;
48
49
  this.youtube_api_key = global_config.youtube_api_key;
49
50
  this.xai_api_key = global_config.xai_api_key;
51
+ this.ballotpedia_api_key = global_config.ballotpedia_api_key;
50
52
  }
51
53
  static is(global_config) {
52
54
  return (global_config !== undefined &&
@@ -71,6 +73,7 @@ export class GlobalConfig {
71
73
  typeof global_config.scraping_bee_api_key === "string" &&
72
74
  typeof global_config.twitter_api_key === "string" &&
73
75
  typeof global_config.youtube_api_key === "string" &&
74
- typeof global_config.xai_api_key === "string");
76
+ typeof global_config.xai_api_key === "string" &&
77
+ typeof global_config.ballotpedia_api_key === "string");
75
78
  }
76
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "triangle-utils",
3
- "version": "1.4.161",
3
+ "version": "1.4.163",
4
4
  "main": "dist/src/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "directories": {
@@ -34,6 +34,7 @@
34
34
  "@types/nodemailer": "^7.0.9",
35
35
  "@types/pg": "^8.21.0",
36
36
  "@xdevplatform/xdk": "^0.5.0",
37
+ "csv-parser": "^3.2.1",
37
38
  "googleapis": "^170.0.0",
38
39
  "jsdom": "^29.0.1",
39
40
  "pg": "^8.23.0",
package/src/UtilsMisc.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import * as crypto from "crypto"
2
2
 
3
3
  import { GlobalConfig } from "./types/GlobalConfig"
4
+ import csv_parser from "csv-parser"
5
+ import { Readable } from "stream"
4
6
 
5
7
  interface Encryption {
6
8
  iv : string,
@@ -140,4 +142,36 @@ export class UtilsMisc {
140
142
  return secret_hash
141
143
  }
142
144
 
145
+ static async batch_import_csv<T>(stream : Readable, import_new_items : (new_items : any[]) => Promise<void>, options : { delimiter? : string, batch_num_items : number, max_num_items? : number }) {
146
+ const delimiter = options.delimiter || ","
147
+ const max_num_items = options.max_num_items
148
+ const batch_num_items = options.batch_num_items
149
+ let new_items : Promise<T[]>[] = []
150
+ let i = 0
151
+ const csv_stream = stream.pipe(csv_parser({ separator: delimiter }))
152
+ await new Promise<void>(resolve => {
153
+ csv_stream
154
+ .on("data", async (item) => {
155
+ i += 1
156
+ if (max_num_items !== undefined && i > max_num_items) {
157
+ csv_stream.end()
158
+ return
159
+ }
160
+ new_items.push(item)
161
+ if (i % batch_num_items === 0) {
162
+ csv_stream.pause()
163
+ console.log(i)
164
+ await import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()))
165
+ new_items = []
166
+ csv_stream.resume()
167
+ }
168
+ })
169
+ .on("end", async () => {
170
+ await import_new_items(await Promise.all(new_items).then(new_items => new_items.flat()))
171
+ new_items = []
172
+ resolve()
173
+ })
174
+ })
175
+ }
176
+
143
177
  }
package/src/index.ts CHANGED
@@ -53,7 +53,6 @@ export class TriangleUtils extends UtilsMisc {
53
53
 
54
54
  export * from "./types/ApplicationConfig"
55
55
  export * from "./types/GlobalConfig"
56
- export * from "./UtilsMisc"
57
56
  export * from "./UtilsAnthropic"
58
57
  export * from "./UtilsDynamoDB"
59
58
  export * from "./UtilsRDS"
@@ -21,6 +21,7 @@ export class GlobalConfig {
21
21
  readonly twitter_api_key : string
22
22
  readonly youtube_api_key : string
23
23
  readonly xai_api_key : string
24
+ readonly ballotpedia_api_key : string
24
25
  readonly [x : string] : any
25
26
 
26
27
  constructor(global_config : GlobalConfig) {
@@ -49,6 +50,7 @@ export class GlobalConfig {
49
50
  this.twitter_api_key = global_config.twitter_api_key
50
51
  this.youtube_api_key = global_config.youtube_api_key
51
52
  this.xai_api_key = global_config.xai_api_key
53
+ this.ballotpedia_api_key = global_config.ballotpedia_api_key
52
54
  }
53
55
 
54
56
  static is(global_config : any) : global_config is GlobalConfig {
@@ -75,7 +77,8 @@ export class GlobalConfig {
75
77
  typeof global_config.scraping_bee_api_key === "string" &&
76
78
  typeof global_config.twitter_api_key === "string" &&
77
79
  typeof global_config.youtube_api_key === "string" &&
78
- typeof global_config.xai_api_key === "string"
80
+ typeof global_config.xai_api_key === "string" &&
81
+ typeof global_config.ballotpedia_api_key === "string"
79
82
  )
80
83
  }
81
84
  }