@machinemetrics/mm-erp-sdk 0.1.7-beta.1 → 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.
@@ -3623,12 +3623,7 @@ const runDataSyncService = async (connectorPath) => {
3623
3623
  CACHE_TTL: process.env.CACHE_TTL,
3624
3624
  // Pass through all other environment variables that might be needed
3625
3625
  ...process.env
3626
- },
3627
- // Add worker options for better isolation and cleanup
3628
- execArgv: [
3629
- "--expose-gc"
3630
- // Allow manual garbage collection
3631
- ]
3626
+ }
3632
3627
  },
3633
3628
  jobs: [
3634
3629
  // {
@@ -4000,14 +3995,14 @@ class SqlServerHelper {
4000
3995
  }
4001
3996
  }
4002
3997
  class PsqlService {
3998
+ connection = null;
4003
3999
  config;
4004
4000
  constructor(config2) {
4005
4001
  this.config = config2;
4006
4002
  }
4007
- // REMOVED: dispose() method - not needed anymore
4008
- // REMOVED: connection property - not needed anymore
4009
- // REMOVED: openConnection() method - not needed anymore
4010
- // REMOVED: closeConnection() method - not needed anymore
4003
+ async dispose() {
4004
+ await this.closeConnection();
4005
+ }
4011
4006
  /**
4012
4007
  * Build PSQL ODBC connection string
4013
4008
  * CRITICAL: ServerName must use IP.PORT format (e.g., 10.4.0.11.1583)
@@ -4025,7 +4020,7 @@ class PsqlService {
4025
4020
  }
4026
4021
  /**
4027
4022
  * Execute a query and return the results
4028
- * Creates a fresh connection for each query to avoid handle corruption
4023
+ * Interface matches SqlServerService for consistency
4029
4024
  *
4030
4025
  * @param query The SQL query to execute
4031
4026
  * @param params Query parameters (currently unused for PSQL read operations)
@@ -4033,58 +4028,73 @@ class PsqlService {
4033
4028
  * @returns The entities fetched from the database, along with paging information
4034
4029
  */
4035
4030
  async executePreparedStatement(query, params = {}, paging) {
4036
- let connection = null;
4031
+ const connection = await this.openConnection();
4032
+ let records;
4037
4033
  try {
4038
- const connStr = this.buildConnectionString();
4039
- logger.debug("Creating fresh PSQL connection for query");
4040
- connection = await odbc.connect(connStr);
4041
4034
  if (Object.keys(params).length > 0) {
4042
4035
  logger.warn(
4043
4036
  "PsqlService: Query parameters provided but parameter binding not yet implemented. Using direct query execution. This is acceptable for Phase 1 read operations."
4044
4037
  );
4045
4038
  }
4046
- const records = await connection.query(query);
4047
- const allRecords = PsqlService.recordsetToRecords(records);
4048
- const rowsFetched = allRecords.length;
4049
- const pagedData = paging?.offset !== void 0 || paging?.limit !== void 0 ? allRecords.slice(
4050
- paging.offset || 0,
4051
- (paging.offset || 0) + (paging.limit || allRecords.length)
4052
- ) : allRecords;
4053
- return {
4054
- data: pagedData,
4055
- paging: {
4056
- count: rowsFetched,
4057
- limit: paging?.limit || 0,
4058
- offset: paging?.offset || 0,
4059
- nextPage: paging?.limit && (paging.offset || 0) + paging.limit < rowsFetched ? String((paging.offset || 0) + paging.limit) : void 0,
4060
- previousPage: paging?.offset ? String(Math.max(0, (paging.offset || 0) - (paging.limit || 10))) : void 0
4061
- }
4062
- };
4039
+ records = await connection.query(query);
4063
4040
  } catch (error) {
4064
4041
  const errorInfo = error;
4065
4042
  logger.error("Error fetching data from PSQL", {
4066
4043
  error: errorInfo.message,
4067
- odbcErrors: errorInfo.odbcErrors,
4068
- query: query.substring(0, 200)
4069
- // Log first 200 chars of query
4044
+ odbcErrors: errorInfo.odbcErrors
4070
4045
  });
4071
4046
  throw this.handleOdbcError(errorInfo);
4072
- } finally {
4073
- if (connection) {
4074
- try {
4075
- await connection.close();
4076
- logger.debug("PSQL connection closed successfully");
4077
- } catch (err) {
4078
- logger.warn("Error closing PSQL connection (non-fatal)", {
4079
- error: err
4080
- });
4081
- }
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
4082
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);
4083
4090
  }
4084
4091
  }
4085
4092
  /**
4086
4093
  * Transform ODBC result set to array of Record<string, string> instances.
4087
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
4088
4098
  */
4089
4099
  static recordsetToRecords(recordset) {
4090
4100
  if (!Array.isArray(recordset)) {
@@ -4126,6 +4136,19 @@ class PsqlService {
4126
4136
  return new Error(`PSQL error (${errorCode || "unknown"}): ${message}`);
4127
4137
  }
4128
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
+ }
4129
4152
  }
4130
4153
  class PsqlLaborTicketOperations {
4131
4154
  constructor(service) {