diff --git a/README.md b/README.md index ac5caef5..03dfa5c0 100644 --- a/README.md +++ b/README.md @@ -150,12 +150,13 @@ database. #### MomoConnectionOptions -| property | type | mandatory | default | description | -|-------------------|----------|-----------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| url | `string` | yes | | The connection string of your database. | -| scheduleName | `string` | yes | | Only one schedule per name can be active at a time. If multiple instances of your application define a schedule with the same name, only one at a time will actually run jobs. | -| collectionsPrefix | `string` | no | no prefix | A prefix for all collections created by Momo. | -| pingIntervalMs | number | no | `60_000` | The keep alive ping interval of the schedule, in milliseconds. After twice the amount of time has elapsed without a ping of your Momo instance, other instances may take over. You might want to reduce this if you have jobs running on short intervals. | +| property | type | mandatory | default | description | +|--------------------|----------------------|-----------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| url | `string` | yes | | The connection string of your database. | +| scheduleName | `string` | yes | | Only one schedule per name can be active at a time. If multiple instances of your application define a schedule with the same name, only one at a time will actually run jobs. | +| collectionsPrefix | `string` | no | no prefix | A prefix for all collections created by Momo. | +| pingIntervalMs | number | no | `60_000` | The keep alive ping interval of the schedule, in milliseconds. After twice the amount of time has elapsed without a ping of your Momo instance, other instances may take over. You might want to reduce this if you have jobs running on short intervals. | +| mongoClientOptions | `MongoClientOptions` | no | | Options for the connection to the MongoDB client as specified by the MongoDB API. Useful for providing configuration options that are not available via the connection string (url). | ### Reacting to events diff --git a/src/Connection.ts b/src/Connection.ts index fc30b975..5216c39d 100644 --- a/src/Connection.ts +++ b/src/Connection.ts @@ -1,4 +1,4 @@ -import { MongoClient } from 'mongodb'; +import { MongoClient, MongoClientOptions } from 'mongodb'; import { SchedulesRepository } from './repository/SchedulesRepository'; import { JobRepository } from './repository/JobRepository'; @@ -8,6 +8,12 @@ export interface MomoConnectionOptions { * The mongodb connection string. */ url: string; + /** + * Additional options for the connection to the Mongo client. + * Refer to MongoClientOptions in the MongoDB API documentation for a list of all available settings. + * Useful for providing configuration options that are not available via the connection string (url). + */ + mongoClientOptions?: MongoClientOptions; /** * Used to prefix all mongodb collections created by Momo. */ @@ -22,12 +28,12 @@ export class Connection { ) {} static async create( - { url, collectionsPrefix }: MomoConnectionOptions, + { url, mongoClientOptions, collectionsPrefix }: MomoConnectionOptions, pingIntervalMs: number, scheduleId: string, scheduleName: string, ): Promise { - const mongoClient = new MongoClient(url); + const mongoClient = new MongoClient(url, mongoClientOptions); await mongoClient.connect(); const schedulesRepository = new SchedulesRepository( diff --git a/test/Connection.spec.ts b/test/Connection.spec.ts new file mode 100644 index 00000000..16c6ec54 --- /dev/null +++ b/test/Connection.spec.ts @@ -0,0 +1,24 @@ +import { Connection } from '../src/Connection'; +import { MongoClient, MongoClientOptions } from 'mongodb'; + +jest.mock('mongodb'); +jest.mock('../src/repository/JobRepository'); +jest.mock('../src/repository/SchedulesRepository'); + +describe('Connection', () => { + const scheduleName = 'schedule'; + const scheduleId = 'scheduleId'; + const url = 'connection-string-to-db'; + + it('should use provided options for the connection to the mongo client', async () => { + const mongoClientOptions: MongoClientOptions = { + tls: true, + secureContext: { context: 'very secure' }, + }; + + await Connection.create({ url, mongoClientOptions }, 0, scheduleId, scheduleName); + + expect(MongoClient).toHaveBeenCalledTimes(1); + expect(MongoClient).toHaveBeenCalledWith(url, mongoClientOptions); + }); +});