Unable to connect to AWS RDS using Sequelize ORM

Issue

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.

Solution

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.

Answered By – meddy

Answer Checked By – Timothy Miller (AngularFixing Admin)

Leave a Reply

Your email address will not be published.