Ayan
Ayan

Reputation: 11

Unable to connect to SAP HANA database from NestJS TypeOrm

All, I am very new to Node.js and SAP HANA. I am trying to connect to a HANA database that I have setup on my SAP trial account using TypeOrm on a NestJS application. My dependencies in package.json are as follows:

"dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/typeorm": "^9.0.1",
    "@sap/hana-client": "^2.16.21",
    "hdb-pool": "^0.1.6",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.12"
  }

In AppModule inside imports, I have the following:

TypeOrmModule.forRoot({
      type: 'sap',
      host: '<endpoint>',
      port: 443,
      logging: 'all',
      username: '<user>',
      password: '<password>',
      database: '<database>',
      schema: '<schema>',
      encrypt: true,
      extra:{
        sslValidateCertificate: "false",
      },
      verboseRetryLog: true,
      entities: [],
      synchronize: true
    })

But no matter what different options I try, I am consistently getting the following error while starting the Nest application. I tried putting the "sslValidateCertificate" outside of extra, have set pool: { min, max} values, but the connection is still not working.

SAP Hana pool raised an error. getConnect error: {"code":2,"sqlState":"HY000"} ERROR [TypeOrmModule] Unable to connect to the database (sap). Message: Request timeout. Request info: (id: 0, creation time: 1679498272307). Retrying (1)...

I followed a number of links on github and stackoverflow but nothing helps. Surprisingly, if I ignore TypeOrm and simply connect to the database as follows, it works perfectly fine, which seemingly means I don't have any problems with the DB connection.

 var connOptions = {
      serverNode: "<endpoint>",
      encrypt: "true",
      sslValidateCertificate: "false",
      uid: "<user>",
      pwd: "<password>",
    };

    var dbConnection = createConnection();

    dbConnection.connect(connOptions, function (err) {
      if (err) throw err;
      dbConnection.exec(
        "SELECT * FROM ......",
        function (err, result) {
          if (err) throw err;
          console.log(result);
          dbConnection.disconnect();
        }
      );
    });

Any help from anyone will be greatly appreciated.

Thank you!!!

Upvotes: 1

Views: 540

Answers (1)

Krishna
Krishna

Reputation: 660

With below configuration I am able to connect.

export const hanaDataSource = new DataSource({
        type: 'sap',
        host: 'your_host',
        port: 30015,// provide your post
        username: 'your_username',
        password: 'your_password',
        synchronize: true,
        logging: true,

        entities: [__dirname + '/**/*.entity{.ts,.js}'],
        extra: {
            serverNode: 'host1:30015,host2:30015,host3:30015' // if you have multiple hosts
        }
});

@Module({
    imports: [
        TypeOrmModule.forRootAsync({
            useFactory: async () => {
                await hanaDataSource.initialize();
                return hanaDataSource.options;
            }
        })
    ],
    controllers: [],
    providers: []
})
export class AppModule {}

Upvotes: 0

Related Questions