loopbackio/loopback-connector-postgresql

LB4 datasource never "connects"

reaperdtme opened this issue · 2 comments

Steps to reproduce

Latest loopback versions:

    "loopback-connector-postgresql": "^5.0.1",
 "@loopback/authentication": "^4.2.7",
    "@loopback/authorization": "^0.6.0",
    "@loopback/boot": "^2.3.3",
    "@loopback/context": "^3.9.0",
    "@loopback/core": "^2.8.0",
    "@loopback/openapi-v3": "^3.4.3",
    "@loopback/repository": "^2.7.0",
    "@loopback/rest": "^5.1.1",
    "@loopback/rest-explorer": "^2.2.4",
    "@loopback/security": "^0.2.12",
    "@loopback/service-proxy": "^2.3.2",

Make any psql datasource.

Current Behavior

Crashing in controller when accessing DB:

Unhandled error in POST /account/login: 500 TypeError: Cannot read property 'name' of undefined
    at PsqlDataSource.DataSource.queueInvocation.DataSource.ready (/Users/z/projects/valiant/web-lb/node_modules/loopback-datasource-juggler/lib/datasource.js:2880:22)
    at stillConnecting (/Users/z/projects/valiant/web-lb/node_modules/loopback-datasource-juggler/lib/dao.js:483:18)
    at Function.findOne (/Users/z/projects/valiant/web-lb/node_modules/loopback-datasource-juggler/lib/dao.js:1749:29)
    at UserRepository.findOne (/Users/z/projects/valiant/web-lb/node_modules/@loopback/repository/src/repositories/legacy-juggler-bridge.ts:405:23)

When accessing DB in tests, calling dataSource.connect() never returns, using either promise or callback:

const testDb = new juggler.DataSource({
  url: process.env.DATABASE_URL,
  connector: 'postgresql',
  name: 'TestPSQL',
  host: '',
  port: 0,
  user: '',
  password: '',
  database: '',
});
export const failConnectTestDb = async () => {
  // Never returns
  await testDb.connect();
};

Can workaround in tests by using the connected event, event though the connect call never returns, the connected event does fire.
The workaround:

export const connectTestDb = async () => {
  return new Promise((resolve, reject) => {
    testDb.on('connected', resolve);
    testDb
      .connect()
      .then(it => {
        console.log('Connect callback');
      })
      .catch(err => {
        console.error('Connect callback', err);
      });
  });
};

Expected Behavior

The dataSource connects and returns.

How's this large of a regression here?

Thank you @aspectgfg for reporting the issue.

Since you are claiming it's a regression, could you please let us know what is the latest version where this works for you? That way we can review the changes made since the last working version when looking for the source of the problem.

@bajtos actually don't have a working last version, just assumed it, because i'm pretty sure you guys are able to connect to your psql instances.
Don't understand what could be incorrect with my setup. All I'm currently running against is a db at localhost:5432 and the connect call never returns when calling a repository api or calling the dataSource.connect directly in tests. Tried versions 3.9.1 and 5.0.1 of this connectors builds.

Also, assuming this is a bug because the connected event fires, but the connect method never returns on it's promise.

We were also able to use the discovery model generators with this datasource, so the datasource has some functionality.