Skip to content

Commit

Permalink
fix: handle properly mongo connection close (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-moncel authored Jul 23, 2021
1 parent 556ebfd commit 4a6da5d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/services/schema/update/analyzer/database-analyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = class DatabaseAnalyzer {
let analyze;
if (config.dbDialect === 'mongodb') {
analyze = this.mongoAnalyzer;
databaseConnection = databaseConnection.db();
} else {
analyze = this.sequelizeAnalyzer;
}
Expand Down
1 change: 0 additions & 1 deletion src/services/schema/update/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class Database {
}

return this.mongodb.MongoClient.connect(connectionUrl, connectionOptionsMongoClient)
.then((client) => client.db(options.dbName))
.catch((error) => this.handleAuthenticationError(error));
}

Expand Down
5 changes: 3 additions & 2 deletions test/services/analyzer/database-analyzer-mongo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ describe('services > database analyser > MongoDB', () => {
expect.assertions(1);
const mongoHelper = await getMongoHelper(mongoUrl);
const databaseConnection = await mongoHelper.connect();
const db = databaseConnection.db();
await mongoHelper.dropAllCollections();
await databaseConnection.collection('connect_test').insertOne({ name: 'hello' });
const doc = await databaseConnection.collection('connect_test').findOne({ name: 'hello' });
await db.collection('connect_test').insertOne({ name: 'hello' });
const doc = await db.collection('connect_test').findOne({ name: 'hello' });
await mongoHelper.close();
expect(doc.name).toStrictEqual('hello');
});
Expand Down
6 changes: 3 additions & 3 deletions test/services/analyzer/helpers/mongo-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ const dbName = 'forest-test';

class MongoHelper {
constructor(url) {
this.url = url;
this.url = `${url}/${dbName}`;
}

connect() {
this.client = new MongoClient(this.url, { useNewUrlParser: true, useUnifiedTopology: true });
return new Promise((resolve) => {
this.client.connect((err) => {
assert.equal(null, err);
this.db = this.client.db(dbName);
resolve(this.db);
this.db = this.client.db();
resolve(this.client);
});
});
}
Expand Down
23 changes: 0 additions & 23 deletions test/services/database.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,6 @@ describe('services > database', () => {
});
});

it('should call mongodb db function on successful connection', async () => {
expect.assertions(2);

const mongoDbMock = {
db: jest.fn(),
};
const context = {
mongodb: {
MongoClient: {
connect: jest.fn().mockReturnValue(
Promise.resolve(mongoDbMock),
),
},
},
};
const database = setupDatabase(context);

await database.connectToMongodb({ dbName: 'fake' });

expect(mongoDbMock.db).toHaveBeenCalledTimes(1);
expect(mongoDbMock.db).toHaveBeenCalledWith('fake');
});

it('should call handleAuthenticationError on a failed connection', async () => {
expect.assertions(2);

Expand Down

0 comments on commit 4a6da5d

Please sign in to comment.