cli icon indicating copy to clipboard operation
cli copied to clipboard

sequelize seeding

Open dharmykoya opened this issue 6 years ago • 11 comments

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

dharmykoya avatar Mar 11 '19 17:03 dharmykoya

what is the solution

tobeSprinble avatar May 01 '19 09:05 tobeSprinble

problem with the hashPassword function

tobeSprinble avatar May 01 '19 09:05 tobeSprinble

yes the hash function was the issue... so I hashed outside and pasted in the seed file

dharmykoya avatar May 01 '19 11:05 dharmykoya

don't understand pls show me

tobeSprinble avatar May 16 '19 13:05 tobeSprinble

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 image

dharmykoya avatar May 16 '19 14:05 dharmykoya

ok i get but is there not a better way to hash the password in the seed

tobeSprinble avatar May 16 '19 18:05 tobeSprinble

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?

JulianKlug avatar Jul 11 '19 09:07 JulianKlug

bump - why can't i use bcrypt in the seed files?

jdunc avatar Oct 09 '19 16:10 jdunc

you can define hash password function in same seed file. or can do this something like i did. image

codextech avatar Oct 30 '19 20:10 codextech

I'm also having a related issue. I think you can't use file imported from another place in seeding

rebirthtobi avatar Apr 06 '20 11:04 rebirthtobi

'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

hhoangg avatar May 31 '20 10:05 hhoangg