@machinemetrics/mm-erp-sdk 0.1.8-beta.7 → 0.1.8-beta.8

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 (63) hide show
  1. package/README.md +2 -8
  2. package/dist/{config-152LkbTv.js → config-qat9zgOl.js} +2 -2
  3. package/dist/{config-152LkbTv.js.map → config-qat9zgOl.js.map} +1 -1
  4. package/dist/{connector-factory-wivcyMhC.js → connector-factory-C2czCs9v.js} +2 -2
  5. package/dist/{connector-factory-wivcyMhC.js.map → connector-factory-C2czCs9v.js.map} +1 -1
  6. package/dist/{hashed-cache-manager-BnviJzB7.js → hashed-cache-manager-CzyFSt2B.js} +4 -4
  7. package/dist/{hashed-cache-manager-BnviJzB7.js.map → hashed-cache-manager-CzyFSt2B.js.map} +1 -1
  8. package/dist/{index-DNqHWa8F.js → index-B9wo8pld.js} +2 -2
  9. package/dist/{index-DNqHWa8F.js.map → index-B9wo8pld.js.map} +1 -1
  10. package/dist/index.d.ts +4 -6
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/{logger-HAWySEbs.js → logger-Db8CkwR6.js} +11 -83
  13. package/dist/{logger-HAWySEbs.js.map → logger-Db8CkwR6.js.map} +1 -1
  14. package/dist/mm-erp-sdk.js +6 -234
  15. package/dist/mm-erp-sdk.js.map +1 -1
  16. package/dist/services/data-sync-service/jobs/clean-up-expired-cache.js +4 -4
  17. package/dist/services/data-sync-service/jobs/from-erp.js +4 -4
  18. package/dist/services/data-sync-service/jobs/retry-failed-labor-tickets.js +3 -3
  19. package/dist/services/data-sync-service/jobs/run-migrations.js +1 -1
  20. package/dist/services/data-sync-service/jobs/to-erp.d.ts.map +1 -1
  21. package/dist/services/data-sync-service/jobs/to-erp.js +3 -3
  22. package/dist/services/data-sync-service/jobs/to-erp.js.map +1 -1
  23. package/dist/services/mm-api-service/mm-api-service.d.ts +4 -4
  24. package/dist/services/mm-api-service/mm-api-service.d.ts.map +1 -1
  25. package/dist/services/mm-api-service/types/checkpoint.d.ts +1 -2
  26. package/dist/services/mm-api-service/types/checkpoint.d.ts.map +1 -1
  27. package/dist/services/reporting-service/logger.d.ts.map +1 -1
  28. package/dist/types/erp-connector.d.ts +4 -2
  29. package/dist/types/erp-connector.d.ts.map +1 -1
  30. package/dist/types/erp-types.d.ts +0 -10
  31. package/dist/types/erp-types.d.ts.map +1 -1
  32. package/dist/types/index.d.ts +1 -1
  33. package/dist/types/index.d.ts.map +1 -1
  34. package/dist/utils/standard-process-drivers/labor-ticket-erp-synchronizer.d.ts +2 -3
  35. package/dist/utils/standard-process-drivers/labor-ticket-erp-synchronizer.d.ts.map +1 -1
  36. package/dist/utils/standard-process-drivers/standard-process-drivers.d.ts +3 -3
  37. package/dist/utils/standard-process-drivers/standard-process-drivers.d.ts.map +1 -1
  38. package/package.json +1 -4
  39. package/src/index.ts +6 -27
  40. package/src/services/data-sync-service/jobs/to-erp.ts +1 -2
  41. package/src/services/mm-api-service/mm-api-service.ts +4 -4
  42. package/src/services/mm-api-service/types/checkpoint.ts +1 -3
  43. package/src/services/reporting-service/logger.ts +11 -94
  44. package/src/types/erp-connector.ts +5 -2
  45. package/src/types/erp-types.ts +0 -12
  46. package/src/types/index.ts +0 -1
  47. package/src/utils/standard-process-drivers/labor-ticket-erp-synchronizer.ts +2 -3
  48. package/src/utils/standard-process-drivers/standard-process-drivers.ts +3 -3
  49. package/dist/services/psql-erp-service/configuration.d.ts +0 -10
  50. package/dist/services/psql-erp-service/configuration.d.ts.map +0 -1
  51. package/dist/services/psql-erp-service/index.d.ts +0 -14
  52. package/dist/services/psql-erp-service/index.d.ts.map +0 -1
  53. package/dist/services/psql-erp-service/internal/types/psql-types.d.ts +0 -12
  54. package/dist/services/psql-erp-service/internal/types/psql-types.d.ts.map +0 -1
  55. package/dist/services/psql-erp-service/psql-helpers.d.ts +0 -32
  56. package/dist/services/psql-erp-service/psql-helpers.d.ts.map +0 -1
  57. package/dist/services/psql-erp-service/psql-service.d.ts +0 -43
  58. package/dist/services/psql-erp-service/psql-service.d.ts.map +0 -1
  59. package/src/services/psql-erp-service/configuration.ts +0 -9
  60. package/src/services/psql-erp-service/index.ts +0 -22
  61. package/src/services/psql-erp-service/internal/types/psql-types.ts +0 -13
  62. package/src/services/psql-erp-service/psql-helpers.ts +0 -90
  63. package/src/services/psql-erp-service/psql-service.ts +0 -240
@@ -1,10 +0,0 @@
1
- export interface PsqlConfiguration {
2
- host: string;
3
- port: string;
4
- database: string;
5
- username: string;
6
- password: string;
7
- connectionTimeout?: string;
8
- requestTimeout?: string;
9
- }
10
- //# sourceMappingURL=configuration.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"configuration.d.ts","sourceRoot":"","sources":["../../../src/services/psql-erp-service/configuration.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB"}
@@ -1,14 +0,0 @@
1
- import { PsqlService } from "./psql-service";
2
- /**
3
- * A class to manage interactions with PSQL (Pervasive) databases via ODBC
4
- */
5
- export { PsqlService };
6
- /**
7
- * Configuration interface for PSQL connections
8
- */
9
- export type { PsqlConfiguration } from "./configuration";
10
- /**
11
- * Helper functions for PSQL data formatting
12
- */
13
- export { formatPsqlDate, formatPsqlTime, combinePsqlDateTime, isPsqlDateEmpty, cleanPsqlCharField, } from "./psql-helpers";
14
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/psql-erp-service/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C;;GAEG;AACH,OAAO,EAAE,WAAW,EAAE,CAAC;AAEvB;;GAEG;AACH,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEzD;;GAEG;AACH,OAAO,EACL,cAAc,EACd,cAAc,EACd,mBAAmB,EACnB,eAAe,EACf,kBAAkB,GACnB,MAAM,gBAAgB,CAAC"}
@@ -1,12 +0,0 @@
1
- /**
2
- * PSQL-specific type definitions
3
- */
4
- export interface OdbcError {
5
- state: string;
6
- message: string;
7
- code?: number;
8
- }
9
- export interface OdbcErrorResponse extends Error {
10
- odbcErrors?: OdbcError[];
11
- }
12
- //# sourceMappingURL=psql-types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"psql-types.d.ts","sourceRoot":"","sources":["../../../../../src/services/psql-erp-service/internal/types/psql-types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,iBAAkB,SAAQ,KAAK;IAC9C,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;CAC1B"}
@@ -1,32 +0,0 @@
1
- /**
2
- * Helper functions for PSQL/Pervasive database operations
3
- */
4
- /**
5
- * Formats a date from PSQL YYMMDD format to ISO date string
6
- * @param psqlDate Date in YYMMDD format (e.g., "250105" for Jan 5, 2025)
7
- * @returns ISO date string (e.g., "2025-01-05") or null if invalid
8
- */
9
- export declare function formatPsqlDate(psqlDate: string): string | null;
10
- /**
11
- * Formats a time from PSQL HHMM format to HH:MM:SS
12
- * @param psqlTime Time in HHMM format (e.g., "1430" for 2:30 PM)
13
- * @returns Time string in HH:MM:SS format or null if invalid
14
- */
15
- export declare function formatPsqlTime(psqlTime: string): string | null;
16
- /**
17
- * Combines PSQL date and time into ISO datetime string
18
- * @param psqlDate Date in YYMMDD format
19
- * @param psqlTime Time in HHMM format
20
- * @returns ISO datetime string or null if invalid
21
- */
22
- export declare function combinePsqlDateTime(psqlDate: string, psqlTime: string): string | null;
23
- /**
24
- * Helper to check if a PSQL date is "empty" (000000 or blank)
25
- */
26
- export declare function isPsqlDateEmpty(psqlDate: string): boolean;
27
- /**
28
- * Clean and trim PSQL CHAR field (removes trailing spaces)
29
- * PSQL CHAR fields are fixed-width and padded with spaces
30
- */
31
- export declare function cleanPsqlCharField(value: string | null | undefined): string;
32
- //# sourceMappingURL=psql-helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"psql-helpers.d.ts","sourceRoot":"","sources":["../../../src/services/psql-erp-service/psql-helpers.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAuB9D;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAY9D;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,MAAM,GAAG,IAAI,CASf;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAEzD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAK3E"}
@@ -1,43 +0,0 @@
1
- import { PsqlConfiguration } from "./configuration";
2
- import { ERPResponse } from "../../types/erp-types";
3
- type PagingParams = {
4
- limit?: number;
5
- offset?: number;
6
- };
7
- export declare class PsqlService {
8
- private config;
9
- private static odbcModule;
10
- private static odbcLoadError;
11
- constructor(config: PsqlConfiguration);
12
- /**
13
- * Dynamically load the ODBC module with lazy initialization and caching
14
- * @throws Error with helpful message if ODBC package is not installed
15
- */
16
- private static getOdbc;
17
- /**
18
- * Build PSQL ODBC connection string
19
- * CRITICAL: ServerName must use IP.PORT format (e.g., 10.4.0.11.1583)
20
- */
21
- private buildConnectionString;
22
- /**
23
- * Execute a query and return the results
24
- * Creates a fresh connection for each query to avoid handle corruption
25
- *
26
- * @param query The SQL query to execute
27
- * @param params Query parameters (currently unused for PSQL read operations)
28
- * @param paging Optional paging parameters
29
- * @returns The entities fetched from the database, along with paging information
30
- */
31
- executePreparedStatement(query: string, params?: Record<string, string>, paging?: PagingParams): Promise<ERPResponse | undefined>;
32
- /**
33
- * Transform ODBC result set to array of Record<string, string> instances.
34
- * IMPORTANT: PSQL CHAR fields are often padded with spaces - we trim them
35
- */
36
- static recordsetToRecords(recordset: any[]): Record<string, string>[];
37
- /**
38
- * Handle ODBC errors and provide meaningful messages
39
- */
40
- private handleOdbcError;
41
- }
42
- export {};
43
- //# sourceMappingURL=psql-service.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"psql-service.d.ts","sourceRoot":"","sources":["../../../src/services/psql-erp-service/psql-service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAIpD,KAAK,YAAY,GAAG;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAiBF,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAoB;IAClC,OAAO,CAAC,MAAM,CAAC,UAAU,CAA2B;IACpD,OAAO,CAAC,MAAM,CAAC,aAAa,CAAsB;gBAEtC,MAAM,EAAE,iBAAiB;IAIrC;;;OAGG;mBACkB,OAAO;IAsC5B;;;OAGG;IACH,OAAO,CAAC,qBAAqB;IAe7B;;;;;;;;OAQG;IACU,wBAAwB,CACnC,KAAK,EAAE,MAAM,EACb,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,EACnC,MAAM,CAAC,EAAE,YAAY,GACpB,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC;IA8EnC;;;OAGG;WACW,kBAAkB,CAAC,SAAS,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE;IAkB5E;;OAEG;IACH,OAAO,CAAC,eAAe;CA4BxB"}
@@ -1,9 +0,0 @@
1
- export interface PsqlConfiguration {
2
- host: string;
3
- port: string;
4
- database: string;
5
- username: string;
6
- password: string;
7
- connectionTimeout?: string;
8
- requestTimeout?: string;
9
- }
@@ -1,22 +0,0 @@
1
- import { PsqlService } from "./psql-service";
2
-
3
- /**
4
- * A class to manage interactions with PSQL (Pervasive) databases via ODBC
5
- */
6
- export { PsqlService };
7
-
8
- /**
9
- * Configuration interface for PSQL connections
10
- */
11
- export type { PsqlConfiguration } from "./configuration";
12
-
13
- /**
14
- * Helper functions for PSQL data formatting
15
- */
16
- export {
17
- formatPsqlDate,
18
- formatPsqlTime,
19
- combinePsqlDateTime,
20
- isPsqlDateEmpty,
21
- cleanPsqlCharField,
22
- } from "./psql-helpers";
@@ -1,13 +0,0 @@
1
- /**
2
- * PSQL-specific type definitions
3
- */
4
-
5
- export interface OdbcError {
6
- state: string;
7
- message: string;
8
- code?: number;
9
- }
10
-
11
- export interface OdbcErrorResponse extends Error {
12
- odbcErrors?: OdbcError[];
13
- }
@@ -1,90 +0,0 @@
1
- /**
2
- * Helper functions for PSQL/Pervasive database operations
3
- */
4
-
5
- /**
6
- * Formats a date from PSQL YYMMDD format to ISO date string
7
- * @param psqlDate Date in YYMMDD format (e.g., "250105" for Jan 5, 2025)
8
- * @returns ISO date string (e.g., "2025-01-05") or null if invalid
9
- */
10
- export function formatPsqlDate(psqlDate: string): string | null {
11
- if (!psqlDate || psqlDate === "000000" || psqlDate.trim() === "") {
12
- return null;
13
- }
14
-
15
- try {
16
- const year = parseInt(psqlDate.substring(0, 2), 10);
17
- const month = parseInt(psqlDate.substring(2, 4), 10);
18
- const day = parseInt(psqlDate.substring(4, 6), 10);
19
-
20
- // Convert 2-digit year to 4-digit (assuming 2000s)
21
- const fullYear = year + 2000;
22
-
23
- // Basic validation
24
- if (month < 1 || month > 12 || day < 1 || day > 31) {
25
- return null;
26
- }
27
-
28
- const date = new Date(fullYear, month - 1, day);
29
- return date.toISOString().split("T")[0];
30
- } catch (error) {
31
- return null;
32
- }
33
- }
34
-
35
- /**
36
- * Formats a time from PSQL HHMM format to HH:MM:SS
37
- * @param psqlTime Time in HHMM format (e.g., "1430" for 2:30 PM)
38
- * @returns Time string in HH:MM:SS format or null if invalid
39
- */
40
- export function formatPsqlTime(psqlTime: string): string | null {
41
- if (!psqlTime || psqlTime.trim() === "") {
42
- return null;
43
- }
44
-
45
- try {
46
- const hours = psqlTime.substring(0, 2);
47
- const minutes = psqlTime.substring(2, 4);
48
- return `${hours}:${minutes}:00`;
49
- } catch (error) {
50
- return null;
51
- }
52
- }
53
-
54
- /**
55
- * Combines PSQL date and time into ISO datetime string
56
- * @param psqlDate Date in YYMMDD format
57
- * @param psqlTime Time in HHMM format
58
- * @returns ISO datetime string or null if invalid
59
- */
60
- export function combinePsqlDateTime(
61
- psqlDate: string,
62
- psqlTime: string
63
- ): string | null {
64
- const date = formatPsqlDate(psqlDate);
65
- const time = formatPsqlTime(psqlTime);
66
-
67
- if (!date || !time) {
68
- return null;
69
- }
70
-
71
- return `${date}T${time}`;
72
- }
73
-
74
- /**
75
- * Helper to check if a PSQL date is "empty" (000000 or blank)
76
- */
77
- export function isPsqlDateEmpty(psqlDate: string): boolean {
78
- return !psqlDate || psqlDate === "000000" || psqlDate.trim() === "";
79
- }
80
-
81
- /**
82
- * Clean and trim PSQL CHAR field (removes trailing spaces)
83
- * PSQL CHAR fields are fixed-width and padded with spaces
84
- */
85
- export function cleanPsqlCharField(value: string | null | undefined): string {
86
- if (value === null || value === undefined) {
87
- return "";
88
- }
89
- return String(value).trim();
90
- }
@@ -1,240 +0,0 @@
1
- import { PsqlConfiguration } from "./configuration";
2
- import { ERPResponse } from "../../types/erp-types";
3
- import { OdbcErrorResponse } from "./internal/types/psql-types";
4
- import logger from "../reporting-service/logger";
5
-
6
- type PagingParams = {
7
- limit?: number;
8
- offset?: number;
9
- };
10
-
11
- /**
12
- * ODBC connection interface for type safety
13
- */
14
- interface OdbcConnection {
15
- query(sql: string): Promise<any[]>;
16
- close(): Promise<void>;
17
- }
18
-
19
- /**
20
- * ODBC module interface
21
- */
22
- interface OdbcModule {
23
- connect(connectionString: string): Promise<OdbcConnection>;
24
- }
25
-
26
- export class PsqlService {
27
- private config: PsqlConfiguration;
28
- private static odbcModule: OdbcModule | null = null;
29
- private static odbcLoadError: Error | null = null;
30
-
31
- constructor(config: PsqlConfiguration) {
32
- this.config = config;
33
- }
34
-
35
- /**
36
- * Dynamically load the ODBC module with lazy initialization and caching
37
- * @throws Error with helpful message if ODBC package is not installed
38
- */
39
- private static async getOdbc(): Promise<OdbcModule> {
40
- // If we've already tried and failed, throw the cached error
41
- if (this.odbcLoadError) {
42
- throw this.odbcLoadError;
43
- }
44
-
45
- // If already loaded, return cached module
46
- if (this.odbcModule) {
47
- return this.odbcModule;
48
- }
49
-
50
- try {
51
- // Dynamic import - only loads when actually needed
52
- // @ts-ignore - odbc is an optional dependency, may not be installed at build time
53
- const odbcImport = await import("odbc");
54
- // Handle both default export and named export patterns
55
- const odbc = odbcImport.default || odbcImport;
56
- this.odbcModule = odbc as OdbcModule;
57
- return this.odbcModule;
58
- } catch (error) {
59
- const errorMessage = error instanceof Error ? error.message : String(error);
60
- this.odbcLoadError = new Error(
61
- `ODBC package is required for PSQL service but is not installed or failed to load.\n` +
62
- `Install it with: npm install odbc\n` +
63
- `Also install OS-level dependencies, e.g. on Alpine Linux:\n` +
64
- ` apk add --no-cache unixodbc unixodbc-dev python3 make g++\n` +
65
- `For other Linux distributions, install unixodbc and unixodbc-dev packages.\n` +
66
- `Original error: ${errorMessage}`
67
- );
68
- throw this.odbcLoadError;
69
- }
70
- }
71
-
72
- // REMOVED: dispose() method - not needed anymore
73
- // REMOVED: connection property - not needed anymore
74
- // REMOVED: openConnection() method - not needed anymore
75
- // REMOVED: closeConnection() method - not needed anymore
76
-
77
- /**
78
- * Build PSQL ODBC connection string
79
- * CRITICAL: ServerName must use IP.PORT format (e.g., 10.4.0.11.1583)
80
- */
81
- private buildConnectionString(): string {
82
- const serverName = `${this.config.host}.${this.config.port}`;
83
-
84
- return (
85
- [
86
- "Driver={Pervasive ODBC Interface}",
87
- `ServerName=${serverName}`,
88
- `DBQ=${this.config.database}`,
89
- `UID=${this.config.username}`,
90
- `PWD=${this.config.password}`,
91
- "AutoDoubleQuote=0",
92
- ].join(";") + ";"
93
- );
94
- }
95
-
96
- /**
97
- * Execute a query and return the results
98
- * Creates a fresh connection for each query to avoid handle corruption
99
- *
100
- * @param query The SQL query to execute
101
- * @param params Query parameters (currently unused for PSQL read operations)
102
- * @param paging Optional paging parameters
103
- * @returns The entities fetched from the database, along with paging information
104
- */
105
- public async executePreparedStatement(
106
- query: string,
107
- params: Record<string, string> = {},
108
- paging?: PagingParams
109
- ): Promise<ERPResponse | undefined> {
110
- // Dynamically load ODBC module (will throw helpful error if not installed)
111
- const odbc = await PsqlService.getOdbc();
112
- let connection: OdbcConnection | null = null;
113
-
114
- try {
115
- // Create fresh connection for THIS query only
116
- const connStr = this.buildConnectionString();
117
- logger.debug("Creating fresh PSQL connection for query");
118
- connection = await odbc.connect(connStr);
119
-
120
- if (Object.keys(params).length > 0) {
121
- logger.warn(
122
- "PsqlService: Query parameters provided but parameter binding not yet implemented. " +
123
- "Using direct query execution."
124
- );
125
- }
126
-
127
- const records = await connection.query(query);
128
- const allRecords = PsqlService.recordsetToRecords(records);
129
- const rowsFetched = allRecords.length;
130
-
131
- // Apply paging if requested
132
- const pagedData =
133
- paging?.offset !== undefined || paging?.limit !== undefined
134
- ? allRecords.slice(
135
- paging.offset || 0,
136
- (paging.offset || 0) + (paging.limit || allRecords.length)
137
- )
138
- : allRecords;
139
-
140
- return {
141
- data: pagedData,
142
- paging: {
143
- count: rowsFetched,
144
- limit: paging?.limit || 0,
145
- offset: paging?.offset || 0,
146
- nextPage:
147
- paging?.limit && (paging.offset || 0) + paging.limit < rowsFetched
148
- ? String((paging.offset || 0) + paging.limit)
149
- : undefined,
150
- previousPage: paging?.offset
151
- ? String(Math.max(0, (paging.offset || 0) - (paging.limit || 10)))
152
- : undefined,
153
- },
154
- };
155
- } catch (error) {
156
- // If this is an ODBC load error (from getOdbc), re-throw it as-is
157
- // since it already has a helpful error message
158
- if (error instanceof Error && error.message.includes("ODBC package is required")) {
159
- throw error;
160
- }
161
-
162
- // Otherwise, handle as ODBC runtime error
163
- const errorInfo = error as OdbcErrorResponse;
164
- logger.error("Error fetching data from PSQL", {
165
- error: errorInfo.message,
166
- odbcErrors: errorInfo.odbcErrors,
167
- query: query.substring(0, 200), // Log first 200 chars of query
168
- });
169
-
170
- throw this.handleOdbcError(errorInfo);
171
- } finally {
172
- // CRITICAL: Always close connection, even on error
173
- if (connection) {
174
- try {
175
- await connection.close();
176
- logger.debug("PSQL connection closed successfully");
177
- } catch (err) {
178
- // Don't throw on close errors, just log
179
- logger.warn("Error closing PSQL connection (non-fatal)", {
180
- error: err,
181
- });
182
- }
183
- }
184
- }
185
- }
186
-
187
- /**
188
- * Transform ODBC result set to array of Record<string, string> instances.
189
- * IMPORTANT: PSQL CHAR fields are often padded with spaces - we trim them
190
- */
191
- public static recordsetToRecords(recordset: any[]): Record<string, string>[] {
192
- if (!Array.isArray(recordset)) {
193
- return [];
194
- }
195
-
196
- const data: Record<string, string>[] = recordset.map((row) => {
197
- const transformedRow: Record<string, string> = {};
198
- Object.keys(row).forEach((key) => {
199
- const value = row[key];
200
- transformedRow[key] =
201
- value !== null && value !== undefined ? String(value).trim() : "";
202
- });
203
- return transformedRow;
204
- });
205
-
206
- return data;
207
- }
208
-
209
- /**
210
- * Handle ODBC errors and provide meaningful messages
211
- */
212
- private handleOdbcError(error: OdbcErrorResponse): Error {
213
- const odbcError = error.odbcErrors?.[0];
214
- const errorCode = odbcError?.state;
215
- const message = odbcError?.message || error.message;
216
-
217
- switch (errorCode) {
218
- case "08S01":
219
- return new Error(
220
- "PSQL connection failed. Check: " +
221
- "1) PVSW environment variable set to /usr/local/psql/etc/pvsw.ini, " +
222
- "2) Network connectivity to ports 1583/3351, " +
223
- "3) ODBC configuration files in /usr/local/psql/etc/ and /etc/. " +
224
- `Original error: ${message}`
225
- );
226
- case "28000":
227
- return new Error(
228
- `PSQL authentication failed. Check username/password. Original error: ${message}`
229
- );
230
- case "42000":
231
- return new Error(`PSQL SQL syntax error. Original error: ${message}`);
232
- case "42S02":
233
- return new Error(
234
- `PSQL table or view not found. Check table names in query. Original error: ${message}`
235
- );
236
- default:
237
- return new Error(`PSQL error (${errorCode || "unknown"}): ${message}`);
238
- }
239
- }
240
- }