@okf/ootils 1.21.4 → 1.21.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
@@ -776,9 +776,8 @@ declare class MongoConnector {
776
776
  */
777
777
  multiConnectToMongoDB(): {};
778
778
  multiConnectToMongoDBAsync(): Promise<{}>;
779
- failedOptionalClusters: Set<any> | undefined;
779
+ skippedClusters: Set<any> | undefined;
780
780
  initiateConnectionEventListeners: (CLUSTER_NAME: any) => void;
781
- initiateOptionalConnectionEventListeners: (CLUSTER_NAME: any) => void;
782
781
  /**
783
782
  * Helper function to close clusterConnections object
784
783
  */
package/dist/node.d.ts CHANGED
@@ -776,9 +776,8 @@ declare class MongoConnector {
776
776
  */
777
777
  multiConnectToMongoDB(): {};
778
778
  multiConnectToMongoDBAsync(): Promise<{}>;
779
- failedOptionalClusters: Set<any> | undefined;
779
+ skippedClusters: Set<any> | undefined;
780
780
  initiateConnectionEventListeners: (CLUSTER_NAME: any) => void;
781
- initiateOptionalConnectionEventListeners: (CLUSTER_NAME: any) => void;
782
781
  /**
783
782
  * Helper function to close clusterConnections object
784
783
  */
package/dist/node.js CHANGED
@@ -681,7 +681,7 @@ var require_MongoConnector = __commonJS({
681
681
  var AnnotationsSchema = (init_Annotations(), __toCommonJS(Annotations_exports));
682
682
  var PlatformConfigsSchema2 = (init_PlatformConfigs(), __toCommonJS(PlatformConfigs_exports));
683
683
  var TplSchema2 = (init_Tpl(), __toCommonJS(Tpl_exports));
684
- var MongoConnector3 = class _MongoConnector {
684
+ var MongoConnector4 = class _MongoConnector {
685
685
  constructor(options) {
686
686
  __publicField(this, "initiateConnectionEventListeners", (CLUSTER_NAME) => {
687
687
  this.clusterConnections[CLUSTER_NAME].on("open", () => {
@@ -700,26 +700,6 @@ 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
- });
723
703
  this.clusterConnections = {};
724
704
  this.env = options.env;
725
705
  this.dbConfigs = options.dbConfigs;
@@ -773,62 +753,37 @@ var require_MongoConnector = __commonJS({
773
753
  }
774
754
  async multiConnectToMongoDBAsync() {
775
755
  const allClusterConfigs = Object.values(this.dbConfigs);
776
- this.failedOptionalClusters = /* @__PURE__ */ new Set();
756
+ this.skippedClusters = /* @__PURE__ */ new Set();
777
757
  await Promise.all(
778
758
  allClusterConfigs.map(async (clusterConf) => {
779
- const { CLUSTER_NAME, CLUSTER_URI, optional } = clusterConf;
759
+ const { CLUSTER_NAME, CLUSTER_URI, skip } = clusterConf;
780
760
  if (!CLUSTER_NAME || !CLUSTER_URI) {
781
761
  throw new Error(
782
762
  `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
783
763
  );
784
764
  }
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
- });
806
- });
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
- }
765
+ if (skip) {
766
+ console.log(`\u23ED\uFE0F Skipping cluster ${CLUSTER_NAME} (skip: true)`);
767
+ this.skippedClusters.add(CLUSTER_NAME);
768
+ return;
815
769
  }
770
+ const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
771
+ this.clusterConnections[CLUSTER_NAME] = connection;
772
+ this.initiateConnectionEventListeners(CLUSTER_NAME);
773
+ await new Promise((resolve, reject) => {
774
+ connection.once("open", () => {
775
+ console.log(`Connected to MongoDB: ${CLUSTER_NAME}`);
776
+ resolve();
777
+ });
778
+ connection.once("error", reject);
779
+ });
816
780
  })
817
781
  );
818
- for (const clusterName of this.failedOptionalClusters) {
819
- delete this.clusterConnections[clusterName];
820
- }
821
- const optionalFailedCount = this.failedOptionalClusters.size;
782
+ const skippedCount = this.skippedClusters.size;
822
783
  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
- }
784
+ let statusMsg = `\u{1F389} Connected to ${connectedCount} MongoDB databases`;
785
+ if (skippedCount > 0) statusMsg += ` (${skippedCount} skipped)`;
786
+ console.log(statusMsg);
832
787
  return this.clusterConnections;
833
788
  }
834
789
  // Static method to get the full instance
@@ -869,7 +824,7 @@ var require_MongoConnector = __commonJS({
869
824
  if (!_MongoConnector.instance) {
870
825
  throw new Error("MongoConnector not initialized");
871
826
  }
872
- return _MongoConnector.instance.failedOptionalClusters?.has(clusterName) || !_MongoConnector.instance.clusterConnections[clusterName];
827
+ return _MongoConnector.instance.skippedClusters?.has(clusterName) || !_MongoConnector.instance.clusterConnections[clusterName];
873
828
  }
874
829
  /**
875
830
  * Helper function to close clusterConnections object
@@ -906,9 +861,9 @@ var require_MongoConnector = __commonJS({
906
861
  console.log("\u{1F389} All MongoDB clusterConnections closed gracefully");
907
862
  }
908
863
  };
909
- MongoConnector3.instance = null;
864
+ MongoConnector4.instance = null;
910
865
  module2.exports = {
911
- MongoConnector: MongoConnector3
866
+ MongoConnector: MongoConnector4
912
867
  };
913
868
  }
914
869
  });
@@ -1708,7 +1663,7 @@ __export(node_exports, {
1708
1663
  ElasticSearchConnector: () => import_ElasticSearchConnector.ElasticSearchConnector,
1709
1664
  GET_GLOBAL_BULLMQ_CONFIG: () => import_GET_GLOBAL_BULLMQ_CONFIG.GET_GLOBAL_BULLMQ_CONFIG,
1710
1665
  GeneratedTopicsSchema: () => GeneratedTopics_default,
1711
- MongoConnector: () => import_MongoConnector2.MongoConnector,
1666
+ MongoConnector: () => import_MongoConnector3.MongoConnector,
1712
1667
  PlatformConfigsSchema: () => PlatformConfigs_default,
1713
1668
  ProducerManager: () => import_ProducerManager.ProducerManager,
1714
1669
  RedisCacheConnector: () => RedisCacheConnector,
@@ -2252,13 +2207,14 @@ var mergeAnnoDataIntoAnnotationsTags = ({
2252
2207
  };
2253
2208
 
2254
2209
  // src/node.ts
2255
- var import_MongoConnector2 = __toESM(require_MongoConnector());
2210
+ var import_MongoConnector3 = __toESM(require_MongoConnector());
2256
2211
  var import_ElasticSearchConnector = __toESM(require_ElasticSearchConnector());
2257
2212
 
2258
2213
  // src/RedisCacheConnector.js
2259
2214
  var import_ioredis = __toESM(require("ioredis"));
2260
2215
  init_models();
2261
2216
  var import_getModelByTenant = __toESM(require_getModelByTenant());
2217
+ var import_MongoConnector2 = __toESM(require_MongoConnector());
2262
2218
  init_platformConfigTypes();
2263
2219
  var BASE_SETTINGS_FOR_CONFIGS_CACHE = [
2264
2220
  {
@@ -2522,7 +2478,13 @@ var RedisCacheConnector = class _RedisCacheConnector {
2522
2478
  const environment = this.getEnv();
2523
2479
  if (!tenant) throw new Error("No tenant/s defined to recache");
2524
2480
  console.log(`Loading configs and templates into cache...`);
2481
+ const tenantToClusterMapping = import_MongoConnector2.MongoConnector.getTenantToClusterMapping();
2525
2482
  const tenantCollectionPromises = toArray(tenant).map(async (tenant2) => {
2483
+ const clusterName = tenantToClusterMapping[tenant2]?.CLUSTER_NAME?.[environment];
2484
+ if (clusterName && import_MongoConnector2.MongoConnector.isClusterUnavailable(clusterName)) {
2485
+ console.log(`\u23ED\uFE0F Skipping cache load for tenant ${tenant2} (cluster ${clusterName} is unavailable)`);
2486
+ return;
2487
+ }
2526
2488
  return Promise.all(
2527
2489
  BASE_SETTINGS_FOR_CONFIGS_CACHE.map(
2528
2490
  async ({ modelName, schema, defaultQuery, typeKey }) => {
package/dist/node.mjs CHANGED
@@ -686,7 +686,7 @@ var require_MongoConnector = __commonJS({
686
686
  var AnnotationsSchema = (init_Annotations(), __toCommonJS(Annotations_exports));
687
687
  var PlatformConfigsSchema2 = (init_PlatformConfigs(), __toCommonJS(PlatformConfigs_exports));
688
688
  var TplSchema2 = (init_Tpl(), __toCommonJS(Tpl_exports));
689
- var MongoConnector3 = class _MongoConnector {
689
+ var MongoConnector4 = class _MongoConnector {
690
690
  constructor(options) {
691
691
  __publicField(this, "initiateConnectionEventListeners", (CLUSTER_NAME) => {
692
692
  this.clusterConnections[CLUSTER_NAME].on("open", () => {
@@ -705,26 +705,6 @@ 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
- });
728
708
  this.clusterConnections = {};
729
709
  this.env = options.env;
730
710
  this.dbConfigs = options.dbConfigs;
@@ -778,62 +758,37 @@ var require_MongoConnector = __commonJS({
778
758
  }
779
759
  async multiConnectToMongoDBAsync() {
780
760
  const allClusterConfigs = Object.values(this.dbConfigs);
781
- this.failedOptionalClusters = /* @__PURE__ */ new Set();
761
+ this.skippedClusters = /* @__PURE__ */ new Set();
782
762
  await Promise.all(
783
763
  allClusterConfigs.map(async (clusterConf) => {
784
- const { CLUSTER_NAME, CLUSTER_URI, optional } = clusterConf;
764
+ const { CLUSTER_NAME, CLUSTER_URI, skip } = clusterConf;
785
765
  if (!CLUSTER_NAME || !CLUSTER_URI) {
786
766
  throw new Error(
787
767
  `Missing CLUSTER_NAME or CLUSTER_URI in cluster conf: ${JSON.stringify(clusterConf)}`
788
768
  );
789
769
  }
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
- });
811
- });
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
- }
770
+ if (skip) {
771
+ console.log(`\u23ED\uFE0F Skipping cluster ${CLUSTER_NAME} (skip: true)`);
772
+ this.skippedClusters.add(CLUSTER_NAME);
773
+ return;
820
774
  }
775
+ const connection = mongoose5.createConnection(CLUSTER_URI, this.mongoOptions);
776
+ this.clusterConnections[CLUSTER_NAME] = connection;
777
+ this.initiateConnectionEventListeners(CLUSTER_NAME);
778
+ await new Promise((resolve, reject) => {
779
+ connection.once("open", () => {
780
+ console.log(`Connected to MongoDB: ${CLUSTER_NAME}`);
781
+ resolve();
782
+ });
783
+ connection.once("error", reject);
784
+ });
821
785
  })
822
786
  );
823
- for (const clusterName of this.failedOptionalClusters) {
824
- delete this.clusterConnections[clusterName];
825
- }
826
- const optionalFailedCount = this.failedOptionalClusters.size;
787
+ const skippedCount = this.skippedClusters.size;
827
788
  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
- }
789
+ let statusMsg = `\u{1F389} Connected to ${connectedCount} MongoDB databases`;
790
+ if (skippedCount > 0) statusMsg += ` (${skippedCount} skipped)`;
791
+ console.log(statusMsg);
837
792
  return this.clusterConnections;
838
793
  }
839
794
  // Static method to get the full instance
@@ -874,7 +829,7 @@ var require_MongoConnector = __commonJS({
874
829
  if (!_MongoConnector.instance) {
875
830
  throw new Error("MongoConnector not initialized");
876
831
  }
877
- return _MongoConnector.instance.failedOptionalClusters?.has(clusterName) || !_MongoConnector.instance.clusterConnections[clusterName];
832
+ return _MongoConnector.instance.skippedClusters?.has(clusterName) || !_MongoConnector.instance.clusterConnections[clusterName];
878
833
  }
879
834
  /**
880
835
  * Helper function to close clusterConnections object
@@ -911,9 +866,9 @@ var require_MongoConnector = __commonJS({
911
866
  console.log("\u{1F389} All MongoDB clusterConnections closed gracefully");
912
867
  }
913
868
  };
914
- MongoConnector3.instance = null;
869
+ MongoConnector4.instance = null;
915
870
  module.exports = {
916
- MongoConnector: MongoConnector3
871
+ MongoConnector: MongoConnector4
917
872
  };
918
873
  }
919
874
  });
@@ -2217,13 +2172,14 @@ var mergeAnnoDataIntoAnnotationsTags = ({
2217
2172
  };
2218
2173
 
2219
2174
  // src/node.ts
2220
- var import_MongoConnector2 = __toESM(require_MongoConnector());
2175
+ var import_MongoConnector3 = __toESM(require_MongoConnector());
2221
2176
  var import_ElasticSearchConnector = __toESM(require_ElasticSearchConnector());
2222
2177
 
2223
2178
  // src/RedisCacheConnector.js
2224
2179
  init_models();
2225
2180
  import Redis from "ioredis";
2226
2181
  var import_getModelByTenant = __toESM(require_getModelByTenant());
2182
+ var import_MongoConnector2 = __toESM(require_MongoConnector());
2227
2183
  init_platformConfigTypes();
2228
2184
  var BASE_SETTINGS_FOR_CONFIGS_CACHE = [
2229
2185
  {
@@ -2487,7 +2443,13 @@ var RedisCacheConnector = class _RedisCacheConnector {
2487
2443
  const environment = this.getEnv();
2488
2444
  if (!tenant) throw new Error("No tenant/s defined to recache");
2489
2445
  console.log(`Loading configs and templates into cache...`);
2446
+ const tenantToClusterMapping = import_MongoConnector2.MongoConnector.getTenantToClusterMapping();
2490
2447
  const tenantCollectionPromises = toArray(tenant).map(async (tenant2) => {
2448
+ const clusterName = tenantToClusterMapping[tenant2]?.CLUSTER_NAME?.[environment];
2449
+ if (clusterName && import_MongoConnector2.MongoConnector.isClusterUnavailable(clusterName)) {
2450
+ console.log(`\u23ED\uFE0F Skipping cache load for tenant ${tenant2} (cluster ${clusterName} is unavailable)`);
2451
+ return;
2452
+ }
2491
2453
  return Promise.all(
2492
2454
  BASE_SETTINGS_FOR_CONFIGS_CACHE.map(
2493
2455
  async ({ modelName, schema, defaultQuery, typeKey }) => {
@@ -2557,7 +2519,7 @@ var export_BaseWorker = import_BaseWorker.BaseWorker;
2557
2519
  var export_ChunksElasticSyncProducer = import_ChunksElasticSyncProducer.ChunksElasticSyncProducer;
2558
2520
  var export_ElasticSearchConnector = import_ElasticSearchConnector.ElasticSearchConnector;
2559
2521
  var export_GET_GLOBAL_BULLMQ_CONFIG = import_GET_GLOBAL_BULLMQ_CONFIG.GET_GLOBAL_BULLMQ_CONFIG;
2560
- var export_MongoConnector = import_MongoConnector2.MongoConnector;
2522
+ var export_MongoConnector = import_MongoConnector3.MongoConnector;
2561
2523
  var export_ProducerManager = import_ProducerManager.ProducerManager;
2562
2524
  var export_WorkerManager = import_WorkerManager.WorkerManager;
2563
2525
  var export_getAIChatModelByTenant = import_getModelByTenant2.getAIChatModelByTenant;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.21.4",
6
+ "version": "1.21.6",
7
7
  "description": "Utility functions for both browser and Node.js",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/index.mjs",