@okf/ootils 1.8.5 → 1.8.6

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/node.d.mts CHANGED
@@ -662,10 +662,12 @@ declare class MongoConnector {
662
662
  static getClusterConnections(): any;
663
663
  static getEnv(): any;
664
664
  static getDbConfigs(): any;
665
+ static getTenantToClusterMapping(): any;
665
666
  constructor(options: any);
666
667
  clusterConnections: {};
667
668
  env: any;
668
669
  dbConfigs: any;
670
+ tenantToClusterMapping: any;
669
671
  mongoOptions: any;
670
672
  /**
671
673
  * Creates multiple MongoDB clusterConnections based on configuration (SYNCHRONOUS)
@@ -838,30 +840,26 @@ declare function getModelByTenant({ tenant, modelName, schema, env }: {
838
840
  schema: any;
839
841
  env: any;
840
842
  }): any;
841
- declare function getAIChatModelByTenant({ tenant, env, mongodb, dbConfigs }: {
843
+ declare function getAIChatModelByTenant({ tenant, env, mongodb }: {
842
844
  tenant: any;
843
845
  env: any;
844
846
  mongodb: any;
845
- dbConfigs: any;
846
847
  }): any;
847
- declare function getAnnotationsModelByTenant({ tenant, env, mongodb, dbConfigs, modelName }: {
848
+ declare function getAnnotationsModelByTenant({ tenant, env, mongodb, modelName }: {
848
849
  tenant: any;
849
850
  env: any;
850
851
  mongodb: any;
851
- dbConfigs: any;
852
852
  modelName: any;
853
853
  }): any;
854
- declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb, dbConfigs }: {
854
+ declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb }: {
855
855
  tenant: any;
856
856
  env: any;
857
857
  mongodb: any;
858
- dbConfigs: any;
859
858
  }): any;
860
- declare function getTplModelByTenant({ tenant, env, mongodb, dbConfigs }: {
859
+ declare function getTplModelByTenant({ tenant, env, mongodb }: {
861
860
  tenant: any;
862
861
  env: any;
863
862
  mongodb: any;
864
- dbConfigs: any;
865
863
  }): any;
866
864
 
867
865
  interface ITagData {
package/dist/node.d.ts CHANGED
@@ -662,10 +662,12 @@ declare class MongoConnector {
662
662
  static getClusterConnections(): any;
663
663
  static getEnv(): any;
664
664
  static getDbConfigs(): any;
665
+ static getTenantToClusterMapping(): any;
665
666
  constructor(options: any);
666
667
  clusterConnections: {};
667
668
  env: any;
668
669
  dbConfigs: any;
670
+ tenantToClusterMapping: any;
669
671
  mongoOptions: any;
670
672
  /**
671
673
  * Creates multiple MongoDB clusterConnections based on configuration (SYNCHRONOUS)
@@ -838,30 +840,26 @@ declare function getModelByTenant({ tenant, modelName, schema, env }: {
838
840
  schema: any;
839
841
  env: any;
840
842
  }): any;
841
- declare function getAIChatModelByTenant({ tenant, env, mongodb, dbConfigs }: {
843
+ declare function getAIChatModelByTenant({ tenant, env, mongodb }: {
842
844
  tenant: any;
843
845
  env: any;
844
846
  mongodb: any;
845
- dbConfigs: any;
846
847
  }): any;
847
- declare function getAnnotationsModelByTenant({ tenant, env, mongodb, dbConfigs, modelName }: {
848
+ declare function getAnnotationsModelByTenant({ tenant, env, mongodb, modelName }: {
848
849
  tenant: any;
849
850
  env: any;
850
851
  mongodb: any;
851
- dbConfigs: any;
852
852
  modelName: any;
853
853
  }): any;
854
- declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb, dbConfigs }: {
854
+ declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb }: {
855
855
  tenant: any;
856
856
  env: any;
857
857
  mongodb: any;
858
- dbConfigs: any;
859
858
  }): any;
860
- declare function getTplModelByTenant({ tenant, env, mongodb, dbConfigs }: {
859
+ declare function getTplModelByTenant({ tenant, env, mongodb }: {
861
860
  tenant: any;
862
861
  env: any;
863
862
  mongodb: any;
864
- dbConfigs: any;
865
863
  }): any;
866
864
 
867
865
  interface ITagData {
package/dist/node.js CHANGED
@@ -687,6 +687,7 @@ var require_MongoConnector = __commonJS({
687
687
  this.clusterConnections = {};
688
688
  this.env = options.env;
689
689
  this.dbConfigs = options.dbConfigs;
690
+ this.tenantToClusterMapping = options.tenantToClusterMapping;
690
691
  this.mongoOptions = options.mongoOptions || {
691
692
  // maxPoolSize: 40,
692
693
  // serverSelectionTimeoutMS: 5000,
@@ -706,27 +707,25 @@ var require_MongoConnector = __commonJS({
706
707
  if (!this.dbConfigs) {
707
708
  throw new Error("dbConfigs must be provided in constructor options");
708
709
  }
710
+ if (!this.tenantToClusterMapping) {
711
+ throw new Error("tenantToClusterMapping must be provided in constructor options");
712
+ }
709
713
  }
710
714
  /**
711
715
  * Creates multiple MongoDB clusterConnections based on configuration (SYNCHRONOUS)
712
716
  * @returns Object containing named database clusterConnections
713
717
  */
714
718
  multiConnectToMongoDB() {
715
- const { connectTo } = this.dbConfigs[this.env];
716
- if (!connectTo) {
717
- throw new Error(
718
- `No connection configuration found for environment: ${this.env}`
719
- );
720
- }
721
- connectTo.forEach((connectToEnv) => {
722
- const { CLUSTER_NAME, DB_URI } = this.dbConfigs[connectToEnv];
723
- if (!CLUSTER_NAME || !DB_URI) {
719
+ const allClusterConfigs = Object.values(this.dbConfigs);
720
+ allClusterConfigs.forEach((clusterConf) => {
721
+ const { CLUSTER_NAME, CLUSTER_URI } = clusterConf;
722
+ if (!CLUSTER_NAME || !CLUSTER_URI) {
724
723
  throw new Error(
725
- `Missing CLUSTER_NAME or DB_URI for environment: ${connectToEnv}`
724
+ `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
726
725
  );
727
726
  }
728
727
  this.clusterConnections[CLUSTER_NAME] = mongoose5.createConnection(
729
- DB_URI,
728
+ CLUSTER_URI,
730
729
  this.mongoOptions
731
730
  );
732
731
  this.initiateConnectionEventListeners(CLUSTER_NAME);
@@ -737,21 +736,16 @@ var require_MongoConnector = __commonJS({
737
736
  return this.clusterConnections;
738
737
  }
739
738
  async multiConnectToMongoDBAsync() {
740
- const { connectTo } = this.dbConfigs[this.env];
741
- if (!connectTo) {
742
- throw new Error(
743
- `No connection configuration found for environment: ${this.env}`
744
- );
745
- }
739
+ const allClusterConfigs = Object.values(this.dbConfigs);
746
740
  await Promise.all(
747
- connectTo.map(async (connectToEnv) => {
748
- const { CLUSTER_NAME, DB_URI } = this.dbConfigs[connectToEnv];
749
- if (!CLUSTER_NAME || !DB_URI) {
741
+ allClusterConfigs.map(async (clusterConf) => {
742
+ const { CLUSTER_NAME, CLUSTER_URI } = clusterConf;
743
+ if (!CLUSTER_NAME || !CLUSTER_URI) {
750
744
  throw new Error(
751
- `Missing CLUSTER_NAME or DB_URI for environment: ${connectToEnv}`
745
+ `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
752
746
  );
753
747
  }
754
- const connection = mongoose5.createConnection(DB_URI, this.mongoOptions);
748
+ const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
755
749
  this.clusterConnections[CLUSTER_NAME] = connection;
756
750
  this.initiateConnectionEventListeners(CLUSTER_NAME);
757
751
  await new Promise((resolve, reject) => {
@@ -763,6 +757,9 @@ var require_MongoConnector = __commonJS({
763
757
  });
764
758
  })
765
759
  );
760
+ console.log(
761
+ `\u{1F389} Connected to ${Object.keys(this.clusterConnections).length} MongoDB databases`
762
+ );
766
763
  return this.clusterConnections;
767
764
  }
768
765
  // Static method to get the full instance
@@ -793,6 +790,12 @@ var require_MongoConnector = __commonJS({
793
790
  }
794
791
  return _MongoConnector.instance.dbConfigs;
795
792
  }
793
+ static getTenantToClusterMapping() {
794
+ if (!_MongoConnector.instance) {
795
+ throw new Error("MongoConnector not initialized");
796
+ }
797
+ return _MongoConnector.instance.tenantToClusterMapping;
798
+ }
796
799
  /**
797
800
  * Helper function to close clusterConnections object
798
801
  */
@@ -984,13 +987,14 @@ var init_getDbByTenant = __esm({
984
987
  }) => {
985
988
  if (!tenant) throw new Error("tenant id has not been provided");
986
989
  const env = _env || import_MongoConnector.MongoConnector.getEnv();
987
- const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
990
+ const tenantToClusterMapping = import_MongoConnector.MongoConnector.getTenantToClusterMapping();
991
+ const thisClusterName = tenantToClusterMapping[tenant]?.CLUSTER_NAME?.[env];
992
+ if (!thisClusterName) throw new Error("Could not find a CLUSTER_NAME mapped to this tenant + env");
988
993
  const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
989
- const { CLUSTER_NAME } = dbConfigs[env];
990
994
  const dbName = `${tenant}_${env}`;
991
- const connection = clusterConnections[CLUSTER_NAME];
995
+ const connection = clusterConnections[thisClusterName];
992
996
  if (!connection) {
993
- throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
997
+ throw new Error(`Connection not found for cluster: ${thisClusterName}`);
994
998
  }
995
999
  return connection.useDb(dbName, { useCache: true });
996
1000
  };
@@ -1013,25 +1017,25 @@ var require_getModelByTenant = __commonJS({
1013
1017
  }
1014
1018
  return db.model(modelName);
1015
1019
  };
1016
- var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant3({
1020
+ var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, modelName }) => getModelByTenant3({
1017
1021
  tenant,
1018
1022
  modelName: modelName || "annotations",
1019
1023
  schema: AnnotationSchema2,
1020
1024
  env
1021
1025
  });
1022
- var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1026
+ var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1023
1027
  tenant,
1024
1028
  modelName: "platformConfigs",
1025
1029
  schema: PlatformConfigsSchema2,
1026
1030
  env
1027
1031
  });
1028
- var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1032
+ var getTplModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1029
1033
  tenant,
1030
1034
  modelName: "tpl",
1031
1035
  schema: TplSchema2,
1032
1036
  env
1033
1037
  });
1034
- var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1038
+ var getAIChatModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1035
1039
  tenant,
1036
1040
  modelName: "aiChat",
1037
1041
  schema: AIChatSchema2,
package/dist/node.mjs CHANGED
@@ -692,6 +692,7 @@ var require_MongoConnector = __commonJS({
692
692
  this.clusterConnections = {};
693
693
  this.env = options.env;
694
694
  this.dbConfigs = options.dbConfigs;
695
+ this.tenantToClusterMapping = options.tenantToClusterMapping;
695
696
  this.mongoOptions = options.mongoOptions || {
696
697
  // maxPoolSize: 40,
697
698
  // serverSelectionTimeoutMS: 5000,
@@ -711,27 +712,25 @@ var require_MongoConnector = __commonJS({
711
712
  if (!this.dbConfigs) {
712
713
  throw new Error("dbConfigs must be provided in constructor options");
713
714
  }
715
+ if (!this.tenantToClusterMapping) {
716
+ throw new Error("tenantToClusterMapping must be provided in constructor options");
717
+ }
714
718
  }
715
719
  /**
716
720
  * Creates multiple MongoDB clusterConnections based on configuration (SYNCHRONOUS)
717
721
  * @returns Object containing named database clusterConnections
718
722
  */
719
723
  multiConnectToMongoDB() {
720
- const { connectTo } = this.dbConfigs[this.env];
721
- if (!connectTo) {
722
- throw new Error(
723
- `No connection configuration found for environment: ${this.env}`
724
- );
725
- }
726
- connectTo.forEach((connectToEnv) => {
727
- const { CLUSTER_NAME, DB_URI } = this.dbConfigs[connectToEnv];
728
- if (!CLUSTER_NAME || !DB_URI) {
724
+ const allClusterConfigs = Object.values(this.dbConfigs);
725
+ allClusterConfigs.forEach((clusterConf) => {
726
+ const { CLUSTER_NAME, CLUSTER_URI } = clusterConf;
727
+ if (!CLUSTER_NAME || !CLUSTER_URI) {
729
728
  throw new Error(
730
- `Missing CLUSTER_NAME or DB_URI for environment: ${connectToEnv}`
729
+ `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
731
730
  );
732
731
  }
733
732
  this.clusterConnections[CLUSTER_NAME] = mongoose5.createConnection(
734
- DB_URI,
733
+ CLUSTER_URI,
735
734
  this.mongoOptions
736
735
  );
737
736
  this.initiateConnectionEventListeners(CLUSTER_NAME);
@@ -742,21 +741,16 @@ var require_MongoConnector = __commonJS({
742
741
  return this.clusterConnections;
743
742
  }
744
743
  async multiConnectToMongoDBAsync() {
745
- const { connectTo } = this.dbConfigs[this.env];
746
- if (!connectTo) {
747
- throw new Error(
748
- `No connection configuration found for environment: ${this.env}`
749
- );
750
- }
744
+ const allClusterConfigs = Object.values(this.dbConfigs);
751
745
  await Promise.all(
752
- connectTo.map(async (connectToEnv) => {
753
- const { CLUSTER_NAME, DB_URI } = this.dbConfigs[connectToEnv];
754
- if (!CLUSTER_NAME || !DB_URI) {
746
+ allClusterConfigs.map(async (clusterConf) => {
747
+ const { CLUSTER_NAME, CLUSTER_URI } = clusterConf;
748
+ if (!CLUSTER_NAME || !CLUSTER_URI) {
755
749
  throw new Error(
756
- `Missing CLUSTER_NAME or DB_URI for environment: ${connectToEnv}`
750
+ `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
757
751
  );
758
752
  }
759
- const connection = mongoose5.createConnection(DB_URI, this.mongoOptions);
753
+ const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
760
754
  this.clusterConnections[CLUSTER_NAME] = connection;
761
755
  this.initiateConnectionEventListeners(CLUSTER_NAME);
762
756
  await new Promise((resolve, reject) => {
@@ -768,6 +762,9 @@ var require_MongoConnector = __commonJS({
768
762
  });
769
763
  })
770
764
  );
765
+ console.log(
766
+ `\u{1F389} Connected to ${Object.keys(this.clusterConnections).length} MongoDB databases`
767
+ );
771
768
  return this.clusterConnections;
772
769
  }
773
770
  // Static method to get the full instance
@@ -798,6 +795,12 @@ var require_MongoConnector = __commonJS({
798
795
  }
799
796
  return _MongoConnector.instance.dbConfigs;
800
797
  }
798
+ static getTenantToClusterMapping() {
799
+ if (!_MongoConnector.instance) {
800
+ throw new Error("MongoConnector not initialized");
801
+ }
802
+ return _MongoConnector.instance.tenantToClusterMapping;
803
+ }
801
804
  /**
802
805
  * Helper function to close clusterConnections object
803
806
  */
@@ -989,13 +992,14 @@ var init_getDbByTenant = __esm({
989
992
  }) => {
990
993
  if (!tenant) throw new Error("tenant id has not been provided");
991
994
  const env = _env || import_MongoConnector.MongoConnector.getEnv();
992
- const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
995
+ const tenantToClusterMapping = import_MongoConnector.MongoConnector.getTenantToClusterMapping();
996
+ const thisClusterName = tenantToClusterMapping[tenant]?.CLUSTER_NAME?.[env];
997
+ if (!thisClusterName) throw new Error("Could not find a CLUSTER_NAME mapped to this tenant + env");
993
998
  const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
994
- const { CLUSTER_NAME } = dbConfigs[env];
995
999
  const dbName = `${tenant}_${env}`;
996
- const connection = clusterConnections[CLUSTER_NAME];
1000
+ const connection = clusterConnections[thisClusterName];
997
1001
  if (!connection) {
998
- throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
1002
+ throw new Error(`Connection not found for cluster: ${thisClusterName}`);
999
1003
  }
1000
1004
  return connection.useDb(dbName, { useCache: true });
1001
1005
  };
@@ -1018,25 +1022,25 @@ var require_getModelByTenant = __commonJS({
1018
1022
  }
1019
1023
  return db.model(modelName);
1020
1024
  };
1021
- var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant3({
1025
+ var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, modelName }) => getModelByTenant3({
1022
1026
  tenant,
1023
1027
  modelName: modelName || "annotations",
1024
1028
  schema: AnnotationSchema2,
1025
1029
  env
1026
1030
  });
1027
- var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1031
+ var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1028
1032
  tenant,
1029
1033
  modelName: "platformConfigs",
1030
1034
  schema: PlatformConfigsSchema2,
1031
1035
  env
1032
1036
  });
1033
- var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1037
+ var getTplModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1034
1038
  tenant,
1035
1039
  modelName: "tpl",
1036
1040
  schema: TplSchema2,
1037
1041
  env
1038
1042
  });
1039
- var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1043
+ var getAIChatModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1040
1044
  tenant,
1041
1045
  modelName: "aiChat",
1042
1046
  schema: AIChatSchema2,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.8.5",
6
+ "version": "1.8.6",
7
7
  "description": "Utility functions for both browser and Node.js",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/index.mjs",