@treatwell/moleculer-essentials 2.1.1 → 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.
|
@@ -783,8 +783,16 @@ function DatabaseConnectionMixin(opts) {
|
|
|
783
783
|
getMongoClient() {
|
|
784
784
|
return this.mongoClient;
|
|
785
785
|
},
|
|
786
|
-
|
|
787
|
-
return this.getMongoClient().db(dbName
|
|
786
|
+
getMongoDb(options) {
|
|
787
|
+
return this.getMongoClient().db(dbName, options);
|
|
788
|
+
},
|
|
789
|
+
getCollection(options, dbOptions) {
|
|
790
|
+
if (!collectionName) {
|
|
791
|
+
throw new Error(
|
|
792
|
+
"No collectionName was provided in DatabaseConnectionMixin"
|
|
793
|
+
);
|
|
794
|
+
}
|
|
795
|
+
return this.getMongoClient().db(dbName, dbOptions).collection(collectionName, options);
|
|
788
796
|
}
|
|
789
797
|
},
|
|
790
798
|
created() {
|
|
@@ -809,13 +817,19 @@ function DatabaseConnectionMixin(opts) {
|
|
|
809
817
|
async started() {
|
|
810
818
|
this.logger.debug("Service connecting to mongoDB");
|
|
811
819
|
await this.getMongoClient().connect();
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
820
|
+
if (collectionName) {
|
|
821
|
+
this.logger.debug("Service connected to mongoDB, creating collection");
|
|
822
|
+
try {
|
|
823
|
+
await this.getMongoClient().db(dbName).createCollection(collectionName, createCollectionOptions);
|
|
824
|
+
} catch (err) {
|
|
825
|
+
if (err?.code !== 48) {
|
|
826
|
+
this.logger.error("Error while creating collection", { err });
|
|
827
|
+
}
|
|
818
828
|
}
|
|
829
|
+
} else {
|
|
830
|
+
this.logger.debug(
|
|
831
|
+
"Service connected to mongoDB, no collection defined"
|
|
832
|
+
);
|
|
819
833
|
}
|
|
820
834
|
},
|
|
821
835
|
async stopped() {
|
|
@@ -991,22 +1005,30 @@ async function getIndexesDifference(collection, declaredIndexes = [], declaredSe
|
|
|
991
1005
|
}
|
|
992
1006
|
const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
|
|
993
1007
|
function DatabaseIndexesMixin(opts) {
|
|
1008
|
+
const { collectionName: mixinCollectionName, searchIndexes, indexes } = opts;
|
|
994
1009
|
return index$1.wrapMixin({
|
|
995
1010
|
methods: {
|
|
996
1011
|
async _syncIndexes({
|
|
1012
|
+
collectionName,
|
|
997
1013
|
dropIndexes,
|
|
998
1014
|
createIndexes
|
|
999
1015
|
}) {
|
|
1000
|
-
if (typeof this.getCollection !== "function") {
|
|
1016
|
+
if (typeof this.getCollection !== "function" || typeof this.getMongoDb !== "function") {
|
|
1001
1017
|
throw new Error(
|
|
1002
|
-
"getCollection method not found, did you add the DatabaseConnectionMixin?"
|
|
1018
|
+
"getCollection or getMongoDb method not found, did you add the DatabaseConnectionMixin?"
|
|
1003
1019
|
);
|
|
1004
1020
|
}
|
|
1005
|
-
|
|
1021
|
+
let collection;
|
|
1022
|
+
if (collectionName) {
|
|
1023
|
+
const db = this.getMongoDb();
|
|
1024
|
+
collection = db.collection(collectionName);
|
|
1025
|
+
} else {
|
|
1026
|
+
collection = this.getCollection();
|
|
1027
|
+
}
|
|
1006
1028
|
const states = await getIndexesDifference(
|
|
1007
1029
|
collection,
|
|
1008
|
-
|
|
1009
|
-
|
|
1030
|
+
indexes,
|
|
1031
|
+
searchIndexes
|
|
1010
1032
|
);
|
|
1011
1033
|
const notOkStates = states.filter((s) => s.status !== IndexStatus.OK);
|
|
1012
1034
|
if (!notOkStates.length) {
|
|
@@ -1073,12 +1095,17 @@ function DatabaseIndexesMixin(opts) {
|
|
|
1073
1095
|
ctx.logger.info(
|
|
1074
1096
|
`Received sync indexes event for service ${this.name}`
|
|
1075
1097
|
);
|
|
1076
|
-
await this._syncIndexes({
|
|
1098
|
+
await this._syncIndexes({
|
|
1099
|
+
collectionName: mixinCollectionName,
|
|
1100
|
+
createIndexes: true,
|
|
1101
|
+
dropIndexes: false
|
|
1102
|
+
});
|
|
1077
1103
|
}
|
|
1078
1104
|
},
|
|
1079
1105
|
"$broker.started": {
|
|
1080
1106
|
async handler() {
|
|
1081
1107
|
await this._syncIndexes({
|
|
1108
|
+
collectionName: mixinCollectionName,
|
|
1082
1109
|
createIndexes: shouldAutoCreateIndexes(),
|
|
1083
1110
|
dropIndexes: shouldAutoDropIndexes()
|
|
1084
1111
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Document, ObjectId, WithoutId, InferIdType, Filter, OptionalId, WithId, CountDocumentsOptions, DeleteOptions, FindOneAndDeleteOptions, FindOptions, BulkWriteOptions, FindOneAndUpdateOptions, UpdateOptions, FindOneAndReplaceOptions, CollationOptions, CreateCollectionOptions, MongoClient, CollectionOptions, Collection, UpdateFilter, FindCursor, UpdateResult } from 'mongodb';
|
|
1
|
+
import { Document, ObjectId, WithoutId, InferIdType, Filter, OptionalId, WithId, CountDocumentsOptions, DeleteOptions, FindOneAndDeleteOptions, FindOptions, BulkWriteOptions, FindOneAndUpdateOptions, UpdateOptions, FindOneAndReplaceOptions, CollationOptions, CreateCollectionOptions, MongoClient, DbOptions, Db, CollectionOptions, Collection, UpdateFilter, FindCursor, UpdateResult } from 'mongodb';
|
|
2
2
|
import { ActionVisibility, Validators, Errors, Context } from 'moleculer';
|
|
3
3
|
import { ZodType, ZodObject } from 'zod/v4';
|
|
4
4
|
import { _ as ValidationSchema, J as JSONSchemaType, C as CustomActionSchema, P as PartialCustomServiceSchema } from '../newrelic-metrics-reporter-CmRMT4LG.cjs';
|
|
@@ -468,13 +468,14 @@ type DatabaseConnectionOptions = {
|
|
|
468
468
|
* Name of the database to use.
|
|
469
469
|
* If not specified, will use the default one (inferred from uri).
|
|
470
470
|
*
|
|
471
|
-
* OVERRIDDEN by globalThis.__MONGO_DB_NAME__ if set, which is useful for tests.
|
|
471
|
+
* OVERRIDDEN by `globalThis.__MONGO_DB_NAME__` if set, which is useful for tests.
|
|
472
472
|
*/
|
|
473
473
|
databaseName?: string;
|
|
474
474
|
/**
|
|
475
|
-
* Name of the collection in the DB.
|
|
475
|
+
* Name of the collection in the DB. If undefined, will throw on `getCollection` method
|
|
476
|
+
* and will not try to create the collection.
|
|
476
477
|
*/
|
|
477
|
-
collectionName: string;
|
|
478
|
+
collectionName: string | undefined;
|
|
478
479
|
/**
|
|
479
480
|
* Collection creation options.
|
|
480
481
|
* If not specified, will use the default one.
|
|
@@ -488,13 +489,14 @@ type DatabaseConnectionOptions = {
|
|
|
488
489
|
* - process.env.MONGODB_URL
|
|
489
490
|
* - 'mongodb://localhost:27017' (default)
|
|
490
491
|
*
|
|
491
|
-
* OVERRIDDEN by globalThis.__MONGO_URI__ if set, which is useful for tests.
|
|
492
|
+
* OVERRIDDEN by `globalThis.__MONGO_URI__` if set, which is useful for tests.
|
|
492
493
|
*/
|
|
493
494
|
uri?: string;
|
|
494
495
|
};
|
|
495
496
|
declare function DatabaseConnectionMixin<TSchema extends Record<string, unknown> = never>(opts: DatabaseConnectionOptions): PartialCustomServiceSchema<unknown, {
|
|
496
497
|
getMongoClient(): MongoClient;
|
|
497
|
-
|
|
498
|
+
getMongoDb(options?: DbOptions): Db;
|
|
499
|
+
getCollection(options?: CollectionOptions, dbOptions?: DbOptions): Collection<TSchema>;
|
|
498
500
|
}, PartialCustomServiceSchema<unknown, {
|
|
499
501
|
getStore(storeName: string): Map<string, {
|
|
500
502
|
services: Set<unknown>;
|
|
@@ -790,16 +792,24 @@ type IndexState = {
|
|
|
790
792
|
};
|
|
791
793
|
|
|
792
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;
|
|
793
802
|
indexes?: IndexTuple[];
|
|
794
803
|
searchIndexes?: Record<string, SearchIndexDefinition>;
|
|
795
804
|
};
|
|
796
805
|
type SyncIndexesOptions = {
|
|
806
|
+
collectionName?: string;
|
|
797
807
|
createIndexes: boolean;
|
|
798
808
|
dropIndexes: boolean;
|
|
799
809
|
};
|
|
800
810
|
declare const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
|
|
801
811
|
declare function DatabaseIndexesMixin(opts: DatabaseIndexesOptions): PartialCustomServiceSchema<unknown, {
|
|
802
|
-
_syncIndexes({ dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
|
|
812
|
+
_syncIndexes({ collectionName, dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
|
|
803
813
|
_createIndexFromState(col: Collection, state: IndexState): Promise<void>;
|
|
804
814
|
}, unknown>;
|
|
805
815
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Document, ObjectId, WithoutId, InferIdType, Filter, OptionalId, WithId, CountDocumentsOptions, DeleteOptions, FindOneAndDeleteOptions, FindOptions, BulkWriteOptions, FindOneAndUpdateOptions, UpdateOptions, FindOneAndReplaceOptions, CollationOptions, CreateCollectionOptions, MongoClient, CollectionOptions, Collection, UpdateFilter, FindCursor, UpdateResult } from 'mongodb';
|
|
1
|
+
import { Document, ObjectId, WithoutId, InferIdType, Filter, OptionalId, WithId, CountDocumentsOptions, DeleteOptions, FindOneAndDeleteOptions, FindOptions, BulkWriteOptions, FindOneAndUpdateOptions, UpdateOptions, FindOneAndReplaceOptions, CollationOptions, CreateCollectionOptions, MongoClient, DbOptions, Db, CollectionOptions, Collection, UpdateFilter, FindCursor, UpdateResult } from 'mongodb';
|
|
2
2
|
import { ActionVisibility, Validators, Errors, Context } from 'moleculer';
|
|
3
3
|
import { ZodType, ZodObject } from 'zod/v4';
|
|
4
4
|
import { _ as ValidationSchema, J as JSONSchemaType, C as CustomActionSchema, P as PartialCustomServiceSchema } from '../newrelic-metrics-reporter-CmRMT4LG.mjs';
|
|
@@ -468,13 +468,14 @@ type DatabaseConnectionOptions = {
|
|
|
468
468
|
* Name of the database to use.
|
|
469
469
|
* If not specified, will use the default one (inferred from uri).
|
|
470
470
|
*
|
|
471
|
-
* OVERRIDDEN by globalThis.__MONGO_DB_NAME__ if set, which is useful for tests.
|
|
471
|
+
* OVERRIDDEN by `globalThis.__MONGO_DB_NAME__` if set, which is useful for tests.
|
|
472
472
|
*/
|
|
473
473
|
databaseName?: string;
|
|
474
474
|
/**
|
|
475
|
-
* Name of the collection in the DB.
|
|
475
|
+
* Name of the collection in the DB. If undefined, will throw on `getCollection` method
|
|
476
|
+
* and will not try to create the collection.
|
|
476
477
|
*/
|
|
477
|
-
collectionName: string;
|
|
478
|
+
collectionName: string | undefined;
|
|
478
479
|
/**
|
|
479
480
|
* Collection creation options.
|
|
480
481
|
* If not specified, will use the default one.
|
|
@@ -488,13 +489,14 @@ type DatabaseConnectionOptions = {
|
|
|
488
489
|
* - process.env.MONGODB_URL
|
|
489
490
|
* - 'mongodb://localhost:27017' (default)
|
|
490
491
|
*
|
|
491
|
-
* OVERRIDDEN by globalThis.__MONGO_URI__ if set, which is useful for tests.
|
|
492
|
+
* OVERRIDDEN by `globalThis.__MONGO_URI__` if set, which is useful for tests.
|
|
492
493
|
*/
|
|
493
494
|
uri?: string;
|
|
494
495
|
};
|
|
495
496
|
declare function DatabaseConnectionMixin<TSchema extends Record<string, unknown> = never>(opts: DatabaseConnectionOptions): PartialCustomServiceSchema<unknown, {
|
|
496
497
|
getMongoClient(): MongoClient;
|
|
497
|
-
|
|
498
|
+
getMongoDb(options?: DbOptions): Db;
|
|
499
|
+
getCollection(options?: CollectionOptions, dbOptions?: DbOptions): Collection<TSchema>;
|
|
498
500
|
}, PartialCustomServiceSchema<unknown, {
|
|
499
501
|
getStore(storeName: string): Map<string, {
|
|
500
502
|
services: Set<unknown>;
|
|
@@ -790,16 +792,24 @@ type IndexState = {
|
|
|
790
792
|
};
|
|
791
793
|
|
|
792
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;
|
|
793
802
|
indexes?: IndexTuple[];
|
|
794
803
|
searchIndexes?: Record<string, SearchIndexDefinition>;
|
|
795
804
|
};
|
|
796
805
|
type SyncIndexesOptions = {
|
|
806
|
+
collectionName?: string;
|
|
797
807
|
createIndexes: boolean;
|
|
798
808
|
dropIndexes: boolean;
|
|
799
809
|
};
|
|
800
810
|
declare const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
|
|
801
811
|
declare function DatabaseIndexesMixin(opts: DatabaseIndexesOptions): PartialCustomServiceSchema<unknown, {
|
|
802
|
-
_syncIndexes({ dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
|
|
812
|
+
_syncIndexes({ collectionName, dropIndexes, createIndexes, }: SyncIndexesOptions): Promise<void>;
|
|
803
813
|
_createIndexFromState(col: Collection, state: IndexState): Promise<void>;
|
|
804
814
|
}, unknown>;
|
|
805
815
|
|
|
@@ -781,8 +781,16 @@ function DatabaseConnectionMixin(opts) {
|
|
|
781
781
|
getMongoClient() {
|
|
782
782
|
return this.mongoClient;
|
|
783
783
|
},
|
|
784
|
-
|
|
785
|
-
return this.getMongoClient().db(dbName
|
|
784
|
+
getMongoDb(options) {
|
|
785
|
+
return this.getMongoClient().db(dbName, options);
|
|
786
|
+
},
|
|
787
|
+
getCollection(options, dbOptions) {
|
|
788
|
+
if (!collectionName) {
|
|
789
|
+
throw new Error(
|
|
790
|
+
"No collectionName was provided in DatabaseConnectionMixin"
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
return this.getMongoClient().db(dbName, dbOptions).collection(collectionName, options);
|
|
786
794
|
}
|
|
787
795
|
},
|
|
788
796
|
created() {
|
|
@@ -807,13 +815,19 @@ function DatabaseConnectionMixin(opts) {
|
|
|
807
815
|
async started() {
|
|
808
816
|
this.logger.debug("Service connecting to mongoDB");
|
|
809
817
|
await this.getMongoClient().connect();
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
818
|
+
if (collectionName) {
|
|
819
|
+
this.logger.debug("Service connected to mongoDB, creating collection");
|
|
820
|
+
try {
|
|
821
|
+
await this.getMongoClient().db(dbName).createCollection(collectionName, createCollectionOptions);
|
|
822
|
+
} catch (err) {
|
|
823
|
+
if (err?.code !== 48) {
|
|
824
|
+
this.logger.error("Error while creating collection", { err });
|
|
825
|
+
}
|
|
816
826
|
}
|
|
827
|
+
} else {
|
|
828
|
+
this.logger.debug(
|
|
829
|
+
"Service connected to mongoDB, no collection defined"
|
|
830
|
+
);
|
|
817
831
|
}
|
|
818
832
|
},
|
|
819
833
|
async stopped() {
|
|
@@ -989,22 +1003,30 @@ async function getIndexesDifference(collection, declaredIndexes = [], declaredSe
|
|
|
989
1003
|
}
|
|
990
1004
|
const DATABASE_INDEXES_MIXIN_SYNC_EVENT = "database-indexes-mixin.sync";
|
|
991
1005
|
function DatabaseIndexesMixin(opts) {
|
|
1006
|
+
const { collectionName: mixinCollectionName, searchIndexes, indexes } = opts;
|
|
992
1007
|
return wrapMixin({
|
|
993
1008
|
methods: {
|
|
994
1009
|
async _syncIndexes({
|
|
1010
|
+
collectionName,
|
|
995
1011
|
dropIndexes,
|
|
996
1012
|
createIndexes
|
|
997
1013
|
}) {
|
|
998
|
-
if (typeof this.getCollection !== "function") {
|
|
1014
|
+
if (typeof this.getCollection !== "function" || typeof this.getMongoDb !== "function") {
|
|
999
1015
|
throw new Error(
|
|
1000
|
-
"getCollection method not found, did you add the DatabaseConnectionMixin?"
|
|
1016
|
+
"getCollection or getMongoDb method not found, did you add the DatabaseConnectionMixin?"
|
|
1001
1017
|
);
|
|
1002
1018
|
}
|
|
1003
|
-
|
|
1019
|
+
let collection;
|
|
1020
|
+
if (collectionName) {
|
|
1021
|
+
const db = this.getMongoDb();
|
|
1022
|
+
collection = db.collection(collectionName);
|
|
1023
|
+
} else {
|
|
1024
|
+
collection = this.getCollection();
|
|
1025
|
+
}
|
|
1004
1026
|
const states = await getIndexesDifference(
|
|
1005
1027
|
collection,
|
|
1006
|
-
|
|
1007
|
-
|
|
1028
|
+
indexes,
|
|
1029
|
+
searchIndexes
|
|
1008
1030
|
);
|
|
1009
1031
|
const notOkStates = states.filter((s) => s.status !== IndexStatus.OK);
|
|
1010
1032
|
if (!notOkStates.length) {
|
|
@@ -1071,12 +1093,17 @@ function DatabaseIndexesMixin(opts) {
|
|
|
1071
1093
|
ctx.logger.info(
|
|
1072
1094
|
`Received sync indexes event for service ${this.name}`
|
|
1073
1095
|
);
|
|
1074
|
-
await this._syncIndexes({
|
|
1096
|
+
await this._syncIndexes({
|
|
1097
|
+
collectionName: mixinCollectionName,
|
|
1098
|
+
createIndexes: true,
|
|
1099
|
+
dropIndexes: false
|
|
1100
|
+
});
|
|
1075
1101
|
}
|
|
1076
1102
|
},
|
|
1077
1103
|
"$broker.started": {
|
|
1078
1104
|
async handler() {
|
|
1079
1105
|
await this._syncIndexes({
|
|
1106
|
+
collectionName: mixinCollectionName,
|
|
1080
1107
|
createIndexes: shouldAutoCreateIndexes(),
|
|
1081
1108
|
dropIndexes: shouldAutoDropIndexes()
|
|
1082
1109
|
});
|