@okf/ootils 1.8.5 → 1.8.7

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)
@@ -827,9 +829,10 @@ declare namespace RedisCacheConnector {
827
829
  * @param env - Optional environment override (defaults to current environment)
828
830
  * @returns Mongoose Connection instance for the tenant-specific database
829
831
  */
830
- declare function getDbByTenant({ tenant, env: _env }: {
832
+ declare function getDbByTenant({ tenant, env: _env, CLUSTER_NAME }: {
831
833
  tenant: any;
832
834
  env: any;
835
+ CLUSTER_NAME: any;
833
836
  }): any;
834
837
 
835
838
  declare function getModelByTenant({ tenant, modelName, schema, env }: {
@@ -838,30 +841,26 @@ declare function getModelByTenant({ tenant, modelName, schema, env }: {
838
841
  schema: any;
839
842
  env: any;
840
843
  }): any;
841
- declare function getAIChatModelByTenant({ tenant, env, mongodb, dbConfigs }: {
844
+ declare function getAIChatModelByTenant({ tenant, env, mongodb }: {
842
845
  tenant: any;
843
846
  env: any;
844
847
  mongodb: any;
845
- dbConfigs: any;
846
848
  }): any;
847
- declare function getAnnotationsModelByTenant({ tenant, env, mongodb, dbConfigs, modelName }: {
849
+ declare function getAnnotationsModelByTenant({ tenant, env, mongodb, modelName }: {
848
850
  tenant: any;
849
851
  env: any;
850
852
  mongodb: any;
851
- dbConfigs: any;
852
853
  modelName: any;
853
854
  }): any;
854
- declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb, dbConfigs }: {
855
+ declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb }: {
855
856
  tenant: any;
856
857
  env: any;
857
858
  mongodb: any;
858
- dbConfigs: any;
859
859
  }): any;
860
- declare function getTplModelByTenant({ tenant, env, mongodb, dbConfigs }: {
860
+ declare function getTplModelByTenant({ tenant, env, mongodb }: {
861
861
  tenant: any;
862
862
  env: any;
863
863
  mongodb: any;
864
- dbConfigs: any;
865
864
  }): any;
866
865
 
867
866
  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)
@@ -827,9 +829,10 @@ declare namespace RedisCacheConnector {
827
829
  * @param env - Optional environment override (defaults to current environment)
828
830
  * @returns Mongoose Connection instance for the tenant-specific database
829
831
  */
830
- declare function getDbByTenant({ tenant, env: _env }: {
832
+ declare function getDbByTenant({ tenant, env: _env, CLUSTER_NAME }: {
831
833
  tenant: any;
832
834
  env: any;
835
+ CLUSTER_NAME: any;
833
836
  }): any;
834
837
 
835
838
  declare function getModelByTenant({ tenant, modelName, schema, env }: {
@@ -838,30 +841,26 @@ declare function getModelByTenant({ tenant, modelName, schema, env }: {
838
841
  schema: any;
839
842
  env: any;
840
843
  }): any;
841
- declare function getAIChatModelByTenant({ tenant, env, mongodb, dbConfigs }: {
844
+ declare function getAIChatModelByTenant({ tenant, env, mongodb }: {
842
845
  tenant: any;
843
846
  env: any;
844
847
  mongodb: any;
845
- dbConfigs: any;
846
848
  }): any;
847
- declare function getAnnotationsModelByTenant({ tenant, env, mongodb, dbConfigs, modelName }: {
849
+ declare function getAnnotationsModelByTenant({ tenant, env, mongodb, modelName }: {
848
850
  tenant: any;
849
851
  env: any;
850
852
  mongodb: any;
851
- dbConfigs: any;
852
853
  modelName: any;
853
854
  }): any;
854
- declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb, dbConfigs }: {
855
+ declare function getPlatformConfigsModelByTenant({ tenant, env, mongodb }: {
855
856
  tenant: any;
856
857
  env: any;
857
858
  mongodb: any;
858
- dbConfigs: any;
859
859
  }): any;
860
- declare function getTplModelByTenant({ tenant, env, mongodb, dbConfigs }: {
860
+ declare function getTplModelByTenant({ tenant, env, mongodb }: {
861
861
  tenant: any;
862
862
  env: any;
863
863
  mongodb: any;
864
- dbConfigs: any;
865
864
  }): any;
866
865
 
867
866
  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
  */
@@ -980,17 +983,19 @@ var init_getDbByTenant = __esm({
980
983
  import_MongoConnector = __toESM(require_MongoConnector());
981
984
  getDbByTenant = ({
982
985
  tenant,
983
- env: _env
986
+ env: _env,
987
+ CLUSTER_NAME
984
988
  }) => {
985
989
  if (!tenant) throw new Error("tenant id has not been provided");
986
990
  const env = _env || import_MongoConnector.MongoConnector.getEnv();
987
- const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
991
+ const tenantToClusterMapping = import_MongoConnector.MongoConnector.getTenantToClusterMapping();
992
+ const thisClusterName = CLUSTER_NAME ? CLUSTER_NAME?.[env] : tenantToClusterMapping[tenant]?.CLUSTER_NAME?.[env];
993
+ if (!thisClusterName) throw new Error("Could not find a CLUSTER_NAME mapped to this tenant + env");
988
994
  const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
989
- const { CLUSTER_NAME } = dbConfigs[env];
990
995
  const dbName = `${tenant}_${env}`;
991
- const connection = clusterConnections[CLUSTER_NAME];
996
+ const connection = clusterConnections[thisClusterName];
992
997
  if (!connection) {
993
- throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
998
+ throw new Error(`Connection not found for cluster: ${thisClusterName}`);
994
999
  }
995
1000
  return connection.useDb(dbName, { useCache: true });
996
1001
  };
@@ -1013,25 +1018,25 @@ var require_getModelByTenant = __commonJS({
1013
1018
  }
1014
1019
  return db.model(modelName);
1015
1020
  };
1016
- var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant3({
1021
+ var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, modelName }) => getModelByTenant3({
1017
1022
  tenant,
1018
1023
  modelName: modelName || "annotations",
1019
1024
  schema: AnnotationSchema2,
1020
1025
  env
1021
1026
  });
1022
- var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1027
+ var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1023
1028
  tenant,
1024
1029
  modelName: "platformConfigs",
1025
1030
  schema: PlatformConfigsSchema2,
1026
1031
  env
1027
1032
  });
1028
- var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1033
+ var getTplModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1029
1034
  tenant,
1030
1035
  modelName: "tpl",
1031
1036
  schema: TplSchema2,
1032
1037
  env
1033
1038
  });
1034
- var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1039
+ var getAIChatModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1035
1040
  tenant,
1036
1041
  modelName: "aiChat",
1037
1042
  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
  */
@@ -985,17 +988,19 @@ var init_getDbByTenant = __esm({
985
988
  import_MongoConnector = __toESM(require_MongoConnector());
986
989
  getDbByTenant = ({
987
990
  tenant,
988
- env: _env
991
+ env: _env,
992
+ CLUSTER_NAME
989
993
  }) => {
990
994
  if (!tenant) throw new Error("tenant id has not been provided");
991
995
  const env = _env || import_MongoConnector.MongoConnector.getEnv();
992
- const dbConfigs = import_MongoConnector.MongoConnector.getDbConfigs();
996
+ const tenantToClusterMapping = import_MongoConnector.MongoConnector.getTenantToClusterMapping();
997
+ const thisClusterName = CLUSTER_NAME ? CLUSTER_NAME?.[env] : tenantToClusterMapping[tenant]?.CLUSTER_NAME?.[env];
998
+ if (!thisClusterName) throw new Error("Could not find a CLUSTER_NAME mapped to this tenant + env");
993
999
  const clusterConnections = import_MongoConnector.MongoConnector.getClusterConnections();
994
- const { CLUSTER_NAME } = dbConfigs[env];
995
1000
  const dbName = `${tenant}_${env}`;
996
- const connection = clusterConnections[CLUSTER_NAME];
1001
+ const connection = clusterConnections[thisClusterName];
997
1002
  if (!connection) {
998
- throw new Error(`Connection not found for cluster: ${CLUSTER_NAME}`);
1003
+ throw new Error(`Connection not found for cluster: ${thisClusterName}`);
999
1004
  }
1000
1005
  return connection.useDb(dbName, { useCache: true });
1001
1006
  };
@@ -1018,25 +1023,25 @@ var require_getModelByTenant = __commonJS({
1018
1023
  }
1019
1024
  return db.model(modelName);
1020
1025
  };
1021
- var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs, modelName }) => getModelByTenant3({
1026
+ var getAnnotationsModelByTenant2 = ({ tenant, env, mongodb, modelName }) => getModelByTenant3({
1022
1027
  tenant,
1023
1028
  modelName: modelName || "annotations",
1024
1029
  schema: AnnotationSchema2,
1025
1030
  env
1026
1031
  });
1027
- var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1032
+ var getPlatformConfigsModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1028
1033
  tenant,
1029
1034
  modelName: "platformConfigs",
1030
1035
  schema: PlatformConfigsSchema2,
1031
1036
  env
1032
1037
  });
1033
- var getTplModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1038
+ var getTplModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1034
1039
  tenant,
1035
1040
  modelName: "tpl",
1036
1041
  schema: TplSchema2,
1037
1042
  env
1038
1043
  });
1039
- var getAIChatModelByTenant2 = ({ tenant, env, mongodb, dbConfigs }) => getModelByTenant3({
1044
+ var getAIChatModelByTenant2 = ({ tenant, env, mongodb }) => getModelByTenant3({
1040
1045
  tenant,
1041
1046
  modelName: "aiChat",
1042
1047
  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.7",
7
7
  "description": "Utility functions for both browser and Node.js",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/index.mjs",