@toxplanet/pegasus-sdk 1.1.18 → 1.1.19

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,29 +1,29 @@
1
- module.exports = {
2
- environment: 'dev',
3
- region: 'us-east-1',
4
- awsAccountId: '292931567094',
5
- sourceService: 'pegasus-sdk',
6
- secretName: 'arn:aws:secretsmanager:us-east-1:292931567094:secret:rds!cluster-b851c3ce-58cc-41cd-aeae-05cc7f5e031a-ZYSjiI',
7
- openSearchEndpoint: 'https://war8lk73nzswquk8dcz1.us-east-1.aoss.amazonaws.com',
8
- openSearchIndex: 'chemicals',
9
- database: {
10
- host: 'cr-chemicals.cluster-cz0iqdg8irhb.us-east-1.rds.amazonaws.com',
11
- name: 'chemicals'
12
- },
13
- postgres: {
14
- maxConnections: 2,
15
- minConnections: 1,
16
- idleTimeoutMillis: 0,
17
- connectionTimeoutMillis: 15000,
18
- statementTimeout: 30000,
19
- queryTimeout: 30000,
20
- ssl: {
21
- rejectUnauthorized: false
22
- }
23
- },
24
- indexRoutes: {
25
- chemicals: ['chemicals*'],
26
- documents: ['documents*'],
27
- search: [/^(chemicals|substances|search)/]
28
- }
29
- };
1
+ module.exports = {
2
+ environment: 'dev',
3
+ region: 'us-east-1',
4
+ awsAccountId: '292931567094',
5
+ sourceService: 'pegasus-sdk',
6
+ secretName: 'arn:aws:secretsmanager:us-east-1:292931567094:secret:rds!cluster-b851c3ce-58cc-41cd-aeae-05cc7f5e031a-ZYSjiI',
7
+ openSearchEndpoint: 'https://war8lk73nzswquk8dcz1.us-east-1.aoss.amazonaws.com',
8
+ openSearchIndex: 'chemicals',
9
+ database: {
10
+ host: 'cr-chemicals.cluster-cz0iqdg8irhb.us-east-1.rds.amazonaws.com',
11
+ name: 'chemicals'
12
+ },
13
+ postgres: {
14
+ maxConnections: 2,
15
+ minConnections: 1,
16
+ idleTimeoutMillis: 0,
17
+ connectionTimeoutMillis: 15000,
18
+ statementTimeout: 30000,
19
+ queryTimeout: 30000,
20
+ ssl: {
21
+ rejectUnauthorized: false
22
+ }
23
+ },
24
+ indexRoutes: {
25
+ chemicals: ['chemicals*'],
26
+ documents: ['documents*'],
27
+ search: [/^(chemicals|substances|search)/]
28
+ }
29
+ };
package/lib/chemicals.js CHANGED
@@ -21,8 +21,9 @@ class ChemicalsService {
21
21
  this.sqsClient = null;
22
22
  }
23
23
 
24
- getDb() {
25
- if (!this.db) {
24
+ async getDb() {
25
+ const reconnected = await this.connection.ensureConnected();
26
+ if (reconnected || !this.db) {
26
27
  this.db = getDrizzle(this.connection.pgPool);
27
28
  }
28
29
  return this.db;
@@ -142,15 +143,6 @@ class ChemicalsService {
142
143
  return { indexed: 0, errors: [], results: [] };
143
144
  }
144
145
 
145
- // Proactively validate the connection before any real query fires.
146
- // If idle too long, this reconnects first so the real query never faces
147
- // the full connectionTimeoutMillis wait on a stale pool.
148
- const reconnected = await this.connection.ensureConnected();
149
- if (reconnected) {
150
- this.db = null; // force getDb() to bind to the fresh pool
151
- }
152
-
153
- const db = this.getDb();
154
146
  const results = [];
155
147
  const errors = [];
156
148
 
@@ -189,8 +181,11 @@ class ChemicalsService {
189
181
  err.code === 'ECONNREFUSED' ||
190
182
  err.code === 'ETIMEDOUT';
191
183
 
192
- const attemptUpsert = () =>
193
- db.insert(schema.chemicals)
184
+ // Use this.getDb() on each attempt so a reconnect mid-loop automatically
185
+ // gets a fresh Drizzle instance bound to the new pool.
186
+ const attemptUpsert = async () => {
187
+ const freshDb = await this.getDb();
188
+ return freshDb.insert(schema.chemicals)
194
189
  .values(chemical)
195
190
  .onConflictDoUpdate({
196
191
  target: schema.chemicals.sourceId,
@@ -207,6 +202,7 @@ class ChemicalsService {
207
202
  chemicalId: schema.chemicals.chemicalId,
208
203
  sourceId: schema.chemicals.sourceId
209
204
  });
205
+ };
210
206
 
211
207
  let lastError = null;
212
208
  let retryCount = 0;
@@ -226,7 +222,6 @@ class ChemicalsService {
226
222
  logInfo('pegasus-sdk', `[bulkIndexFielded] Document ${i} connection error (${firstErr.message}), reconnecting pool and retrying`);
227
223
  try {
228
224
  await this.connection.reconnect();
229
- this.db = null; // force getDb() to bind to the new pool
230
225
  const [result] = await attemptUpsert();
231
226
  logInfo('pegasus-sdk', `[bulkIndexFielded] Document ${i} indexed successfully after reconnect: ${result?.chemicalId || 'no ID returned'}`);
232
227
  this.connection.recordActivity();
@@ -315,7 +310,7 @@ class ChemicalsService {
315
310
 
316
311
  async createChemical(chemical) {
317
312
  try {
318
- const db = this.getDb();
313
+ const db = await this.getDb();
319
314
 
320
315
  const [result] = await db
321
316
  .insert(schema.chemicals)
@@ -342,7 +337,7 @@ class ChemicalsService {
342
337
 
343
338
  async updateChemical(chemicalId, updates) {
344
339
  try {
345
- const db = this.getDb();
340
+ const db = await this.getDb();
346
341
 
347
342
  const updateData = {};
348
343
  if (updates.chemical_name) updateData.chemicalName = updates.chemical_name;
@@ -367,7 +362,7 @@ class ChemicalsService {
367
362
 
368
363
  async deleteChemical(chemicalId) {
369
364
  try {
370
- const db = this.getDb();
365
+ const db = await this.getDb();
371
366
 
372
367
  const [deleted] = await db
373
368
  .delete(schema.chemicals)
@@ -383,7 +378,7 @@ class ChemicalsService {
383
378
 
384
379
  async deleteBySourceId(sourceId) {
385
380
  try {
386
- const db = this.getDb();
381
+ const db = await this.getDb();
387
382
 
388
383
  const [deleted] = await db
389
384
  .delete(schema.chemicals)
@@ -399,7 +394,7 @@ class ChemicalsService {
399
394
 
400
395
  async deleteCollection(collectionName) {
401
396
  try {
402
- const db = this.getDb();
397
+ const db = await this.getDb();
403
398
 
404
399
  const deleted = await db
405
400
  .delete(schema.chemicals)
@@ -415,7 +410,7 @@ class ChemicalsService {
415
410
 
416
411
  async updateCollectionProperty(collectionName, propertyPath, newValue) {
417
412
  try {
418
- const db = this.getDb();
413
+ const db = await this.getDb();
419
414
  const pathArray = propertyPath.split('.');
420
415
  const valueJson = JSON.stringify(newValue);
421
416
 
@@ -437,7 +432,7 @@ class ChemicalsService {
437
432
 
438
433
  async bulkUpdateProperty(filter, propertyPath, newValue) {
439
434
  try {
440
- const db = this.getDb();
435
+ const db = await this.getDb();
441
436
 
442
437
  let whereCondition = sql`1=1`;
443
438
 
@@ -470,7 +465,7 @@ class ChemicalsService {
470
465
 
471
466
  async getChemicalById(chemicalId) {
472
467
  try {
473
- const db = this.getDb();
468
+ const db = await this.getDb();
474
469
 
475
470
  const [result] = await db
476
471
  .select()
@@ -487,7 +482,7 @@ class ChemicalsService {
487
482
 
488
483
  async getChemicalBySourceId(sourceId) {
489
484
  try {
490
- const db = this.getDb();
485
+ const db = await this.getDb();
491
486
 
492
487
  const [result] = await db
493
488
  .select()
@@ -504,7 +499,7 @@ class ChemicalsService {
504
499
 
505
500
  async getChemicalsByCAS(casNumber) {
506
501
  try {
507
- const db = this.getDb();
502
+ const db = await this.getDb();
508
503
 
509
504
  const results = await db
510
505
  .select()
@@ -524,7 +519,7 @@ class ChemicalsService {
524
519
  throw new Error(`Invalid identifier type: ${identifierType}`);
525
520
  }
526
521
 
527
- const db = this.getDb();
522
+ const db = await this.getDb();
528
523
 
529
524
  const results = await db
530
525
  .select()
@@ -540,7 +535,7 @@ class ChemicalsService {
540
535
 
541
536
  async countByCollection(collectionName) {
542
537
  try {
543
- const db = this.getDb();
538
+ const db = await this.getDb();
544
539
 
545
540
  const result = await db
546
541
  .select({ count: sql`count(*)::int` })
@@ -556,7 +551,7 @@ class ChemicalsService {
556
551
 
557
552
  async countByIdentifier(identifierValue) {
558
553
  try {
559
- const db = this.getDb();
554
+ const db = await this.getDb();
560
555
 
561
556
  const searchPattern = `%${escapeLikePattern(identifierValue)}%`;
562
557
  const result = await db
@@ -573,7 +568,7 @@ class ChemicalsService {
573
568
 
574
569
  async countByCAS(casNumber) {
575
570
  try {
576
- const db = this.getDb();
571
+ const db = await this.getDb();
577
572
 
578
573
  const result = await db
579
574
  .select({ count: sql`count(*)::int` })
@@ -589,7 +584,7 @@ class ChemicalsService {
589
584
 
590
585
  async getTotalSynonymCount() {
591
586
  try {
592
- const db = this.getDb();
587
+ const db = await this.getDb();
593
588
 
594
589
  const result = await db
595
590
  .select({ count: sql`sum(array_length(${schema.chemicals.chemicalSynonyms}, 1))::int` })
@@ -604,7 +599,7 @@ class ChemicalsService {
604
599
 
605
600
  async getSynonymCount(synonymTerm) {
606
601
  try {
607
- const db = this.getDb();
602
+ const db = await this.getDb();
608
603
 
609
604
  const result = await db
610
605
  .select({ count: sql`count(*)::int` })
@@ -620,7 +615,7 @@ class ChemicalsService {
620
615
 
621
616
  async convertIdentifier(fromIdentifier, toIdentifierType) {
622
617
  try {
623
- const db = this.getDb();
618
+ const db = await this.getDb();
624
619
 
625
620
  const searchPattern = `%${escapeLikePattern(fromIdentifier)}%`;
626
621
  const chemicals = await db
@@ -768,7 +763,7 @@ class ChemicalsService {
768
763
 
769
764
  async countAll() {
770
765
  try {
771
- const db = this.getDb();
766
+ const db = await this.getDb();
772
767
  const result = await db
773
768
  .select({ count: sql`count(*)::int` })
774
769
  .from(schema.chemicals);
@@ -781,7 +776,7 @@ class ChemicalsService {
781
776
 
782
777
  async findChemicalsWithoutDocuments(collectionName, searchTerm, pageSize = 100) {
783
778
  try {
784
- const db = this.getDb();
779
+ const db = await this.getDb();
785
780
 
786
781
  let whereConditions = [];
787
782
 
@@ -811,7 +806,7 @@ class ChemicalsService {
811
806
 
812
807
  async countChemicalsWithoutDocuments(collectionName) {
813
808
  try {
814
- const db = this.getDb();
809
+ const db = await this.getDb();
815
810
 
816
811
  const whereClause = collectionName
817
812
  ? arrayContains(schema.chemicals.chemicalCategories, [collectionName])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toxplanet/pegasus-sdk",
3
- "version": "1.1.18",
3
+ "version": "1.1.19",
4
4
  "description": "SDK for migrating chemical data to Pegasus PostgreSQL + OpenSearch architecture with Elasticsearch client compatibility",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",