@nsshunt/stsfhirpg 1.2.27 → 1.2.29

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/index.cjs CHANGED
@@ -6,7 +6,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __commonJSMin = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
9
+ var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
10
10
  var __copyProps = (to, from, except, desc) => {
11
11
  if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
12
  key = keys[i];
@@ -3272,7 +3272,8 @@ var HashUtils = class HashUtils {
3272
3272
  return input.trim().replace(/\s+/g, " ").toLowerCase();
3273
3273
  };
3274
3274
  InitHash = async () => {
3275
- this.h64 = (await e()).h64;
3275
+ const api = await e();
3276
+ this.h64 = api.h64;
3276
3277
  };
3277
3278
  xxhash64Signed(input) {
3278
3279
  if (this.h64) return BigInt.asIntN(64, this.h64(input, this.HASH_SEED));
@@ -4813,19 +4814,46 @@ var PGPoolManager = class extends tiny_emitter.TinyEmitter {
4813
4814
  }
4814
4815
  };
4815
4816
  //#endregion
4817
+ //#region src/fhir-database/pg/pgfhirdbbase.ts
4818
+ var DBSTSFhirDBBase = class {
4819
+ _options;
4820
+ _poolManager;
4821
+ constructor(options) {
4822
+ this._options = options;
4823
+ this._poolManager = options.poolManager;
4824
+ }
4825
+ async withClient(fname, fn) {
4826
+ let client;
4827
+ try {
4828
+ client = await this._poolManager.connectReadWrite();
4829
+ } catch (error) {
4830
+ this._options.logger.error(`${fname}(): DB: Connection Error: [${error}]`);
4831
+ throw error;
4832
+ }
4833
+ try {
4834
+ return await fn(client);
4835
+ } catch (error) {
4836
+ this._options.logger.error(`${fname}(): DB: Query Error: [${error}]`);
4837
+ throw error;
4838
+ } finally {
4839
+ client.release();
4840
+ }
4841
+ }
4842
+ };
4843
+ //#endregion
4816
4844
  //#region src/fhir-database/pg/pgfhirresource.ts
4817
- var DBSTSResource = class {
4818
- #poolManager;
4819
- constructor(poolManager) {
4820
- this.#poolManager = poolManager;
4845
+ var DBSTSResource = class extends DBSTSFhirDBBase {
4846
+ constructor(options) {
4847
+ super(options);
4821
4848
  }
4822
4849
  async InsertResourceEx(client, data) {
4823
4850
  try {
4824
- await client.query(`
4851
+ return (await client.query(`
4825
4852
  INSERT INTO stsresfhir (
4826
4853
  PID, PARTITION_ID, PARTITION_DATE, RES_ID, RES_TYPE,
4827
4854
  RES_VER, RES_VERSION, RES_PUBLISHED, RES_UPDATED
4828
4855
  ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
4856
+ RETURNING RES_VER
4829
4857
  `, [
4830
4858
  data.PID,
4831
4859
  data.PARTITION_ID,
@@ -4836,146 +4864,90 @@ var DBSTSResource = class {
4836
4864
  data.RES_VERSION,
4837
4865
  BigInt(data.RES_PUBLISHED),
4838
4866
  BigInt(data.RES_UPDATED)
4839
- ]);
4867
+ ])).rows[0]?.res_ver;
4840
4868
  } catch (error) {
4841
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:InsertResource(): Query Error: [${error}]`);
4869
+ this._options.logger.error(`DBSTSResource:InsertResource(): Query Error: [${error}]`);
4842
4870
  throw error;
4843
4871
  }
4844
4872
  }
4845
4873
  async InsertResource(data) {
4846
- try {
4847
- const client = await this.#poolManager.connectReadWrite();
4848
- try {
4849
- await this.InsertResourceEx(client, data);
4850
- } catch (error) {
4851
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:InsertResource(): Query Error: [${error}]`);
4852
- } finally {
4853
- client.release();
4854
- }
4855
- } catch (error) {
4856
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:InsertResource(): Connection Error: [${error}]`);
4857
- }
4874
+ return this.withClient("InsertResource", ((client) => this.InsertResourceEx(client, data)));
4858
4875
  }
4859
4876
  async UpdateResourceRecordEx(client, data) {
4860
4877
  try {
4861
- const res = await client.query(`
4878
+ return (await client.query(`
4862
4879
  UPDATE stsresfhir SET
4863
4880
  RES_VER = RES_VER + 1,
4864
4881
  RES_UPDATED = $1,
4865
4882
  RES_DELETED_AT = NULL
4866
4883
  WHERE RES_ID = $2 AND RES_TYPE = $3 AND RES_VER = $4
4884
+ RETURNING RES_VER
4867
4885
  `, [
4868
4886
  BigInt(data.RES_UPDATED),
4869
4887
  data.RES_ID,
4870
4888
  data.RES_TYPE,
4871
4889
  data.RES_VER
4872
- ]);
4873
- return res.rowCount ? res.rowCount > 0 : false;
4890
+ ])).rows[0]?.res_ver;
4874
4891
  } catch (error) {
4875
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:UpdateResourceRecordEx(): Query Error: [${error}]`);
4892
+ this._options.logger.error(`DBSTSResource:UpdateResourceRecordEx(): Query Error: [${error}]`);
4876
4893
  throw error;
4877
4894
  }
4878
4895
  }
4879
4896
  async UpdateResourceRecord(data) {
4880
- try {
4881
- const client = await this.#poolManager.connectReadWrite();
4882
- try {
4883
- return await this.UpdateResourceRecordEx(client, data);
4884
- } catch (error) {
4885
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:UpdateResourceRecord-1(): Query Error: [${error}]`);
4886
- } finally {
4887
- client.release();
4888
- }
4889
- } catch (error) {
4890
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:UpdateResourceRecord-2(): Connection Error: [${error}]`);
4891
- }
4897
+ return this.withClient("UpdateResourceRecord", ((client) => this.UpdateResourceRecordEx(client, data)));
4892
4898
  }
4893
4899
  async DeleteResourceRecordEx(client, data) {
4894
4900
  try {
4895
- const res = await client.query(`
4901
+ return (await client.query(`
4896
4902
  UPDATE stsresfhir SET
4897
4903
  RES_DELETED_AT = $1,
4898
4904
  RES_VER = RES_VER + 1,
4899
4905
  RES_UPDATED = $2
4900
4906
  WHERE RES_ID = $3 AND RES_TYPE = $4 AND RES_VER = $5
4907
+ RETURNING RES_VER
4901
4908
  `, [
4902
4909
  BigInt(data.RES_DELETED_AT),
4903
4910
  BigInt(data.RES_UPDATED),
4904
4911
  data.RES_ID,
4905
4912
  data.RES_TYPE,
4906
4913
  data.RES_VER
4907
- ]);
4908
- return res.rowCount ? res.rowCount > 0 : false;
4914
+ ])).rows[0]?.res_ver;
4909
4915
  } catch (error) {
4910
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:DeleteResourceRecord(): Query Error: [${error}]`);
4916
+ this._options.logger.error(`DBSTSResource:DeleteResourceRecord(): Query Error: [${error}]`);
4911
4917
  throw error;
4912
4918
  }
4913
4919
  }
4914
4920
  async DeleteResourceRecord(data) {
4915
- try {
4916
- const client = await this.#poolManager.connectReadWrite();
4917
- try {
4918
- return await this.DeleteResourceRecordEx(client, data);
4919
- } catch (error) {
4920
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:DeleteResourceRecord(): Query Error: [${error}]`);
4921
- } finally {
4922
- client.release();
4923
- }
4924
- } catch (error) {
4925
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:DeleteResourceRecord(): Connection Error: [${error}]`);
4926
- }
4921
+ return this.withClient("DeleteResourceRecord", ((client) => this.DeleteResourceRecordEx(client, data)));
4927
4922
  }
4928
4923
  async ReadResourceRecordEx(client, PID) {
4929
4924
  try {
4930
4925
  return (await client.query(`SELECT * FROM stsresfhir WHERE PID = $1`, [PID])).rows[0];
4931
4926
  } catch (error) {
4932
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadResourceRecord(): Query Error: [${error}]`);
4927
+ this._options.logger.error(`DBSTSResource:ReadResourceRecord(): Query Error: [${error}]`);
4933
4928
  throw error;
4934
4929
  }
4935
4930
  }
4936
4931
  async ReadResourceRecord(PID) {
4937
- try {
4938
- const client = await this.#poolManager.connectReadOnly();
4939
- try {
4940
- return await this.ReadResourceRecordEx(client, PID);
4941
- } catch (error) {
4942
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadResourceRecord(): Query Error: [${error}]`);
4943
- } finally {
4944
- client.release();
4945
- }
4946
- } catch (error) {
4947
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadResourceRecord(): Connection Error: [${error}]`);
4948
- }
4932
+ return this.withClient("ReadResourceRecord", ((client) => this.ReadResourceRecordEx(client, PID)));
4949
4933
  }
4950
4934
  async ReadAllResourceRecordsEx(client) {
4951
4935
  try {
4952
4936
  return (await client.query(`SELECT * FROM stsresfhir`)).rows;
4953
4937
  } catch (error) {
4954
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadAllResourceRecords(): Query Error: [${error}]`);
4938
+ this._options.logger.error(`DBSTSResource:ReadAllResourceRecords(): Query Error: [${error}]`);
4955
4939
  throw error;
4956
4940
  }
4957
4941
  }
4958
4942
  async ReadAllResourceRecords() {
4959
- try {
4960
- const client = await this.#poolManager.connectReadOnly();
4961
- try {
4962
- return await this.ReadAllResourceRecordsEx(client);
4963
- } catch (error) {
4964
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadAllResourceRecords(): Query Error: [${error}]`);
4965
- } finally {
4966
- client.release();
4967
- }
4968
- } catch (error) {
4969
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadAllResourceRecords(): Connection Error: [${error}]`);
4970
- }
4943
+ return this.withClient("ReadAllResourceRecords", ((client) => this.ReadAllResourceRecordsEx(client)));
4971
4944
  }
4972
4945
  };
4973
4946
  //#endregion
4974
4947
  //#region src/fhir-database/pg/pgfhirresourceversion.ts
4975
- var DBSTSResourceVersion = class {
4976
- #poolManager;
4977
- constructor(poolManager) {
4978
- this.#poolManager = poolManager;
4948
+ var DBSTSResourceVersion = class extends DBSTSFhirDBBase {
4949
+ constructor(options) {
4950
+ super(options);
4979
4951
  }
4980
4952
  async InsertResourceVersionEx(client, data) {
4981
4953
  const values = [
@@ -4989,81 +4961,49 @@ var DBSTSResourceVersion = class {
4989
4961
  data.RES
4990
4962
  ];
4991
4963
  try {
4992
- await client.query(`
4964
+ return (await client.query(`
4993
4965
  INSERT INTO stsresfhirver (
4994
4966
  PID, PARTITION_ID, PARTITION_DATE,
4995
4967
  RES_ID, RES_TYPE, RES_VER, OPERATION, RES
4996
4968
  ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
4997
- `, values);
4969
+ RETURNING RES_VER
4970
+ `, values)).rows[0]?.res_ver;
4998
4971
  } catch (error) {
4999
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): Query Error: [${error}]`);
5000
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values PID: [${values[0]}]`);
5001
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values PARTITION_ID: [${values[1]}]`);
5002
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values PARTITION_DATE: [${values[2]}]`);
5003
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values RES_ID: [${values[3]}]`);
5004
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values RES_TYPE: [${values[4]}]`);
5005
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values RES_VER: [${values[5]}]`);
5006
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values OPERATION: [${values[6]}]`);
4972
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): Query Error: [${error}]`);
4973
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values PID: [${values[0]}]`);
4974
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values PARTITION_ID: [${values[1]}]`);
4975
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values PARTITION_DATE: [${values[2]}]`);
4976
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values RES_ID: [${values[3]}]`);
4977
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values RES_TYPE: [${values[4]}]`);
4978
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values RES_VER: [${values[5]}]`);
4979
+ this._options.logger.error(`DBSTSResourceVersion:InsertResourceVersion(): --> Values OPERATION: [${values[6]}]`);
5007
4980
  throw error;
5008
4981
  }
5009
4982
  }
5010
4983
  async InsertResourceVersion(data) {
5011
- try {
5012
- const client = await this.#poolManager.connectReadWrite();
5013
- try {
5014
- await this.InsertResourceVersionEx(client, data);
5015
- } catch (error) {
5016
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): Query Error: [${error}]`);
5017
- } finally {
5018
- client.release();
5019
- }
5020
- } catch (error) {
5021
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:InsertResourceVersion(): Connection Error: [${error}]`);
5022
- }
4984
+ return this.withClient("InsertResourceVersion", ((client) => this.InsertResourceVersionEx(client, data)));
5023
4985
  }
5024
4986
  async ReadResourceVersionRecordEx(client, PID, RES_VER) {
5025
4987
  try {
5026
4988
  return (await client.query(`SELECT * FROM stsresfhirver WHERE PID = $1 AND RES_VER = $2`, [PID, RES_VER])).rows[0];
5027
4989
  } catch (error) {
5028
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:ReadResourceVersionRecord(): Query Error: [${error}]`);
4990
+ this._options.logger.error(`DBSTSResourceVersion:ReadResourceVersionRecord(): Query Error: [${error}]`);
5029
4991
  throw error;
5030
4992
  }
5031
4993
  }
5032
4994
  async ReadResourceVersionRecord(PID, RES_VER) {
5033
- try {
5034
- const client = await this.#poolManager.connectReadWrite();
5035
- try {
5036
- return await this.ReadResourceVersionRecordEx(client, PID, RES_VER);
5037
- } catch (error) {
5038
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:ReadResourceVersionRecord(): Query Error: [${error}]`);
5039
- } finally {
5040
- client.release();
5041
- }
5042
- } catch (error) {
5043
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:ReadResourceVersionRecord(): Connection Error: [${error}]`);
5044
- }
4995
+ return this.withClient("ReadResourceVersionRecord", ((client) => this.ReadResourceVersionRecordEx(client, PID, RES_VER)));
5045
4996
  }
5046
4997
  async ReadAllResourceVersionRecordsEx(client) {
5047
4998
  try {
5048
4999
  return (await client.query(`SELECT * FROM stsresfhirver`)).rows;
5049
5000
  } catch (error) {
5050
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:ReadAllResourceVersionRecords(): Query Error: [${error}]`);
5001
+ this._options.logger.error(`DBSTSResourceVersion:ReadAllResourceVersionRecords(): Query Error: [${error}]`);
5051
5002
  throw error;
5052
5003
  }
5053
5004
  }
5054
5005
  async ReadAllResourceVersionRecords() {
5055
- try {
5056
- const client = await this.#poolManager.connectReadWrite();
5057
- try {
5058
- return await this.ReadAllResourceVersionRecordsEx(client);
5059
- } catch (error) {
5060
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:ReadAllResourceVersionRecords(): Query Error: [${error}]`);
5061
- } finally {
5062
- client.release();
5063
- }
5064
- } catch (error) {
5065
- _nsshunt_stsutils.defaultLogger.error(`DBSTSResourceVersion:ReadAllResourceVersionRecords(): Connection Error: [${error}]`);
5066
- }
5006
+ return this.withClient("ReadAllResourceVersionRecords", ((client) => this.ReadAllResourceVersionRecordsEx(client)));
5067
5007
  }
5068
5008
  };
5069
5009
  //#endregion
@@ -5849,8 +5789,14 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
5849
5789
  logger: this.#options.logger,
5850
5790
  usedefaultdb: this.#options.usedefaultdb
5851
5791
  });
5852
- this.#dbSTSResource = new DBSTSResource(this.#poolManager);
5853
- this.#dbSTSResourceVersion = new DBSTSResourceVersion(this.#poolManager);
5792
+ this.#dbSTSResource = new DBSTSResource({
5793
+ logger: this.#options.logger,
5794
+ poolManager: this.#poolManager
5795
+ });
5796
+ this.#dbSTSResourceVersion = new DBSTSResourceVersion({
5797
+ logger: this.#options.logger,
5798
+ poolManager: this.#poolManager
5799
+ });
5854
5800
  this.#dbSearchIndex = DBSearchIndex.getInstance();
5855
5801
  this.#dbSTSResourceLink = new DBSTSResourceLink();
5856
5802
  this.#dbSTSString = new DBSTSString();
@@ -5861,6 +5807,16 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
5861
5807
  this.#dbSTSUri = new DBSTSUri();
5862
5808
  this.#dbSTSCombo = new DBSTSCombo();
5863
5809
  }
5810
+ GetOperationOutcome = (code, diagnostics) => {
5811
+ return {
5812
+ resourceType: "OperationOutcome",
5813
+ issue: [{
5814
+ severity: "error",
5815
+ code,
5816
+ diagnostics
5817
+ }]
5818
+ };
5819
+ };
5864
5820
  async StartDatabase() {
5865
5821
  return true;
5866
5822
  }
@@ -6046,14 +6002,14 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6046
6002
  return false;
6047
6003
  };
6048
6004
  CreateResource = async (resource, transactionHandle) => {
6049
- if (this.#poolManager) try {
6050
- let PK = (0, node_crypto.randomUUID)();
6051
- const RES_ID = resource.id;
6052
- const RES_TYPE = resource.resourceType;
6053
- const RES = JSON.stringify(resource);
6054
- const now = (/* @__PURE__ */ new Date()).getTime();
6055
- let client;
6056
- let useTransaction;
6005
+ if (!this.#poolManager) {
6006
+ const errorMessage = `CreateResource(): poolManager not set`;
6007
+ this.#options.logger.error(errorMessage);
6008
+ throw new Error(errorMessage);
6009
+ }
6010
+ let client;
6011
+ let useTransaction;
6012
+ try {
6057
6013
  if (transactionHandle && this.#transactions[transactionHandle]) {
6058
6014
  client = this.#transactions[transactionHandle].client;
6059
6015
  useTransaction = false;
@@ -6061,50 +6017,72 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6061
6017
  client = await this.#poolManager.connectReadWrite();
6062
6018
  useTransaction = true;
6063
6019
  }
6064
- try {
6065
- if (useTransaction) await client.query("BEGIN");
6066
- await this.#dbSTSResource.InsertResourceEx(client, {
6067
- PID: PK,
6068
- PARTITION_ID: 0,
6069
- PARTITION_DATE: 0,
6070
- RES_ID,
6071
- RES_TYPE,
6072
- RES_VER: 1,
6073
- RES_VERSION: "r5",
6074
- RES_PUBLISHED: now,
6075
- RES_UPDATED: now
6076
- });
6077
- await this.#dbSTSResourceVersion.InsertResourceVersionEx(client, {
6078
- PID: (0, node_crypto.randomUUID)(),
6079
- PARTITION_ID: 0,
6080
- PARTITION_DATE: 0,
6081
- RES_ID,
6082
- RES_TYPE,
6083
- RES_VER: 1,
6084
- OPERATION: "POST",
6085
- RES
6086
- });
6087
- performance.now();
6088
- await this.UpdateIndexes(client, resource);
6089
- if (useTransaction) await client.query("COMMIT");
6090
- return resource;
6091
- } catch (error) {
6092
- if (useTransaction) await client.query("ROLLBACK");
6093
- } finally {
6094
- if (useTransaction) client.release();
6095
- }
6096
6020
  } catch (error) {
6097
- _nsshunt_stsutils.defaultLogger.error(`PGFhirAccessLayer:CreateResource(): [${error}]`);
6021
+ const errorMessage = `CreateResource(): Could not get connection: Error: [${error}]`;
6022
+ this.#options.logger.error(errorMessage);
6023
+ throw new Error(errorMessage);
6098
6024
  }
6099
- };
6100
- UpdateResource = async (resource, RES_VER, operation, transactionHandle) => {
6101
- if (this.#poolManager) try {
6025
+ try {
6102
6026
  const RES_ID = resource.id;
6103
6027
  const RES_TYPE = resource.resourceType;
6104
- const RES = JSON.stringify(resource);
6105
6028
  const now = (/* @__PURE__ */ new Date()).getTime();
6106
- let client;
6107
- let useTransaction;
6029
+ if (useTransaction) await client.query("BEGIN");
6030
+ const newVersion = await this.#dbSTSResource.InsertResourceEx(client, {
6031
+ PID: (0, node_crypto.randomUUID)(),
6032
+ PARTITION_ID: 0,
6033
+ PARTITION_DATE: 0,
6034
+ RES_ID,
6035
+ RES_TYPE,
6036
+ RES_VER: 1,
6037
+ RES_VERSION: "r5",
6038
+ RES_PUBLISHED: now,
6039
+ RES_UPDATED: now
6040
+ });
6041
+ if (newVersion === void 0) {
6042
+ const errorMessage = `CreateResource(): Version Conflict. newVersion is undefined.`;
6043
+ this.#options.logger.error(errorMessage);
6044
+ throw new Error(errorMessage);
6045
+ }
6046
+ resource.meta ??= {};
6047
+ resource.meta.versionId = String(newVersion);
6048
+ resource.meta.lastUpdated = new Date(now).toISOString();
6049
+ const RES = JSON.stringify(resource);
6050
+ if (await this.#dbSTSResourceVersion.InsertResourceVersionEx(client, {
6051
+ PID: (0, node_crypto.randomUUID)(),
6052
+ PARTITION_ID: 0,
6053
+ PARTITION_DATE: 0,
6054
+ RES_ID,
6055
+ RES_TYPE,
6056
+ RES_VER: newVersion,
6057
+ OPERATION: "POST",
6058
+ RES
6059
+ }) === void 0) {
6060
+ const errorMessage = `CreateResource(): Version Conflict. resourceVersionRunResult is undefined.`;
6061
+ this.#options.logger.error(errorMessage);
6062
+ throw new Error(errorMessage);
6063
+ }
6064
+ performance.now();
6065
+ await this.UpdateIndexes(client, resource);
6066
+ if (useTransaction) await client.query("COMMIT");
6067
+ return resource;
6068
+ } catch (error) {
6069
+ if (useTransaction) await client.query("ROLLBACK");
6070
+ const errorMessage = `CreateResource(): Error: [${error}]`;
6071
+ this.#options.logger.error(errorMessage);
6072
+ throw new Error(errorMessage);
6073
+ } finally {
6074
+ if (useTransaction) client.release();
6075
+ }
6076
+ };
6077
+ UpdateResource = async (resource, RES_VER, operation, transactionHandle) => {
6078
+ if (!this.#poolManager) {
6079
+ const errorMessage = `UpdateResource(): poolManager not set`;
6080
+ this.#options.logger.error(errorMessage);
6081
+ throw new Error(errorMessage);
6082
+ }
6083
+ let client;
6084
+ let useTransaction;
6085
+ try {
6108
6086
  if (transactionHandle && this.#transactions[transactionHandle]) {
6109
6087
  client = this.#transactions[transactionHandle].client;
6110
6088
  useTransaction = false;
@@ -6112,43 +6090,66 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6112
6090
  client = await this.#poolManager.connectReadWrite();
6113
6091
  useTransaction = true;
6114
6092
  }
6115
- try {
6116
- if (useTransaction) await client.query("BEGIN");
6117
- await this.#dbSTSResource.UpdateResourceRecordEx(client, {
6118
- RES_TYPE,
6119
- RES_ID,
6120
- RES_VER,
6121
- RES_UPDATED: now
6122
- });
6123
- await this.#dbSTSResourceVersion.InsertResourceVersionEx(client, {
6124
- PID: (0, node_crypto.randomUUID)(),
6125
- PARTITION_ID: 0,
6126
- PARTITION_DATE: 0,
6127
- RES_ID,
6128
- RES_TYPE,
6129
- RES_VER: RES_VER + 1,
6130
- OPERATION: operation,
6131
- RES
6132
- });
6133
- await this.UpdateIndexes(client, resource);
6134
- if (useTransaction) await client.query("COMMIT");
6135
- return resource;
6136
- } catch (error) {
6137
- if (useTransaction) await client.query("ROLLBACK");
6138
- } finally {
6139
- if (useTransaction) client.release();
6093
+ } catch (error) {
6094
+ const errorMessage = `UpdateResource(): Could not get connection: Error: [${error}]`;
6095
+ this.#options.logger.error(errorMessage);
6096
+ throw new Error(errorMessage);
6097
+ }
6098
+ try {
6099
+ const RES_ID = resource.id;
6100
+ const RES_TYPE = resource.resourceType;
6101
+ const now = (/* @__PURE__ */ new Date()).getTime();
6102
+ if (useTransaction) await client.query("BEGIN");
6103
+ const newVersion = await this.#dbSTSResource.UpdateResourceRecordEx(client, {
6104
+ RES_TYPE,
6105
+ RES_ID,
6106
+ RES_VER,
6107
+ RES_UPDATED: now
6108
+ });
6109
+ if (newVersion === void 0) {
6110
+ const errorMessage = `UpdateResource(): Version Conflict. newVersion is undefined. RES_VER: [${RES_VER}]`;
6111
+ this.#options.logger.error(errorMessage);
6112
+ throw new Error(errorMessage);
6113
+ }
6114
+ resource.meta ??= {};
6115
+ resource.meta.versionId = String(newVersion);
6116
+ resource.meta.lastUpdated = new Date(now).toISOString();
6117
+ const RES = JSON.stringify(resource);
6118
+ if (await this.#dbSTSResourceVersion.InsertResourceVersionEx(client, {
6119
+ PID: (0, node_crypto.randomUUID)(),
6120
+ PARTITION_ID: 0,
6121
+ PARTITION_DATE: 0,
6122
+ RES_ID,
6123
+ RES_TYPE,
6124
+ RES_VER: newVersion,
6125
+ OPERATION: operation,
6126
+ RES
6127
+ }) === void 0) {
6128
+ const errorMessage = `UpdateResource(): Version Conflict. resourceVersionRunResult is undefined. RES_VER: [${RES_VER}]`;
6129
+ this.#options.logger.error(errorMessage);
6130
+ throw new Error(errorMessage);
6140
6131
  }
6132
+ await this.UpdateIndexes(client, resource);
6133
+ if (useTransaction) await client.query("COMMIT");
6134
+ return resource;
6141
6135
  } catch (error) {
6142
- _nsshunt_stsutils.defaultLogger.error(`PGFhirAccessLayer:UpdateResource(): [${error}]`);
6136
+ if (useTransaction) await client.query("ROLLBACK");
6137
+ const errorMessage = `UpdateResource(): Error: [${error}]`;
6138
+ this.#options.logger.error(errorMessage);
6139
+ throw new Error(errorMessage);
6140
+ } finally {
6141
+ if (useTransaction) client.release();
6143
6142
  }
6144
6143
  };
6145
6144
  DeleteResource = async (resource, RES_VER, transactionHandle) => {
6146
- if (this.#poolManager) try {
6147
- const RES_ID = resource.id;
6148
- const RES_TYPE = resource.resourceType;
6149
- const now = new Date(luxon.DateTime.fromISO(resource.meta.lastUpdated).toJSDate()).getTime();
6150
- let client;
6151
- let useTransaction;
6145
+ if (!this.#poolManager) {
6146
+ const errorMessage = `DeleteResource(): poolManager not set`;
6147
+ this.#options.logger.error(errorMessage);
6148
+ throw new Error(errorMessage);
6149
+ }
6150
+ let client;
6151
+ let useTransaction;
6152
+ try {
6152
6153
  if (transactionHandle && this.#transactions[transactionHandle]) {
6153
6154
  client = this.#transactions[transactionHandle].client;
6154
6155
  useTransaction = false;
@@ -6156,39 +6157,64 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6156
6157
  client = await this.#poolManager.connectReadWrite();
6157
6158
  useTransaction = true;
6158
6159
  }
6159
- try {
6160
- if (useTransaction) await client.query("BEGIN");
6161
- await this.#dbSTSResource.DeleteResourceRecordEx(client, {
6162
- RES_ID,
6163
- RES_TYPE,
6164
- RES_VER,
6165
- RES_DELETED_AT: now,
6166
- RES_UPDATED: now
6167
- });
6168
- await this.#dbSTSResourceVersion.InsertResourceVersionEx(client, {
6169
- PID: (0, node_crypto.randomUUID)(),
6170
- PARTITION_ID: 0,
6171
- PARTITION_DATE: 0,
6172
- RES_ID,
6173
- RES_TYPE,
6174
- RES_VER: RES_VER + 1,
6175
- OPERATION: "DELETE",
6176
- RES: ""
6177
- });
6178
- await this.UpdateIndexes(client, resource);
6179
- if (useTransaction) await client.query("COMMIT");
6180
- return resource;
6181
- } catch (error) {
6182
- if (useTransaction) await client.query("ROLLBACK");
6183
- } finally {
6184
- if (useTransaction) client.release();
6160
+ } catch (error) {
6161
+ const errorMessage = `DeleteResource(): Could not get connection: Error: [${error}]`;
6162
+ this.#options.logger.error(errorMessage);
6163
+ throw new Error(errorMessage);
6164
+ }
6165
+ try {
6166
+ const RES_ID = resource.id;
6167
+ const RES_TYPE = resource.resourceType;
6168
+ const now = (/* @__PURE__ */ new Date()).getTime();
6169
+ if (useTransaction) await client.query("BEGIN");
6170
+ const newVersion = await this.#dbSTSResource.DeleteResourceRecordEx(client, {
6171
+ RES_ID,
6172
+ RES_TYPE,
6173
+ RES_VER,
6174
+ RES_DELETED_AT: now,
6175
+ RES_UPDATED: now
6176
+ });
6177
+ if (newVersion === void 0) {
6178
+ const errorMessage = `DeleteResource(): Version Conflict. newVersion is undefined. RES_VER: [${RES_VER}]`;
6179
+ this.#options.logger.error(errorMessage);
6180
+ throw new Error(errorMessage);
6181
+ }
6182
+ resource.meta ??= {};
6183
+ resource.meta.versionId = String(newVersion);
6184
+ resource.meta.lastUpdated = new Date(now).toISOString();
6185
+ if (await this.#dbSTSResourceVersion.InsertResourceVersionEx(client, {
6186
+ PID: (0, node_crypto.randomUUID)(),
6187
+ PARTITION_ID: 0,
6188
+ PARTITION_DATE: 0,
6189
+ RES_ID,
6190
+ RES_TYPE,
6191
+ RES_VER: newVersion,
6192
+ OPERATION: "DELETE",
6193
+ RES: ""
6194
+ }) === void 0) {
6195
+ const errorMessage = `DeleteResource(): Version Conflict. resourceVersionRunResult is undefined. RES_VER: [${RES_VER}]`;
6196
+ this.#options.logger.error(errorMessage);
6197
+ throw new Error(errorMessage);
6185
6198
  }
6199
+ await this.UpdateIndexes(client, resource);
6200
+ if (useTransaction) await client.query("COMMIT");
6201
+ return resource;
6186
6202
  } catch (error) {
6187
- _nsshunt_stsutils.defaultLogger.error(`PGFhirAccessLayer:DeleteResource(): [${error}]`);
6203
+ if (useTransaction) await client.query("ROLLBACK");
6204
+ const errorMessage = `DeleteResource(): Error: [${error}]`;
6205
+ this.#options.logger.error(errorMessage);
6206
+ throw new Error(errorMessage);
6207
+ } finally {
6208
+ if (useTransaction) client.release();
6188
6209
  }
6189
6210
  };
6190
6211
  ReadResourceRecord = async (RES_TYPE, RES_ID, transactionHandle) => {
6191
- if (this.#poolManager) try {
6212
+ if (!this.#poolManager) {
6213
+ const errorMessage = `ReadResourceRecord(): poolManager not set`;
6214
+ this.#options.logger.error(errorMessage);
6215
+ throw new Error(errorMessage);
6216
+ }
6217
+ try {
6192
6218
  let client;
6193
6219
  let useTransaction;
6194
6220
  if (transactionHandle && this.#transactions[transactionHandle]) {
@@ -6200,19 +6226,19 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6200
6226
  }
6201
6227
  try {
6202
6228
  return (await client.query(`
6203
- SELECT
6204
- r.*, v.* FROM stsresfhir r
6205
- JOIN
6206
- stsresfhirver v
6207
- ON
6208
- r.RES_ID = v.RES_ID AND
6209
- r.RES_TYPE = v.RES_TYPE AND
6210
- r.RES_VER = v.RES_VER
6211
- WHERE
6212
- r.RES_ID = $1 AND
6213
- r.RES_TYPE = $2 AND
6214
- r.RES_DELETED_AT IS NULL
6215
- `, [RES_ID, RES_TYPE])).rows[0];
6229
+ SELECT
6230
+ r.*, v.* FROM stsresfhir r
6231
+ JOIN
6232
+ stsresfhirver v
6233
+ ON
6234
+ r.RES_ID = v.RES_ID AND
6235
+ r.RES_TYPE = v.RES_TYPE AND
6236
+ r.RES_VER = v.RES_VER
6237
+ WHERE
6238
+ r.RES_ID = $1 AND
6239
+ r.RES_TYPE = $2 AND
6240
+ r.RES_DELETED_AT IS NULL
6241
+ `, [RES_ID, RES_TYPE])).rows[0];
6216
6242
  } catch (error) {
6217
6243
  _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadResourceRecord(): Query Error: [${error}]`);
6218
6244
  } finally {
@@ -6223,7 +6249,12 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6223
6249
  }
6224
6250
  };
6225
6251
  ReadResourceRecordWithVersion = async (RES_TYPE, RES_ID, RES_VER, transactionHandle) => {
6226
- if (this.#poolManager) try {
6252
+ if (!this.#poolManager) {
6253
+ const errorMessage = `ReadResourceRecordWithVersion(): poolManager not set`;
6254
+ this.#options.logger.error(errorMessage);
6255
+ throw new Error(errorMessage);
6256
+ }
6257
+ try {
6227
6258
  let client;
6228
6259
  let useTransaction;
6229
6260
  if (transactionHandle && this.#transactions[transactionHandle]) {
@@ -6235,18 +6266,18 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6235
6266
  }
6236
6267
  try {
6237
6268
  return (await client.query(`
6238
- SELECT
6239
- r.*, v.*, r.res_ver as R_res_ver, v.res_ver as V_res_ver FROM stsresfhir r
6240
- JOIN
6241
- stsresfhirver v
6242
- ON
6243
- r.RES_ID = v.RES_ID AND
6244
- r.RES_TYPE = v.RES_TYPE
6245
- WHERE
6246
- r.RES_ID = $1 AND
6247
- r.RES_TYPE = $2 AND
6248
- v.RES_VER = $3
6249
- `, [
6269
+ SELECT
6270
+ r.*, v.*, r.res_ver as R_res_ver, v.res_ver as V_res_ver FROM stsresfhir r
6271
+ JOIN
6272
+ stsresfhirver v
6273
+ ON
6274
+ r.RES_ID = v.RES_ID AND
6275
+ r.RES_TYPE = v.RES_TYPE
6276
+ WHERE
6277
+ r.RES_ID = $1 AND
6278
+ r.RES_TYPE = $2 AND
6279
+ v.RES_VER = $3
6280
+ `, [
6250
6281
  RES_ID,
6251
6282
  RES_TYPE,
6252
6283
  RES_VER
@@ -6261,7 +6292,12 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6261
6292
  }
6262
6293
  };
6263
6294
  ReadResourceInstanceWithAllVersions = async (RES_TYPE, RES_ID, transactionHandle) => {
6264
- if (this.#poolManager) try {
6295
+ if (!this.#poolManager) {
6296
+ const errorMessage = `ReadResourceInstanceWithAllVersions(): poolManager not set`;
6297
+ this.#options.logger.error(errorMessage);
6298
+ throw new Error(errorMessage);
6299
+ }
6300
+ try {
6265
6301
  let client;
6266
6302
  let useTransaction;
6267
6303
  if (transactionHandle && this.#transactions[transactionHandle]) {
@@ -6273,19 +6309,19 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6273
6309
  }
6274
6310
  try {
6275
6311
  return (await client.query(`
6276
- SELECT
6277
- r.*, v.*, r.res_ver as R_res_ver, v.res_ver as V_res_ver FROM stsresfhir r
6278
- JOIN
6279
- stsresfhirver v
6280
- ON
6281
- r.RES_ID = v.RES_ID AND
6282
- r.RES_TYPE = v.RES_TYPE
6283
- WHERE
6284
- r.RES_ID = $1 AND
6285
- r.RES_TYPE = $2
6286
- ORDER BY
6287
- v.RES_TYPE ASC, v.RES_ID ASC, v.RES_VER DESC
6288
- `, [RES_ID, RES_TYPE])).rows;
6312
+ SELECT
6313
+ r.*, v.*, r.res_ver as R_res_ver, v.res_ver as V_res_ver FROM stsresfhir r
6314
+ JOIN
6315
+ stsresfhirver v
6316
+ ON
6317
+ r.RES_ID = v.RES_ID AND
6318
+ r.RES_TYPE = v.RES_TYPE
6319
+ WHERE
6320
+ r.RES_ID = $1 AND
6321
+ r.RES_TYPE = $2
6322
+ ORDER BY
6323
+ v.RES_TYPE ASC, v.RES_ID ASC, v.RES_VER DESC
6324
+ `, [RES_ID, RES_TYPE])).rows;
6289
6325
  } catch (error) {
6290
6326
  _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadResourceRecord(): Query Error: [${error}]`);
6291
6327
  } finally {
@@ -6296,7 +6332,12 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6296
6332
  }
6297
6333
  };
6298
6334
  ReadResourcesWithAllVersions = async (RES_TYPE, transactionHandle) => {
6299
- if (this.#poolManager) try {
6335
+ if (!this.#poolManager) {
6336
+ const errorMessage = `ReadResourcesWithAllVersions(): poolManager not set`;
6337
+ this.#options.logger.error(errorMessage);
6338
+ throw new Error(errorMessage);
6339
+ }
6340
+ try {
6300
6341
  let client;
6301
6342
  let useTransaction;
6302
6343
  if (transactionHandle && this.#transactions[transactionHandle]) {
@@ -6308,18 +6349,18 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6308
6349
  }
6309
6350
  try {
6310
6351
  return (await client.query(`
6311
- SELECT
6312
- r.*, v.*, r.res_ver as R_res_ver, v.res_ver as V_res_ver FROM stsresfhir r
6313
- JOIN
6314
- stsresfhirver v
6315
- ON
6316
- r.RES_ID = v.RES_ID AND
6317
- r.RES_TYPE = v.RES_TYPE
6318
- WHERE
6319
- r.RES_TYPE = $1
6320
- ORDER BY
6321
- v.RES_TYPE ASC, v.RES_ID ASC, v.RES_VER DESC
6322
- `, [RES_TYPE])).rows;
6352
+ SELECT
6353
+ r.*, v.*, r.res_ver as R_res_ver, v.res_ver as V_res_ver FROM stsresfhir r
6354
+ JOIN
6355
+ stsresfhirver v
6356
+ ON
6357
+ r.RES_ID = v.RES_ID AND
6358
+ r.RES_TYPE = v.RES_TYPE
6359
+ WHERE
6360
+ r.RES_TYPE = $1
6361
+ ORDER BY
6362
+ v.RES_TYPE ASC, v.RES_ID ASC, v.RES_VER DESC
6363
+ `, [RES_TYPE])).rows;
6323
6364
  } catch (error) {
6324
6365
  _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadResourceRecord(): Query Error: [${error}]`);
6325
6366
  } finally {
@@ -6330,7 +6371,12 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6330
6371
  }
6331
6372
  };
6332
6373
  ReadAllResourcesWithVersions = async (transactionHandle) => {
6333
- if (this.#poolManager) try {
6374
+ if (!this.#poolManager) {
6375
+ const errorMessage = `ReadAllResourcesWithVersions(): poolManager not set`;
6376
+ this.#options.logger.error(errorMessage);
6377
+ throw new Error(errorMessage);
6378
+ }
6379
+ try {
6334
6380
  let client;
6335
6381
  let useTransaction;
6336
6382
  if (transactionHandle && this.#transactions[transactionHandle]) {
@@ -6342,16 +6388,16 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6342
6388
  }
6343
6389
  try {
6344
6390
  return (await client.query(`
6345
- SELECT
6346
- r.*, v.* FROM stsresfhir r
6347
- JOIN
6348
- stsresfhirver v
6349
- ON
6350
- r.RES_ID = v.RES_ID AND
6351
- r.RES_TYPE = v.RES_TYPE AND
6352
- ORDER BY
6353
- v.RES_TYPE ASC, v.RES_ID ASC, v.RES_VER DESC
6354
- `)).rows;
6391
+ SELECT
6392
+ r.*, v.* FROM stsresfhir r
6393
+ JOIN
6394
+ stsresfhirver v
6395
+ ON
6396
+ r.RES_ID = v.RES_ID AND
6397
+ r.RES_TYPE = v.RES_TYPE AND
6398
+ ORDER BY
6399
+ v.RES_TYPE ASC, v.RES_ID ASC, v.RES_VER DESC
6400
+ `)).rows;
6355
6401
  } catch (error) {
6356
6402
  _nsshunt_stsutils.defaultLogger.error(`DBSTSResource:ReadResourceRecord(): Query Error: [${error}]`);
6357
6403
  } finally {
@@ -6365,7 +6411,12 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6365
6411
  return this.#dbSTSResourceVersion.ReadAllResourceVersionRecords();
6366
6412
  };
6367
6413
  ReadResourceIndexes = async (resource, transactionHandle) => {
6368
- if (this.#poolManager) try {
6414
+ if (!this.#poolManager) {
6415
+ const errorMessage = `ReadResourceIndexes(): poolManager not set`;
6416
+ this.#options.logger.error(errorMessage);
6417
+ throw new Error(errorMessage);
6418
+ }
6419
+ try {
6369
6420
  const resultSet = {
6370
6421
  "stsresfhirlink": [],
6371
6422
  "stsresfhirstring": [],
@@ -6405,7 +6456,12 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6405
6456
  }
6406
6457
  };
6407
6458
  ExecuteReadQuery = async (queryData, transactionHandle) => {
6408
- if (this.#poolManager) try {
6459
+ if (!this.#poolManager) {
6460
+ const errorMessage = `ExecuteReadQuery(): poolManager not set`;
6461
+ this.#options.logger.error(errorMessage);
6462
+ throw new Error(errorMessage);
6463
+ }
6464
+ try {
6409
6465
  let client;
6410
6466
  let useTransaction;
6411
6467
  if (transactionHandle && this.#transactions[transactionHandle]) {
@@ -6428,7 +6484,12 @@ var PGFhirAccessLayer = class extends tiny_emitter.TinyEmitter {
6428
6484
  }
6429
6485
  };
6430
6486
  TruncateAll = async () => {
6431
- if (this.#poolManager) try {
6487
+ if (!this.#poolManager) {
6488
+ const errorMessage = `TruncateAll(): poolManager not set`;
6489
+ this.#options.logger.error(errorMessage);
6490
+ throw new Error(errorMessage);
6491
+ }
6492
+ try {
6432
6493
  let client;
6433
6494
  client = await this.#poolManager.connectReadWrite();
6434
6495
  try {