@machinemetrics/mm-erp-sdk 0.1.4 → 0.1.6-beta.0
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.
- package/dist/{config-WKwu1mMo.js → config-CWRevSxh.js} +2 -2
- package/dist/{config-WKwu1mMo.js.map → config-CWRevSxh.js.map} +1 -1
- package/dist/{connector-factory-DHmMYsRs.js → connector-factory-76kTjfE8.js} +2 -2
- package/dist/{connector-factory-DHmMYsRs.js.map → connector-factory-76kTjfE8.js.map} +1 -1
- package/dist/{hashed-cache-manager-CtDhFqj6.js → hashed-cache-manager-DW2y4SFu.js} +4 -4
- package/dist/{hashed-cache-manager-CtDhFqj6.js.map → hashed-cache-manager-DW2y4SFu.js.map} +1 -1
- package/dist/{index-aci_wdcn.js → index-DdqBliMK.js} +2 -2
- package/dist/{index-aci_wdcn.js.map → index-DdqBliMK.js.map} +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/{logger-hqtl8hFM.js → logger-nJCE--ZL.js} +46 -40
- package/dist/{logger-hqtl8hFM.js.map → logger-nJCE--ZL.js.map} +1 -1
- package/dist/mm-erp-sdk.js +13836 -141
- package/dist/mm-erp-sdk.js.map +1 -1
- package/dist/services/data-sync-service/jobs/clean-up-expired-cache.js +4 -4
- package/dist/services/data-sync-service/jobs/from-erp.js +4 -4
- package/dist/services/data-sync-service/jobs/retry-failed-labor-tickets.js +3 -3
- package/dist/services/data-sync-service/jobs/run-migrations.js +1 -1
- package/dist/services/data-sync-service/jobs/to-erp.js +3 -3
- package/dist/services/psql-erp-service/configuration.d.ts +10 -0
- package/dist/services/psql-erp-service/configuration.d.ts.map +1 -0
- package/dist/services/psql-erp-service/index.d.ts +19 -0
- package/dist/services/psql-erp-service/index.d.ts.map +1 -0
- package/dist/services/psql-erp-service/internal/psql-config.d.ts +28 -0
- package/dist/services/psql-erp-service/internal/psql-config.d.ts.map +1 -0
- package/dist/services/psql-erp-service/internal/psql-labor-ticket-operations.d.ts +40 -0
- package/dist/services/psql-erp-service/internal/psql-labor-ticket-operations.d.ts.map +1 -0
- package/dist/services/psql-erp-service/internal/types/psql-types.d.ts +15 -0
- package/dist/services/psql-erp-service/internal/types/psql-types.d.ts.map +1 -0
- package/dist/services/psql-erp-service/psql-helpers.d.ts +32 -0
- package/dist/services/psql-erp-service/psql-helpers.d.ts.map +1 -0
- package/dist/services/psql-erp-service/psql-service.d.ts +49 -0
- package/dist/services/psql-erp-service/psql-service.d.ts.map +1 -0
- package/dist/types/erp-types.d.ts +2 -1
- package/dist/types/erp-types.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/index.ts +27 -5
- package/src/services/psql-erp-service/configuration.ts +9 -0
- package/src/services/psql-erp-service/index.ts +28 -0
- package/src/services/psql-erp-service/internal/psql-config.ts +13 -0
- package/src/services/psql-erp-service/internal/psql-labor-ticket-operations.ts +58 -0
- package/src/services/psql-erp-service/internal/types/psql-types.ts +17 -0
- package/src/services/psql-erp-service/psql-helpers.ts +90 -0
- package/src/services/psql-erp-service/psql-service.ts +215 -0
- package/src/types/erp-types.ts +1 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import odbc from "odbc";
|
|
2
|
+
import { PsqlConfiguration } from "./configuration";
|
|
3
|
+
import { ERPResponse } from "../../types/erp-types";
|
|
4
|
+
import { OdbcErrorResponse } from "./internal/types/psql-types";
|
|
5
|
+
import logger from "../reporting-service/logger";
|
|
6
|
+
|
|
7
|
+
type PagingParams = {
|
|
8
|
+
limit?: number;
|
|
9
|
+
offset?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export class PsqlService {
|
|
13
|
+
private connection: odbc.Connection | null = null;
|
|
14
|
+
private config: PsqlConfiguration;
|
|
15
|
+
|
|
16
|
+
constructor(config: PsqlConfiguration) {
|
|
17
|
+
this.config = config;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async dispose() {
|
|
21
|
+
await this.closeConnection();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Build PSQL ODBC connection string
|
|
26
|
+
* CRITICAL: ServerName must use IP.PORT format (e.g., 10.4.0.11.1583)
|
|
27
|
+
*/
|
|
28
|
+
private buildConnectionString(): string {
|
|
29
|
+
// PSQL requires ServerName in format IP.PORT (not IP:PORT)
|
|
30
|
+
const serverName = `${this.config.host}.${this.config.port}`;
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
[
|
|
34
|
+
"Driver={Pervasive ODBC Interface}",
|
|
35
|
+
`ServerName=${serverName}`,
|
|
36
|
+
`DBQ=${this.config.database}`,
|
|
37
|
+
`UID=${this.config.username}`,
|
|
38
|
+
`PWD=${this.config.password}`,
|
|
39
|
+
"AutoDoubleQuote=0",
|
|
40
|
+
].join(";") + ";"
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Execute a query and return the results
|
|
46
|
+
* Interface matches SqlServerService for consistency
|
|
47
|
+
*
|
|
48
|
+
* @param query The SQL query to execute
|
|
49
|
+
* @param params Query parameters (currently unused for PSQL read operations)
|
|
50
|
+
* @param paging Optional paging parameters
|
|
51
|
+
* @returns The entities fetched from the database, along with paging information
|
|
52
|
+
*/
|
|
53
|
+
public async executePreparedStatement(
|
|
54
|
+
query: string,
|
|
55
|
+
params: Record<string, string> = {},
|
|
56
|
+
paging?: PagingParams
|
|
57
|
+
): Promise<ERPResponse | undefined> {
|
|
58
|
+
const connection = await this.openConnection();
|
|
59
|
+
|
|
60
|
+
let records;
|
|
61
|
+
try {
|
|
62
|
+
// For Phase 1 (read-only), we execute queries directly
|
|
63
|
+
// Phase 2 will add proper parameter binding for INSERT/UPDATE/DELETE
|
|
64
|
+
if (Object.keys(params).length > 0) {
|
|
65
|
+
logger.warn(
|
|
66
|
+
"PsqlService: Query parameters provided but parameter binding not yet implemented. " +
|
|
67
|
+
"Using direct query execution. This is acceptable for Phase 1 read operations."
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
records = await connection.query(query);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
const errorInfo = error as OdbcErrorResponse;
|
|
74
|
+
logger.error("Error fetching data from PSQL", {
|
|
75
|
+
error: errorInfo.message,
|
|
76
|
+
odbcErrors: errorInfo.odbcErrors,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
throw this.handleOdbcError(errorInfo);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const allRecords = PsqlService.recordsetToRecords(records);
|
|
83
|
+
const rowsFetched = allRecords.length;
|
|
84
|
+
|
|
85
|
+
// Apply paging if requested
|
|
86
|
+
const pagedData =
|
|
87
|
+
paging?.offset !== undefined || paging?.limit !== undefined
|
|
88
|
+
? allRecords.slice(
|
|
89
|
+
paging.offset || 0,
|
|
90
|
+
(paging.offset || 0) + (paging.limit || allRecords.length)
|
|
91
|
+
)
|
|
92
|
+
: allRecords;
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
data: pagedData,
|
|
96
|
+
paging: {
|
|
97
|
+
count: rowsFetched,
|
|
98
|
+
limit: paging?.limit || 0,
|
|
99
|
+
offset: paging?.offset || 0,
|
|
100
|
+
nextPage:
|
|
101
|
+
paging?.limit && (paging.offset || 0) + paging.limit < rowsFetched
|
|
102
|
+
? String((paging.offset || 0) + paging.limit)
|
|
103
|
+
: undefined,
|
|
104
|
+
previousPage: paging?.offset
|
|
105
|
+
? String(Math.max(0, (paging.offset || 0) - (paging.limit || 10)))
|
|
106
|
+
: undefined,
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Opens a connection to PSQL database
|
|
113
|
+
* Caches the connection so that it can be reused.
|
|
114
|
+
* On failure to connect, throws
|
|
115
|
+
*/
|
|
116
|
+
async openConnection(): Promise<odbc.Connection> {
|
|
117
|
+
// If we have a connection, reuse it
|
|
118
|
+
// Note: ODBC connections don't have a .connected property like SQL Server
|
|
119
|
+
// We'll keep it simple and reuse if not null
|
|
120
|
+
if (this.connection) {
|
|
121
|
+
logger.debug("Reusing existing PSQL connection");
|
|
122
|
+
return this.connection;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const connStr = this.buildConnectionString();
|
|
127
|
+
logger.info("Opening new PSQL connection");
|
|
128
|
+
logger.debug(
|
|
129
|
+
"Connection string (password hidden):",
|
|
130
|
+
connStr.replace(/PWD=[^;]+/, "PWD=***")
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
this.connection = await odbc.connect(connStr);
|
|
134
|
+
logger.info("Successfully connected to PSQL database");
|
|
135
|
+
return this.connection;
|
|
136
|
+
} catch (error) {
|
|
137
|
+
logger.error("PsqlService>>openConnection>> Connection failed", {
|
|
138
|
+
error,
|
|
139
|
+
});
|
|
140
|
+
throw this.handleOdbcError(error as OdbcErrorResponse);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Transform ODBC result set to array of Record<string, string> instances.
|
|
146
|
+
* IMPORTANT: PSQL CHAR fields are often padded with spaces - we trim them
|
|
147
|
+
*
|
|
148
|
+
* @param recordset Result set from ODBC query
|
|
149
|
+
* @returns array of Record<string, string> instances
|
|
150
|
+
*/
|
|
151
|
+
public static recordsetToRecords(recordset: any[]): Record<string, string>[] {
|
|
152
|
+
if (!Array.isArray(recordset)) {
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const data: Record<string, string>[] = recordset.map((row) => {
|
|
157
|
+
const transformedRow: Record<string, string> = {};
|
|
158
|
+
Object.keys(row).forEach((key) => {
|
|
159
|
+
const value = row[key];
|
|
160
|
+
// Convert to string and trim (PSQL CHAR fields have trailing spaces)
|
|
161
|
+
transformedRow[key] =
|
|
162
|
+
value !== null && value !== undefined ? String(value).trim() : "";
|
|
163
|
+
});
|
|
164
|
+
return transformedRow;
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
return data;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Handle ODBC errors and provide meaningful messages
|
|
172
|
+
*/
|
|
173
|
+
private handleOdbcError(error: OdbcErrorResponse): Error {
|
|
174
|
+
const odbcError = error.odbcErrors?.[0];
|
|
175
|
+
const errorCode = odbcError?.state;
|
|
176
|
+
const message = odbcError?.message || error.message;
|
|
177
|
+
|
|
178
|
+
switch (errorCode) {
|
|
179
|
+
case "08S01":
|
|
180
|
+
return new Error(
|
|
181
|
+
"PSQL connection failed. Check: " +
|
|
182
|
+
"1) PVSW environment variable set to /usr/local/psql/etc/pvsw.ini, " +
|
|
183
|
+
"2) Network connectivity to ports 1583/3351, " +
|
|
184
|
+
"3) ODBC configuration files in /usr/local/psql/etc/ and /etc/. " +
|
|
185
|
+
`Original error: ${message}`
|
|
186
|
+
);
|
|
187
|
+
case "28000":
|
|
188
|
+
return new Error(
|
|
189
|
+
`PSQL authentication failed. Check username/password. Original error: ${message}`
|
|
190
|
+
);
|
|
191
|
+
case "42000":
|
|
192
|
+
return new Error(`PSQL SQL syntax error. Original error: ${message}`);
|
|
193
|
+
case "42S02":
|
|
194
|
+
return new Error(
|
|
195
|
+
`PSQL table or view not found. Check table names in query. Original error: ${message}`
|
|
196
|
+
);
|
|
197
|
+
default:
|
|
198
|
+
return new Error(`PSQL error (${errorCode || "unknown"}): ${message}`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private async closeConnection(): Promise<void> {
|
|
203
|
+
if (this.connection) {
|
|
204
|
+
logger.info("Closing PSQL connection");
|
|
205
|
+
try {
|
|
206
|
+
await this.connection.close();
|
|
207
|
+
} catch (error) {
|
|
208
|
+
logger.error("PsqlService::closeConnection: Error closing connection", {
|
|
209
|
+
error,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
this.connection = null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|