@machinemetrics/mm-erp-sdk 0.1.7-beta.2 → 0.1.7-beta.3

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.
@@ -3995,14 +3995,14 @@ class SqlServerHelper {
3995
3995
  }
3996
3996
  }
3997
3997
  class PsqlService {
3998
+ connection = null;
3998
3999
  config;
3999
4000
  constructor(config2) {
4000
4001
  this.config = config2;
4001
4002
  }
4002
- // REMOVED: dispose() method - not needed anymore
4003
- // REMOVED: connection property - not needed anymore
4004
- // REMOVED: openConnection() method - not needed anymore
4005
- // REMOVED: closeConnection() method - not needed anymore
4003
+ async dispose() {
4004
+ await this.closeConnection();
4005
+ }
4006
4006
  /**
4007
4007
  * Build PSQL ODBC connection string
4008
4008
  * CRITICAL: ServerName must use IP.PORT format (e.g., 10.4.0.11.1583)
@@ -4020,7 +4020,7 @@ class PsqlService {
4020
4020
  }
4021
4021
  /**
4022
4022
  * Execute a query and return the results
4023
- * Creates a fresh connection for each query to avoid handle corruption
4023
+ * Interface matches SqlServerService for consistency
4024
4024
  *
4025
4025
  * @param query The SQL query to execute
4026
4026
  * @param params Query parameters (currently unused for PSQL read operations)
@@ -4028,58 +4028,73 @@ class PsqlService {
4028
4028
  * @returns The entities fetched from the database, along with paging information
4029
4029
  */
4030
4030
  async executePreparedStatement(query, params = {}, paging) {
4031
- let connection = null;
4031
+ const connection = await this.openConnection();
4032
+ let records;
4032
4033
  try {
4033
- const connStr = this.buildConnectionString();
4034
- logger.debug("Creating fresh PSQL connection for query");
4035
- connection = await odbc.connect(connStr);
4036
4034
  if (Object.keys(params).length > 0) {
4037
4035
  logger.warn(
4038
4036
  "PsqlService: Query parameters provided but parameter binding not yet implemented. Using direct query execution. This is acceptable for Phase 1 read operations."
4039
4037
  );
4040
4038
  }
4041
- const records = await connection.query(query);
4042
- const allRecords = PsqlService.recordsetToRecords(records);
4043
- const rowsFetched = allRecords.length;
4044
- const pagedData = paging?.offset !== void 0 || paging?.limit !== void 0 ? allRecords.slice(
4045
- paging.offset || 0,
4046
- (paging.offset || 0) + (paging.limit || allRecords.length)
4047
- ) : allRecords;
4048
- return {
4049
- data: pagedData,
4050
- paging: {
4051
- count: rowsFetched,
4052
- limit: paging?.limit || 0,
4053
- offset: paging?.offset || 0,
4054
- nextPage: paging?.limit && (paging.offset || 0) + paging.limit < rowsFetched ? String((paging.offset || 0) + paging.limit) : void 0,
4055
- previousPage: paging?.offset ? String(Math.max(0, (paging.offset || 0) - (paging.limit || 10))) : void 0
4056
- }
4057
- };
4039
+ records = await connection.query(query);
4058
4040
  } catch (error) {
4059
4041
  const errorInfo = error;
4060
4042
  logger.error("Error fetching data from PSQL", {
4061
4043
  error: errorInfo.message,
4062
- odbcErrors: errorInfo.odbcErrors,
4063
- query: query.substring(0, 200)
4064
- // Log first 200 chars of query
4044
+ odbcErrors: errorInfo.odbcErrors
4065
4045
  });
4066
4046
  throw this.handleOdbcError(errorInfo);
4067
- } finally {
4068
- if (connection) {
4069
- try {
4070
- await connection.close();
4071
- logger.debug("PSQL connection closed successfully");
4072
- } catch (err) {
4073
- logger.warn("Error closing PSQL connection (non-fatal)", {
4074
- error: err
4075
- });
4076
- }
4047
+ }
4048
+ const allRecords = PsqlService.recordsetToRecords(records);
4049
+ const rowsFetched = allRecords.length;
4050
+ const pagedData = paging?.offset !== void 0 || paging?.limit !== void 0 ? allRecords.slice(
4051
+ paging.offset || 0,
4052
+ (paging.offset || 0) + (paging.limit || allRecords.length)
4053
+ ) : allRecords;
4054
+ return {
4055
+ data: pagedData,
4056
+ paging: {
4057
+ count: rowsFetched,
4058
+ limit: paging?.limit || 0,
4059
+ offset: paging?.offset || 0,
4060
+ nextPage: paging?.limit && (paging.offset || 0) + paging.limit < rowsFetched ? String((paging.offset || 0) + paging.limit) : void 0,
4061
+ previousPage: paging?.offset ? String(Math.max(0, (paging.offset || 0) - (paging.limit || 10))) : void 0
4077
4062
  }
4063
+ };
4064
+ }
4065
+ /**
4066
+ * Opens a connection to PSQL database
4067
+ * Caches the connection so that it can be reused.
4068
+ * On failure to connect, throws
4069
+ */
4070
+ async openConnection() {
4071
+ if (this.connection) {
4072
+ logger.debug("Reusing existing PSQL connection");
4073
+ return this.connection;
4074
+ }
4075
+ try {
4076
+ const connStr = this.buildConnectionString();
4077
+ logger.info("Opening new PSQL connection");
4078
+ logger.debug(
4079
+ "Connection string (password hidden):",
4080
+ connStr.replace(/PWD=[^;]+/, "PWD=***")
4081
+ );
4082
+ this.connection = await odbc.connect(connStr);
4083
+ logger.info("Successfully connected to PSQL database");
4084
+ return this.connection;
4085
+ } catch (error) {
4086
+ logger.error("PsqlService>>openConnection>> Connection failed", {
4087
+ error
4088
+ });
4089
+ throw this.handleOdbcError(error);
4078
4090
  }
4079
4091
  }
4080
4092
  /**
4081
4093
  * Transform ODBC result set to array of Record<string, string> instances.
4082
4094
  * IMPORTANT: PSQL CHAR fields are often padded with spaces - we trim them
4095
+ *
4096
+ * @param recordset Result set from ODBC query
4097
+ * @returns array of Record<string, string> instances
4083
4098
  */
4084
4099
  static recordsetToRecords(recordset) {
4085
4100
  if (!Array.isArray(recordset)) {
@@ -4121,6 +4136,19 @@ class PsqlService {
4121
4136
  return new Error(`PSQL error (${errorCode || "unknown"}): ${message}`);
4122
4137
  }
4123
4138
  }
4139
+ async closeConnection() {
4140
+ if (this.connection) {
4141
+ logger.info("Closing PSQL connection");
4142
+ try {
4143
+ await this.connection.close();
4144
+ } catch (error) {
4145
+ logger.error("PsqlService::closeConnection: Error closing connection", {
4146
+ error
4147
+ });
4148
+ }
4149
+ this.connection = null;
4150
+ }
4151
+ }
4124
4152
  }
4125
4153
  class PsqlLaborTicketOperations {
4126
4154
  constructor(service) {