@treatwell/moleculer-essentials 2.2.0 → 2.2.1

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.
@@ -1005,22 +1005,30 @@ async function getIndexesDifference(collection, declaredIndexes = [], declaredSe
1005
1005
  }
1006
1006
  const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
1007
1007
  function DatabaseIndexesMixin(opts) {
1008
+ const { collectionName: mixinCollectionName, searchIndexes, indexes } = opts;
1008
1009
  return index$1.wrapMixin({
1009
1010
  methods: {
1010
1011
  async _syncIndexes({
1012
+ collectionName,
1011
1013
  dropIndexes,
1012
1014
  createIndexes
1013
1015
  }) {
1014
- if (typeof this.getCollection !== "function") {
1016
+ if (typeof this.getCollection !== "function" || typeof this.getMongoDb !== "function") {
1015
1017
  throw new Error(
1016
- "getCollection method not found, did you add the DatabaseConnectionMixin?"
1018
+ "getCollection or getMongoDb method not found, did you add the DatabaseConnectionMixin?"
1017
1019
  );
1018
1020
  }
1019
- const collection = this.getCollection();
1021
+ let collection;
1022
+ if (collectionName) {
1023
+ const db = this.getMongoDb();
1024
+ collection = db.collection(collectionName);
1025
+ } else {
1026
+ collection = this.getCollection();
1027
+ }
1020
1028
  const states = await getIndexesDifference(
1021
1029
  collection,
1022
- opts.indexes,
1023
- opts.searchIndexes
1030
+ indexes,
1031
+ searchIndexes
1024
1032
  );
1025
1033
  const notOkStates = states.filter((s) => s.status !== IndexStatus.OK);
1026
1034
  if (!notOkStates.length) {
@@ -1087,12 +1095,17 @@ function DatabaseIndexesMixin(opts) {
1087
1095
  ctx.logger.info(
1088
1096
  `Received sync indexes event for service ${this.name}`
1089
1097
  );
1090
- await this._syncIndexes({ createIndexes: true, dropIndexes: false });
1098
+ await this._syncIndexes({
1099
+ collectionName: mixinCollectionName,
1100
+ createIndexes: true,
1101
+ dropIndexes: false
1102
+ });
1091
1103
  }
1092
1104
  },
1093
1105
  "$broker.started": {
1094
1106
  async handler() {
1095
1107
  await this._syncIndexes({
1108
+ collectionName: mixinCollectionName,
1096
1109
  createIndexes: shouldAutoCreateIndexes(),
1097
1110
  dropIndexes: shouldAutoDropIndexes()
1098
1111
  });
@@ -792,16 +792,24 @@ type IndexState = {
792
792
  };
793
793
 
794
794
  type DatabaseIndexesOptions = {
795
+ /**
796
+ * Mostly useful when no collectionName was provided on the underlying DatabaseConnectionMixin.
797
+ * It allows users to have multiple DatabaseIndexesMixin on the same service.
798
+ *
799
+ * Using a different collectionName from the one specified in DatabaseConnectionMixin is NOT recommended.
800
+ */
801
+ collectionName?: string;
795
802
  indexes?: IndexTuple[];
796
803
  searchIndexes?: Record<string, SearchIndexDefinition>;
797
804
  };
798
805
  type SyncIndexesOptions = {
806
+ collectionName?: string;
799
807
  createIndexes: boolean;
800
808
  dropIndexes: boolean;
801
809
  };
802
810
  declare const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
803
811
  declare function DatabaseIndexesMixin(opts: DatabaseIndexesOptions): PartialCustomServiceSchema<unknown, {
804
- _syncIndexes({ dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
812
+ _syncIndexes({ collectionName, dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
805
813
  _createIndexFromState(col: Collection, state: IndexState): Promise<void>;
806
814
  }, unknown>;
807
815
 
@@ -792,16 +792,24 @@ type IndexState = {
792
792
  };
793
793
 
794
794
  type DatabaseIndexesOptions = {
795
+ /**
796
+ * Mostly useful when no collectionName was provided on the underlying DatabaseConnectionMixin.
797
+ * It allows users to have multiple DatabaseIndexesMixin on the same service.
798
+ *
799
+ * Using a different collectionName from the one specified in DatabaseConnectionMixin is NOT recommended.
800
+ */
801
+ collectionName?: string;
795
802
  indexes?: IndexTuple[];
796
803
  searchIndexes?: Record<string, SearchIndexDefinition>;
797
804
  };
798
805
  type SyncIndexesOptions = {
806
+ collectionName?: string;
799
807
  createIndexes: boolean;
800
808
  dropIndexes: boolean;
801
809
  };
802
810
  declare const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
803
811
  declare function DatabaseIndexesMixin(opts: DatabaseIndexesOptions): PartialCustomServiceSchema<unknown, {
804
- _syncIndexes({ dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
812
+ _syncIndexes({ collectionName, dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
805
813
  _createIndexFromState(col: Collection, state: IndexState): Promise<void>;
806
814
  }, unknown>;
807
815
 
@@ -1003,22 +1003,30 @@ async function getIndexesDifference(collection, declaredIndexes = [], declaredSe
1003
1003
  }
1004
1004
  const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
1005
1005
  function DatabaseIndexesMixin(opts) {
1006
+ const { collectionName: mixinCollectionName, searchIndexes, indexes } = opts;
1006
1007
  return wrapMixin({
1007
1008
  methods: {
1008
1009
  async _syncIndexes({
1010
+ collectionName,
1009
1011
  dropIndexes,
1010
1012
  createIndexes
1011
1013
  }) {
1012
- if (typeof this.getCollection !== "function") {
1014
+ if (typeof this.getCollection !== "function" || typeof this.getMongoDb !== "function") {
1013
1015
  throw new Error(
1014
- "getCollection method not found, did you add the DatabaseConnectionMixin?"
1016
+ "getCollection or getMongoDb method not found, did you add the DatabaseConnectionMixin?"
1015
1017
  );
1016
1018
  }
1017
- const collection = this.getCollection();
1019
+ let collection;
1020
+ if (collectionName) {
1021
+ const db = this.getMongoDb();
1022
+ collection = db.collection(collectionName);
1023
+ } else {
1024
+ collection = this.getCollection();
1025
+ }
1018
1026
  const states = await getIndexesDifference(
1019
1027
  collection,
1020
- opts.indexes,
1021
- opts.searchIndexes
1028
+ indexes,
1029
+ searchIndexes
1022
1030
  );
1023
1031
  const notOkStates = states.filter((s) => s.status !== IndexStatus.OK);
1024
1032
  if (!notOkStates.length) {
@@ -1085,12 +1093,17 @@ function DatabaseIndexesMixin(opts) {
1085
1093
  ctx.logger.info(
1086
1094
  `Received sync indexes event for service ${this.name}`
1087
1095
  );
1088
- await this._syncIndexes({ createIndexes: true, dropIndexes: false });
1096
+ await this._syncIndexes({
1097
+ collectionName: mixinCollectionName,
1098
+ createIndexes: true,
1099
+ dropIndexes: false
1100
+ });
1089
1101
  }
1090
1102
  },
1091
1103
  "$broker.started": {
1092
1104
  async handler() {
1093
1105
  await this._syncIndexes({
1106
+ collectionName: mixinCollectionName,
1094
1107
  createIndexes: shouldAutoCreateIndexes(),
1095
1108
  dropIndexes: shouldAutoDropIndexes()
1096
1109
  });
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "type": "git",
7
7
  "url": "https://github.com/treatwell/moleculer-essentials"
8
8
  },
9
- "version": "2.2.0",
9
+ "version": "2.2.1",
10
10
  "main": "./dist/index.cjs",
11
11
  "module": "./dist/index.mjs",
12
12
  "types": "./dist/index.d.cts",