Here are the basic steps to populate mySql tables with the secelize and sequelize-fixtures modules:
step 1: model creation
module.exports = function(sequelize, Sequelize) {
const User = sequelize.define('user', {
id : { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true },
firstname : { type: Sequelize.STRING },
lastname : { type: Sequelize.STRING },
email : { type: Sequelize.STRING, validate: {isEmail:true} },
password : { type: Sequelize.STRING },
});
return User;
}
2:
{
"development": {
"username": "root",
"password": null,
"database": "hotsausemedia",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "",
"password": null,
"database": "hotsausemedia",
"host": "",
"dialect": "mysql"
},
"production": {
"username": "",
"password": null,
"database": "hotsausemedia",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
3: . json
[
{
"model": "product",
"keys": ["id"],
"data": {
"id": 1,
"name": "Product #1",
"src": "./assets/img/products/01.jpg",
"price": 9.99,
"desc": "Product description..."
}
},
{
"model": "product",
"keys": ["id"],
"data": {
"id": 2,
"name": "Product #2",
"src": "./assets/img/products/02.jpg",
"price": 19.99,
"desc": "Product description..."
}
},
...
]
4:
models.sequelize.sync().then(() => {
console.log('You are connected to the database successfully.');
sequelize_fixtures.loadFile('./fixtures/*.json', models).then(() =>{
console.log("database is updated!");
});
}).catch((err) => {
console.log(err,"Some problems with database connection!!!");
});