@okf/ootils 1.21.3 → 1.21.4

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
@@ -763,6 +763,7 @@ declare class MongoConnector {
763
763
  static getEnv(): any;
764
764
  static getDbConfigs(): any;
765
765
  static getTenantToClusterMapping(): any;
766
+ static isClusterUnavailable(clusterName: any): any;
766
767
  constructor(options: any);
767
768
  clusterConnections: {};
768
769
  env: any;
@@ -775,7 +776,9 @@ declare class MongoConnector {
775
776
  */
776
777
  multiConnectToMongoDB(): {};
777
778
  multiConnectToMongoDBAsync(): Promise<{}>;
779
+ failedOptionalClusters: Set<any> | undefined;
778
780
  initiateConnectionEventListeners: (CLUSTER_NAME: any) => void;
781
+ initiateOptionalConnectionEventListeners: (CLUSTER_NAME: any) => void;
779
782
  /**
780
783
  * Helper function to close clusterConnections object
781
784
  */
package/dist/node.d.ts CHANGED
@@ -763,6 +763,7 @@ declare class MongoConnector {
763
763
  static getEnv(): any;
764
764
  static getDbConfigs(): any;
765
765
  static getTenantToClusterMapping(): any;
766
+ static isClusterUnavailable(clusterName: any): any;
766
767
  constructor(options: any);
767
768
  clusterConnections: {};
768
769
  env: any;
@@ -775,7 +776,9 @@ declare class MongoConnector {
775
776
  */
776
777
  multiConnectToMongoDB(): {};
777
778
  multiConnectToMongoDBAsync(): Promise<{}>;
779
+ failedOptionalClusters: Set<any> | undefined;
778
780
  initiateConnectionEventListeners: (CLUSTER_NAME: any) => void;
781
+ initiateOptionalConnectionEventListeners: (CLUSTER_NAME: any) => void;
779
782
  /**
780
783
  * Helper function to close clusterConnections object
781
784
  */
package/dist/node.js CHANGED
@@ -700,6 +700,26 @@ var require_MongoConnector = __commonJS({
700
700
  console.log(`\u{1F504} Mongoose reconnected to ${CLUSTER_NAME}`);
701
701
  });
702
702
  });
703
+ __publicField(this, "initiateOptionalConnectionEventListeners", (CLUSTER_NAME) => {
704
+ this.clusterConnections[CLUSTER_NAME].on("open", () => {
705
+ console.log(`\u2705 Mongoose connection open to optional cluster ${CLUSTER_NAME}`);
706
+ this.failedOptionalClusters?.delete(CLUSTER_NAME);
707
+ });
708
+ this.clusterConnections[CLUSTER_NAME].on("error", (err) => {
709
+ console.warn(
710
+ `\u26A0\uFE0F Mongoose connection error on optional cluster ${CLUSTER_NAME}: ${err.message}`
711
+ );
712
+ this.failedOptionalClusters?.add(CLUSTER_NAME);
713
+ });
714
+ this.clusterConnections[CLUSTER_NAME].on("disconnected", () => {
715
+ console.log(`\u{1F50C} Mongoose disconnected from optional cluster ${CLUSTER_NAME}`);
716
+ this.failedOptionalClusters?.add(CLUSTER_NAME);
717
+ });
718
+ this.clusterConnections[CLUSTER_NAME].on("reconnected", () => {
719
+ console.log(`\u{1F504} Mongoose reconnected to optional cluster ${CLUSTER_NAME}`);
720
+ this.failedOptionalClusters?.delete(CLUSTER_NAME);
721
+ });
722
+ });
703
723
  this.clusterConnections = {};
704
724
  this.env = options.env;
705
725
  this.dbConfigs = options.dbConfigs;
@@ -753,29 +773,62 @@ var require_MongoConnector = __commonJS({
753
773
  }
754
774
  async multiConnectToMongoDBAsync() {
755
775
  const allClusterConfigs = Object.values(this.dbConfigs);
776
+ this.failedOptionalClusters = /* @__PURE__ */ new Set();
756
777
  await Promise.all(
757
778
  allClusterConfigs.map(async (clusterConf) => {
758
- const { CLUSTER_NAME, CLUSTER_URI } = clusterConf;
779
+ const { CLUSTER_NAME, CLUSTER_URI, optional } = clusterConf;
759
780
  if (!CLUSTER_NAME || !CLUSTER_URI) {
760
781
  throw new Error(
761
782
  `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
762
783
  );
763
784
  }
764
- const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
765
- this.clusterConnections[CLUSTER_NAME] = connection;
766
- this.initiateConnectionEventListeners(CLUSTER_NAME);
767
- await new Promise((resolve, reject) => {
768
- connection.once("open", () => {
769
- console.log(`Connected to MongoDB: ${CLUSTER_NAME}`);
770
- resolve();
785
+ try {
786
+ const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
787
+ this.clusterConnections[CLUSTER_NAME] = connection;
788
+ if (!optional) {
789
+ this.initiateConnectionEventListeners(CLUSTER_NAME);
790
+ } else {
791
+ this.initiateOptionalConnectionEventListeners(CLUSTER_NAME);
792
+ }
793
+ await new Promise((resolve, reject) => {
794
+ connection.once("open", () => {
795
+ console.log(`Connected to MongoDB: ${CLUSTER_NAME}`);
796
+ resolve();
797
+ });
798
+ connection.once("error", (err) => {
799
+ if (optional) {
800
+ console.warn(`\u26A0\uFE0F Optional cluster ${CLUSTER_NAME} failed to connect: ${err.message}`);
801
+ resolve();
802
+ } else {
803
+ reject(err);
804
+ }
805
+ });
771
806
  });
772
- connection.once("error", reject);
773
- });
807
+ } catch (err) {
808
+ if (optional) {
809
+ console.warn(`\u26A0\uFE0F Optional cluster ${CLUSTER_NAME} failed to connect: ${err.message}`);
810
+ this.failedOptionalClusters.add(CLUSTER_NAME);
811
+ delete this.clusterConnections[CLUSTER_NAME];
812
+ } else {
813
+ throw err;
814
+ }
815
+ }
774
816
  })
775
817
  );
776
- console.log(
777
- `\u{1F389} Connected to ${Object.keys(this.clusterConnections).length} MongoDB databases`
778
- );
818
+ for (const clusterName of this.failedOptionalClusters) {
819
+ delete this.clusterConnections[clusterName];
820
+ }
821
+ const optionalFailedCount = this.failedOptionalClusters.size;
822
+ const connectedCount = Object.keys(this.clusterConnections).length;
823
+ if (optionalFailedCount > 0) {
824
+ console.log(
825
+ `\u{1F389} Connected to ${connectedCount} MongoDB databases (${optionalFailedCount} optional cluster(s) unavailable)`
826
+ );
827
+ } else {
828
+ console.log(
829
+ `\u{1F389} Connected to ${connectedCount} MongoDB databases`
830
+ );
831
+ }
779
832
  return this.clusterConnections;
780
833
  }
781
834
  // Static method to get the full instance
@@ -812,6 +865,12 @@ var require_MongoConnector = __commonJS({
812
865
  }
813
866
  return _MongoConnector.instance.tenantToClusterMapping;
814
867
  }
868
+ static isClusterUnavailable(clusterName) {
869
+ if (!_MongoConnector.instance) {
870
+ throw new Error("MongoConnector not initialized");
871
+ }
872
+ return _MongoConnector.instance.failedOptionalClusters?.has(clusterName) || !_MongoConnector.instance.clusterConnections[clusterName];
873
+ }
815
874
  /**
816
875
  * Helper function to close clusterConnections object
817
876
  */
package/dist/node.mjs CHANGED
@@ -705,6 +705,26 @@ var require_MongoConnector = __commonJS({
705
705
  console.log(`\u{1F504} Mongoose reconnected to ${CLUSTER_NAME}`);
706
706
  });
707
707
  });
708
+ __publicField(this, "initiateOptionalConnectionEventListeners", (CLUSTER_NAME) => {
709
+ this.clusterConnections[CLUSTER_NAME].on("open", () => {
710
+ console.log(`\u2705 Mongoose connection open to optional cluster ${CLUSTER_NAME}`);
711
+ this.failedOptionalClusters?.delete(CLUSTER_NAME);
712
+ });
713
+ this.clusterConnections[CLUSTER_NAME].on("error", (err) => {
714
+ console.warn(
715
+ `\u26A0\uFE0F Mongoose connection error on optional cluster ${CLUSTER_NAME}: ${err.message}`
716
+ );
717
+ this.failedOptionalClusters?.add(CLUSTER_NAME);
718
+ });
719
+ this.clusterConnections[CLUSTER_NAME].on("disconnected", () => {
720
+ console.log(`\u{1F50C} Mongoose disconnected from optional cluster ${CLUSTER_NAME}`);
721
+ this.failedOptionalClusters?.add(CLUSTER_NAME);
722
+ });
723
+ this.clusterConnections[CLUSTER_NAME].on("reconnected", () => {
724
+ console.log(`\u{1F504} Mongoose reconnected to optional cluster ${CLUSTER_NAME}`);
725
+ this.failedOptionalClusters?.delete(CLUSTER_NAME);
726
+ });
727
+ });
708
728
  this.clusterConnections = {};
709
729
  this.env = options.env;
710
730
  this.dbConfigs = options.dbConfigs;
@@ -758,29 +778,62 @@ var require_MongoConnector = __commonJS({
758
778
  }
759
779
  async multiConnectToMongoDBAsync() {
760
780
  const allClusterConfigs = Object.values(this.dbConfigs);
781
+ this.failedOptionalClusters = /* @__PURE__ */ new Set();
761
782
  await Promise.all(
762
783
  allClusterConfigs.map(async (clusterConf) => {
763
- const { CLUSTER_NAME, CLUSTER_URI } = clusterConf;
784
+ const { CLUSTER_NAME, CLUSTER_URI, optional } = clusterConf;
764
785
  if (!CLUSTER_NAME || !CLUSTER_URI) {
765
786
  throw new Error(
766
787
  `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
767
788
  );
768
789
  }
769
- const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
770
- this.clusterConnections[CLUSTER_NAME] = connection;
771
- this.initiateConnectionEventListeners(CLUSTER_NAME);
772
- await new Promise((resolve, reject) => {
773
- connection.once("open", () => {
774
- console.log(`Connected to MongoDB: ${CLUSTER_NAME}`);
775
- resolve();
790
+ try {
791
+ const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
792
+ this.clusterConnections[CLUSTER_NAME] = connection;
793
+ if (!optional) {
794
+ this.initiateConnectionEventListeners(CLUSTER_NAME);
795
+ } else {
796
+ this.initiateOptionalConnectionEventListeners(CLUSTER_NAME);
797
+ }
798
+ await new Promise((resolve, reject) => {
799
+ connection.once("open", () => {
800
+ console.log(`Connected to MongoDB: ${CLUSTER_NAME}`);
801
+ resolve();
802
+ });
803
+ connection.once("error", (err) => {
804
+ if (optional) {
805
+ console.warn(`\u26A0\uFE0F Optional cluster ${CLUSTER_NAME} failed to connect: ${err.message}`);
806
+ resolve();
807
+ } else {
808
+ reject(err);
809
+ }
810
+ });
776
811
  });
777
- connection.once("error", reject);
778
- });
812
+ } catch (err) {
813
+ if (optional) {
814
+ console.warn(`\u26A0\uFE0F Optional cluster ${CLUSTER_NAME} failed to connect: ${err.message}`);
815
+ this.failedOptionalClusters.add(CLUSTER_NAME);
816
+ delete this.clusterConnections[CLUSTER_NAME];
817
+ } else {
818
+ throw err;
819
+ }
820
+ }
779
821
  })
780
822
  );
781
- console.log(
782
- `\u{1F389} Connected to ${Object.keys(this.clusterConnections).length} MongoDB databases`
783
- );
823
+ for (const clusterName of this.failedOptionalClusters) {
824
+ delete this.clusterConnections[clusterName];
825
+ }
826
+ const optionalFailedCount = this.failedOptionalClusters.size;
827
+ const connectedCount = Object.keys(this.clusterConnections).length;
828
+ if (optionalFailedCount > 0) {
829
+ console.log(
830
+ `\u{1F389} Connected to ${connectedCount} MongoDB databases (${optionalFailedCount} optional cluster(s) unavailable)`
831
+ );
832
+ } else {
833
+ console.log(
834
+ `\u{1F389} Connected to ${connectedCount} MongoDB databases`
835
+ );
836
+ }
784
837
  return this.clusterConnections;
785
838
  }
786
839
  // Static method to get the full instance
@@ -817,6 +870,12 @@ var require_MongoConnector = __commonJS({
817
870
  }
818
871
  return _MongoConnector.instance.tenantToClusterMapping;
819
872
  }
873
+ static isClusterUnavailable(clusterName) {
874
+ if (!_MongoConnector.instance) {
875
+ throw new Error("MongoConnector not initialized");
876
+ }
877
+ return _MongoConnector.instance.failedOptionalClusters?.has(clusterName) || !_MongoConnector.instance.clusterConnections[clusterName];
878
+ }
820
879
  /**
821
880
  * Helper function to close clusterConnections object
822
881
  */
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.21.3",
6
+ "version": "1.21.4",
7
7
  "description": "Utility functions for both browser and Node.js",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/index.mjs",