Muhammad Essam
Muhammad Essam

Reputation: 21

How can i sync all models in one file in sequelize?

I'm a trying to sync all the models in one file , according to sequelize documentation sequelize.sync() will do the work but after running the file no errors occurs but also no tables created in the database

This an example of the product model file

module.exports = (sequelize, DataTypes) => {

const Product = sequelize.define('product', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
    },
    name: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    description: {
        type: DataTypes.TEXT
    },
    price: {
        type: DataTypes.DECIMAL
    }, sale_price: {
        type: DataTypes.DECIMAL,
    }, sku: {
        type: DataTypes.INTEGER
    }, qty: {
        type: DataTypes.INTEGER
    }
})
return Product;

}

and this is the file supposed to sync the database :

const { Sequelize, DataTypes } = require('sequelize');
const dbConfig = require('./config/dbConfig');
const Product = require('./models/product')

const sequelize = new Sequelize(
    dbConfig.DB,
    dbConfig.USER,
    dbConfig.PASSWORD, {
    host: dbConfig.HOST,
    dialect: dbConfig.dialect,
    operatorsAliases: false,
    pool: {
        max: dbConfig.pool.max,
        min: dbConfig.pool.min,
        aquire: dbConfig.pool.aquire,
        idle: dbConfig.pool.idle
    }
}
)

sequelize.authenticate()
    .then(() => {
        console.log("Connected to the database successfuly !");
    }).catch((err) => {
        console.log("Error: " + err)
    })


const db = {}

db.Sequelize = Sequelize
db.sequelize = sequelize
db.product = Product



db.sequelize.sync({ force: true })
    .then(() => {
        console.log("re-sync done!");
    })


module.exports = db

Upvotes: 1

Views: 2285

Answers (1)

Anatoly
Anatoly

Reputation: 22758

You didn't register your Product model in Sequelize instance. Please look at my other answer here to get the idea how to do it.

Upvotes: 1

Related Questions