@toxplanet/pegasus-sdk 1.1.24 → 1.1.25

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.
Files changed (2) hide show
  1. package/lib/chemicals.js +52 -8
  2. package/package.json +1 -1
package/lib/chemicals.js CHANGED
@@ -39,6 +39,42 @@ function parsePostgresArray(str) {
39
39
  return result;
40
40
  }
41
41
 
42
+ function transformChemicalMeta(meta) {
43
+ if (!meta || typeof meta !== 'object') return [];
44
+ if (Array.isArray(meta)) {
45
+ // If it's already in new format, return as-is
46
+ if (meta.length > 0 && meta[0].key !== undefined) {
47
+ return meta;
48
+ }
49
+ // Transform from old format { meta_key, meta_value_text, meta_value_type, ... } to new format { key, value, [unit] }
50
+ return meta.map(item => {
51
+ const transformed = {
52
+ key: item.meta_key || item.key,
53
+ value: item.meta_value_text || item.value || []
54
+ };
55
+ if (item.unit) transformed.unit = item.unit;
56
+ return transformed;
57
+ });
58
+ }
59
+ return [];
60
+ }
61
+
62
+ function transformChemicalIdentifiers(identifiers) {
63
+ if (!identifiers || typeof identifiers !== 'object') return [];
64
+ if (Array.isArray(identifiers)) {
65
+ // If it's already in new format, return as-is
66
+ if (identifiers.length > 0 && identifiers[0].type !== undefined) {
67
+ return identifiers;
68
+ }
69
+ // Transform from old format { identifier_key, identifier_value, ... } to new format { type, value }
70
+ return identifiers.map(item => ({
71
+ type: item.identifier_key || item.type,
72
+ value: Array.isArray(item.identifier_value) ? item.identifier_value[0] : (item.value || item.identifier_value)
73
+ }));
74
+ }
75
+ return [];
76
+ }
77
+
42
78
  class ChemicalsService {
43
79
  constructor(connection) {
44
80
  this.connection = connection;
@@ -64,8 +100,8 @@ class ChemicalsService {
64
100
  chemicalId: row.chemical_id,
65
101
  sourceId: row.source_id,
66
102
  chemicalName: row.chemical_name,
67
- chemicalMeta: row.chemical_meta ? JSON.parse(row.chemical_meta) : null,
68
- chemicalIdentifiers: row.chemical_identifiers ? JSON.parse(row.chemical_identifiers) : null,
103
+ chemicalMeta: row.chemical_meta ? (Array.isArray(row.chemical_meta) ? row.chemical_meta : JSON.parse(row.chemical_meta)) : null,
104
+ chemicalIdentifiers: row.chemical_identifiers ? (Array.isArray(row.chemical_identifiers) ? row.chemical_identifiers : JSON.parse(row.chemical_identifiers)) : null,
69
105
  chemicalSynonyms: this._parsePostgresArray(row.chemical_synonyms),
70
106
  chemicalCategories: this._parsePostgresArray(row.chemical_categories),
71
107
  createdAt: row.created_at,
@@ -132,11 +168,14 @@ class ChemicalsService {
132
168
  'RETURNING chemical_id, source_id'
133
169
  ].join('\n');
134
170
 
171
+ const transformedMeta = transformChemicalMeta(chemical.chemicalMeta);
172
+ const transformedIdentifiers = transformChemicalIdentifiers(chemical.chemicalIdentifiers);
173
+
135
174
  const parameters = [
136
175
  { name: 'source_id', value: { stringValue: chemical.sourceId } },
137
176
  { name: 'chemical_name', value: { stringValue: chemical.chemicalName } },
138
- { name: 'chemical_meta', value: { stringValue: JSON.stringify(chemical.chemicalMeta ?? {}) }, typeHint: 'JSON' },
139
- { name: 'chemical_identifiers', value: { stringValue: JSON.stringify(chemical.chemicalIdentifiers ?? {}) }, typeHint: 'JSON' },
177
+ { name: 'chemical_meta', value: { stringValue: JSON.stringify(transformedMeta) }, typeHint: 'JSON' },
178
+ { name: 'chemical_identifiers', value: { stringValue: JSON.stringify(transformedIdentifiers) }, typeHint: 'JSON' },
140
179
  { name: 'chemical_synonyms', value: { stringValue: this._toPostgresArray(chemical.chemicalSynonyms ?? []) } },
141
180
  { name: 'chemical_categories', value: { stringValue: this._toPostgresArray(chemical.chemicalCategories ?? []) } },
142
181
  { name: 'created_at', value: { stringValue: this._serializeDate(chemical.createdAt) } },
@@ -326,13 +365,16 @@ class ChemicalsService {
326
365
  try {
327
366
  await this.connection.ensureConnected();
328
367
 
368
+ const transformedMeta = transformChemicalMeta(chemical.chemical_meta);
369
+ const transformedIdentifiers = transformChemicalIdentifiers(chemical.chemical_identifiers);
370
+
329
371
  const columns = ['source_id', 'chemical_name', 'chemical_meta', 'chemical_identifiers', 'chemical_synonyms', 'chemical_categories', 'created_at', 'updated_at'];
330
372
  const values = [':source_id', ':chemical_name', ':chemical_meta::jsonb', ':chemical_identifiers::jsonb', ':chemical_synonyms::text[]', ':chemical_categories::text[]', ':created_at::timestamp', ':updated_at::timestamp'];
331
373
  const params = [
332
374
  { name: 'source_id', value: { stringValue: chemical.source_id } },
333
375
  { name: 'chemical_name', value: { stringValue: chemical.chemical_name } },
334
- { name: 'chemical_meta', value: { stringValue: JSON.stringify(chemical.chemical_meta || {}) }, typeHint: 'JSON' },
335
- { name: 'chemical_identifiers', value: { stringValue: JSON.stringify(chemical.chemical_identifiers || {}) }, typeHint: 'JSON' },
376
+ { name: 'chemical_meta', value: { stringValue: JSON.stringify(transformedMeta) }, typeHint: 'JSON' },
377
+ { name: 'chemical_identifiers', value: { stringValue: JSON.stringify(transformedIdentifiers) }, typeHint: 'JSON' },
336
378
  { name: 'chemical_synonyms', value: { stringValue: this._toPostgresArray(chemical.chemical_synonyms || []) } },
337
379
  { name: 'chemical_categories', value: { stringValue: this._toPostgresArray(chemical.chemical_categories || []) } },
338
380
  { name: 'created_at', value: { stringValue: this._serializeDate(chemical.created_at || new Date()) } },
@@ -372,12 +414,14 @@ class ChemicalsService {
372
414
  params.push({ name: 'chemical_name', value: { stringValue: updates.chemical_name } });
373
415
  }
374
416
  if (updates.chemical_meta) {
417
+ const transformedMeta = transformChemicalMeta(updates.chemical_meta);
375
418
  setClauses.push('chemical_meta = :chemical_meta::jsonb');
376
- params.push({ name: 'chemical_meta', value: { stringValue: JSON.stringify(updates.chemical_meta) }, typeHint: 'JSON' });
419
+ params.push({ name: 'chemical_meta', value: { stringValue: JSON.stringify(transformedMeta) }, typeHint: 'JSON' });
377
420
  }
378
421
  if (updates.chemical_identifiers) {
422
+ const transformedIdentifiers = transformChemicalIdentifiers(updates.chemical_identifiers);
379
423
  setClauses.push('chemical_identifiers = :chemical_identifiers::jsonb');
380
- params.push({ name: 'chemical_identifiers', value: { stringValue: JSON.stringify(updates.chemical_identifiers) }, typeHint: 'JSON' });
424
+ params.push({ name: 'chemical_identifiers', value: { stringValue: JSON.stringify(transformedIdentifiers) }, typeHint: 'JSON' });
381
425
  }
382
426
  if (updates.chemical_synonyms) {
383
427
  setClauses.push('chemical_synonyms = :chemical_synonyms::text[]');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toxplanet/pegasus-sdk",
3
- "version": "1.1.24",
3
+ "version": "1.1.25",
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",