@treatwell/moleculer-essentials 2.1.1 → 2.2.0

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
- getCollection(options) {
787
- return this.getMongoClient().db(dbName).collection(collectionName, options);
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
- this.logger.debug("Service connected to mongoDB, creating collection");
813
- try {
814
- await this.getMongoClient().db(dbName).createCollection(collectionName, createCollectionOptions);
815
- } catch (err) {
816
- if (err?.code !== 48) {
817
- this.logger.error("Error while creating collection", { err });
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() {
@@ -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
- getCollection(options?: CollectionOptions): Collection<TSchema>;
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>;
@@ -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
- getCollection(options?: CollectionOptions): Collection<TSchema>;
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>;
@@ -781,8 +781,16 @@ function DatabaseConnectionMixin(opts) {
781
781
  getMongoClient() {
782
782
  return this.mongoClient;
783
783
  },
784
- getCollection(options) {
785
- return this.getMongoClient().db(dbName).collection(collectionName, options);
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
- this.logger.debug("Service connected to mongoDB, creating collection");
811
- try {
812
- await this.getMongoClient().db(dbName).createCollection(collectionName, createCollectionOptions);
813
- } catch (err) {
814
- if (err?.code !== 48) {
815
- this.logger.error("Error while creating collection", { err });
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() {
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.1.1",
9
+ "version": "2.2.0",
10
10
  "main": "./dist/index.cjs",
11
11
  "module": "./dist/index.mjs",
12
12
  "types": "./dist/index.d.cts",