Skip to content

Commit

Permalink
feat: add options for mongo client connection
Browse files Browse the repository at this point in the history
Issue: #857
Signed-off-by: Luisa Peter <[email protected]>
  • Loading branch information
luipe committed Oct 18, 2024
1 parent cf1ea2c commit b6aa04b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 9 additions & 3 deletions src/Connection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MongoClient } from 'mongodb';
import { MongoClient, MongoClientOptions } from 'mongodb';

import { SchedulesRepository } from './repository/SchedulesRepository';
import { JobRepository } from './repository/JobRepository';
Expand All @@ -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.
*/
Expand All @@ -22,12 +28,12 @@ export class Connection {
) {}

static async create(
{ url, collectionsPrefix }: MomoConnectionOptions,
{ url, mongoClientOptions, collectionsPrefix }: MomoConnectionOptions,
pingIntervalMs: number,
scheduleId: string,
scheduleName: string,
): Promise<Connection> {
const mongoClient = new MongoClient(url);
const mongoClient = new MongoClient(url, mongoClientOptions);
await mongoClient.connect();

const schedulesRepository = new SchedulesRepository(
Expand Down
24 changes: 24 additions & 0 deletions test/Connection.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit b6aa04b

Please sign in to comment.