cli
cli copied to clipboard
sequelize seeding
What you are doing?
./node_modules/.bin/sequelize db:seed:all
// code here
hashPassword.js
import bcrypt from 'bcrypt';
let password = '';
const hashPassword = (pass) => {
/**
* Hash Password Method
* @param {string} password
* @returns {string} returns hashed password
*/
password = bcrypt.hashSync(pass, bcrypt.genSaltSync(8));
return password;
};
export default hashPassword;
config.js
require('dotenv').config();
module.exports = {
development: {
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
host: process.env.DB_HOST,
dialect: 'postgres',
},
test: {
username: 'postgres',
password: 'postgres',
database: 'bookmeal',
host: '127.0.0.1',
dialect: 'postgres',
},
production: {
use_env_variable: 'DATABASE_URL',
},
}
my seeders file
import hashPassword from '../services/hashPassword';
const now = new Date();
module.exports = {
up: queryInterface => queryInterface.bulkInsert('Users', [
{
name: 'super-admin',
email: '[email protected]',
phone_number: '08037145164',
address: '12, oluwole street, Lagos.',
password: hashPassword('pass'),
role_id: 1,
// restaurant_name: 'super-admin',
// restaurant_logo: 'chris.png',
// type: 1,
authorizations: [1],
createdAt: now,
updatedAt: now,
},
{
name: 'Doyin John',
email: '[email protected]',
phone_number: '0801234567',
address: '12, ajayi street, Lagos.',
password: hashPassword('pass'),
role_id: 2,
// type: 2,
// restaurant_name: 'African kitchen',
// restaurant_logo: 'chris.png',
authorizations: [5, 6, 7, 8, 9, 10],
createdAt: now,
updatedAt: now,
},
{
name: 'Isaac Olayi',
email: '[email protected]',
phone_number: '0809876543',
address: '12, solanke street, Lagos.',
password: hashPassword('pass'),
role_id: 3,
// type: 3,
authorizations: [3, 10, 13, 14, 15],
createdAt: now,
updatedAt: now,
},
]),
down: queryInterface => queryInterface.bulkDelete('Users', null, {}),
};
What do you expect to happen?
my seed data should be saved in the database
What is actually happening?
But the output was bar! == 20190303113530-user: migrating ======= ERROR: Unexpected identifier
Output, either JSON or SQL
__Dialect: postgres __Database version: __Sequelize CLI version: "^5.4.0 __Sequelize version: ^4.42.0
what is the solution
problem with the hashPassword function
yes the hash function was the issue... so I hashed outside and pasted in the seed file
don't understand pls show me
I used the hashed function in another js file and console.log the result so I copied the result and pasted it in the seed file as a string

ok i get but is there not a better way to hash the password in the seed
Hashing outside the file still seems like a very unsatisfying solution. The generated hash might also be subject to change if the configuration of the hash function changes (for example number of salting rounds).
(Also the error message displayed by sequelize is not very useful to getting to the bottom of this error.)
Any better suggestions?
bump - why can't i use bcrypt in the seed files?
you can define hash password function in same seed file.
or can do this something like i did.

I'm also having a related issue. I think you can't use file imported from another place in seeding
'use strict';
const bcrypt = require('bcrypt');
const uuid = require('uuid');
const makePassword = (pw) => {
return new Promise(async rs => {
let salt, hash;
salt = await bcrypt.genSalt(10);
hash = await bcrypt.hash(pw, salt);
return rs(hash);
})
}
module.exports = {
up: async (queryInterface, Sequelize) => {
let password = await makePassword("abcABC123!@#");
return queryInterface.bulkInsert('Users', [{
id: uuid(),
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
password,
createdAt: new Date(),
updatedAt: new Date()
}], {});
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Users', null, {});
}
};
it worked with me