Skip to content

Commit

Permalink
feat(mongo): support user and password connection options
Browse files Browse the repository at this point in the history
Closes #31, #32
  • Loading branch information
B4nan committed Apr 2, 2019
1 parent 2499019 commit a2d9250
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
18 changes: 15 additions & 3 deletions lib/connections/MongoConnection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Collection, Db, MongoClient, ObjectID } from 'mongodb';
import { Connection, QueryResult } from './Connection';
import { Collection, Db, MongoClient, MongoClientOptions, ObjectID } from 'mongodb';
import { Connection, ConnectionConfig, QueryResult } from './Connection';
import { Utils } from '../utils';
import { QueryOrder } from '../query';
import { FilterQuery } from '..';
Expand All @@ -10,7 +10,7 @@ export class MongoConnection extends Connection {
protected db: Db;

async connect(): Promise<void> {
this.client = await MongoClient.connect(this.config.getClientUrl(), { useNewUrlParser: true });
this.client = await MongoClient.connect(this.config.getClientUrl(), this.getConnectionOptions());
this.db = this.client.db(this.config.get('dbName'));
}

Expand All @@ -30,6 +30,18 @@ export class MongoConnection extends Connection {
return 'mongodb://localhost:27017';
}

getConnectionOptions(): MongoClientOptions & ConnectionConfig {
const ret: MongoClientOptions = { useNewUrlParser: true };
const user = this.config.get('user');
const password = this.config.get('password');

if (user && password) {
ret.auth = { user, password };
}

return ret;
}

async execute(query: string): Promise<any> {
throw new Error(`${this.constructor.name} does not support generic execute method`);
}
Expand Down
12 changes: 11 additions & 1 deletion tests/EntityManager.mongo.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ObjectID } from 'mongodb';
import { Collection, EntityManager, MikroORM, QueryOrder } from '../lib';
import { Collection, Configuration, EntityManager, MikroORM, QueryOrder } from '../lib';
import { EntityProperty } from '../lib/decorators';
import { Author, Book, BookTag, Publisher, PublisherType, Test } from './entities';
import { AuthorRepository } from './repositories/AuthorRepository';
import { initORM, wipeDatabase } from './bootstrap';
import { MongoDriver } from '../lib/drivers/MongoDriver';
import { MongoConnection } from '../lib/connections/MongoConnection';
import { Logger } from '../lib/utils';

/**
Expand Down Expand Up @@ -307,6 +308,15 @@ describe('EntityManagerMongo', () => {
await expect(driver.getConnection().execute('')).rejects.toThrowError('MongoConnection does not support generic execute method');
});

test('should use user and password as connection options', async () => {
const config = new Configuration({ user: 'usr', password: 'pw' } as any, false);
const connection = new MongoConnection(config);
await expect(connection.getConnectionOptions()).toEqual({
useNewUrlParser: true,
auth: { user: 'usr', password: 'pw' },
});
});

test('findOne by id', async () => {
const authorRepository = orm.em.getRepository(Author);
const jon = new Author('Jon Snow', '[email protected]');
Expand Down

0 comments on commit a2d9250

Please sign in to comment.