meddy
meddy

Reputation: 395

Unable to connect to AWS RDS using Sequelize ORM

I am working on an application which uses the Sequelize ORM to connect to AWS RDS. I have my connection set up as such:

Connection

import {Sequelize} from 'sequelize-typescript';



// Instantiate new Sequelize instance!
export const sequelize = new Sequelize({
  "username": "AWS RDS USER",
  "password": "AWS RDS PASS",
  "database": "postgres",
  "host":     "******.******.us-east-1.rds.amazonaws.com",

  dialect: 'postgres',
  storage: ':memory:',
});

I also have defined a model to represent the database table which is defined as such:

Model

import {Table, Column, Model, CreatedAt, UpdatedAt} from 'sequelize-typescript';

@Table
export class FeedItem extends Model<FeedItem> {
  @Column
  public caption!: string;

  @Column
  public url!: string;

  @Column
  @CreatedAt
  public createdAt: Date = new Date();

  @Column
  @UpdatedAt
  public updatedAt: Date = new Date();
}

and exported as such:

import { FeedItem } from './feed/models/FeedItem';


export const V0MODELS = [ FeedItem ];

Then within my server.ts I import my sequelize connection and model and attempt to connect to my AWS RDS as such:

server.ts

import express from 'express';
import { sequelize } from './sequelize';

import { IndexRouter } from './controllers/v0/index.router';

import { V0MODELS } from './controllers/v0/model.index';

(async () => {
  
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');
    await sequelize.addModels(V0MODELS);
    await sequelize.sync({ force: true, logging: console.log });
  } catch (error) {
    console.error(error);
  }
  
  
  const app = express();
  const port = process.env.PORT || 8080; // default port to listen
  
  app.use(express.json());

  //CORS Should be restricted
  app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "http://localhost:8100");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    next();
  });

  app.use('/api/v0/', IndexRouter)

  // Root URI call
  app.get( "/", async ( req, res ) => {
    res.send( "/api/v0/" );
  } );
  

  // Start the Server
  app.listen( port, () => {
      console.log( `server running http://localhost:${ port }` );
      console.log( `press CTRL+C to stop server` );
  } );
})();

When I run the program no connection is established, and the server fails to start. When I remove the sequelize.sync method, the server will start but my tables are not created. No error is caught by the catch block so I do not suspect there is an error. Currently I do believe this is connection issue dealing with postgres and AWS, but I cannot seem to pinned it down. All feedback and direction are appreciated.

Upvotes: 0

Views: 2147

Answers (2)

Godstrump
Godstrump

Reputation: 154

My node version is 16 and I am guessing Postgres 13 is not compatible with it hence, I had to downgrade my node version to 11.15.0.

Downgrade your node version to 11.15.0 by downloading nvm from here

Unzip the downloaded zip file and install. Click next and accept all default settings -- Do not customize the settings. If you have node already installed it might detect your current node version and ask to take control of it click yes.

After the installation process is done, open CMD or Powershell and type

nvm list

This is will show you the list of node versions installed, then type

nvm install 11.15.0

After installing the node 11.15.0 type

nvm list

To list the newly installed node version along with your previously installed node and then type

nvm use 11.15.0

Then go back to your vscode and run your project again.

#EDIT

If you're from ALX-Cloud Developer Program I suggest you downgrade your node version to 12 because Angular CLI version is only compatible with node v12 and above(at the time of writing this). Good luck!!!

Upvotes: 0

meddy
meddy

Reputation: 395

I have found the issue. The problem was due to the node version I was using, which at the time was 14.15.3. This version is not compatible with my current version of postgres 13.1 so I used nvm and downgraded to node 11.15.0 and now my sequelize commands are working.

Upvotes: 1

Related Questions