1

When i try to run my server sequelize not connect to database and for consequence none of my model are created.

My docker-compose

version: "3.7"
services:
   backend:
    build: ./Trademon-Backend
    environment:
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: Trademon
      DB_HOST: database
      DB_PORT: 5432
      DB_DIALECT: postgres
      DB_RECREATE: 1 # To recreate database
      JWT_SECRET: TrademonSecret
    ports:
      - "8000:8000"
    volumes:
      - ./Trademon-Backend:/backend/
      - /backend/node_modules/
    depends_on:
      - database
    command: ["./wait-for-it.sh", "database:5432", "--", "nodemon"]

  database:
    image: postgres:latest
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: Trademon
    volumes:
      - ~/databases/Trademon:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 30s
      timeout: 30s
      retries: 3

My app.js

const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const passport = require("passport");

const db = require("./models/index").sequelize;

const usersRouter = require("./routes/v1/users");

const app = express();

const RECREATE_DB = !!+process.env.DB_RECREATE || false;

require("./config/passport");

/**
 * Passport initialization
 */
app.use(passport.initialize());

/**
 * Database connection
 */

console.log("Trying to connect to database...");

try {
  db.authenticate();
  console.log("Connection to database has been established successfully.");

  if (RECREATE_DB) {
    console.log("Recreating database!");
    db.sync({ force: true });
  }
} catch (err) {
  console.error("Unable to connect to the database:", err);
}

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/v1/users", usersRouter);

app.use((req, res) => {
  res.status(404).send("Sorry this page does not exist!");
});

module.exports = app;

My sequelize config

"use strict";

const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");

const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/database.js")[env];
const basename = path.basename(__filename);
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    config
  );
}

fs.readdirSync(__dirname)
  .filter(file => {
    return (
      file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
    );
  })
  .forEach(file => {
    const model = sequelize["import"](path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

My log on console

I dont get errors from db.authenticate() but nothing happened and the execution proceed to the next lines (console.logs).

Any help?


Edit: This is an incompatibility with node v14, so downgrade to v13 ou LTS solved my problem.

3
  • Please post the Trademon-Backend Dockerfile and your package.json Commented May 11, 2020 at 9:16
  • Thank you, but after search for issues on sequelize github i found that the problem is the node version, i have node:current on dockerfile and when i have changed it to lts or 13 it runs without problems. Github issue Commented May 11, 2020 at 10:20
  • Upgrading to [email protected] should do the trick, too. Commented May 11, 2020 at 10:23

1 Answer 1

1

Stop your application:

docker-compose down

Clean up your databases files:

rm -rf ~/databases/Trademon/*

Then start again:

docker-compose up

and then restore database backup if any.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but after search for issues on sequelize github i found that the problem is the node version, i have node:current on dockerfile and when i have changed it to lts or 13 it runs without problems. Github issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.