Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb config does not seem to wait for the connection to resolve #996

Closed
daffl opened this issue Sep 13, 2018 · 6 comments
Closed

mongodb config does not seem to wait for the connection to resolve #996

daffl opened this issue Sep 13, 2018 · 6 comments

Comments

@daffl
Copy link
Member

daffl commented Sep 13, 2018

From @eddyystop on July 30, 2017 20:45

The relevent code in src/services/monododb.js is

  const config = app.get('mongodb');
  const promise = MongoClient.connect(config);
  app.set('mongoClient', promise);

My test apparently used a mongodb service before the connection was established. It looks like it threw because app.service(...) was undefined. It did not throw once I delayed the test with a setTimeout.

I think the main concern is that an app using mongodb may sometimes boot and sometimes not.

Copied from original issue: feathersjs-ecosystem/generator-feathers#253

@daffl
Copy link
Member Author

daffl commented Sep 13, 2018

I think adding something like

before(() => app.get('mongoClient'));

In the tests should also wait for the client to connect and run your tests reliably.

@daffl
Copy link
Member Author

daffl commented Sep 13, 2018

From @eddyystop on July 30, 2017 21:20

These are not mocha tests, rather some test apps. I think this should at least be documented as its a time sink to track down.

@daffl
Copy link
Member Author

daffl commented Sep 13, 2018

I wonder if there is a good way for the generator to wait for the connection before starting the server.

@daffl
Copy link
Member Author

daffl commented Sep 13, 2018

From @eddyystop on August 1, 2017 17:35

That is the conundrum.

One positive point is that MongoDB can only be used on the server. Can anything be done with async/await?

@daffl
Copy link
Member Author

daffl commented Sep 13, 2018

This will be addressed with #853 and #509

@AmberByte
Copy link

For everyone coming here from google, I've worked around this by using a custom event which is emitted when the setup is finished.
I'm using mongoose but it should work the same way for mongodb as well

src/mongoose.ts:

import mongoose from 'mongoose';
import { Application } from './declarations';
import logger from './logger';

export default function (app: Application) {
  mongoose.connect(
    app.get('mongodb'),
    { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true },
  ).then(() => {
   // This emits a event called mognoose-ready on app instance
    app.emit('mongoose-ready');
  }).catch((err) => {
    logger.error(err);
    process.exit(1);
  });

  mongoose.Promise = global.Promise;

  app.set('mongooseClient', mongoose);
}

src/app.test.ts

  beforeAll((done) => {
    // Waits for mongoose to be initialized and ready then executed tests
    app.on('mongoose-ready', () => {
      done();
    });
  });

  afterAll(() => {
      app.get('mongooseClient').disconnect();
  })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants