sequelize / cli

The Sequelize CLI
MIT License
2.53k stars 529 forks source link

sequelize seeding #752

Open dharmykoya opened 5 years ago

dharmykoya commented 5 years ago

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: 'dharmykoya38@gmail.com',
      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: 'doyin@gmail.com',
      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: 'isaac@gmail.com',
      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

tobeSprinble commented 5 years ago

what is the solution

tobeSprinble commented 5 years ago

problem with the hashPassword function

dharmykoya commented 5 years ago

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

tobeSprinble commented 5 years ago

don't understand pls show me

dharmykoya commented 5 years ago

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

tobeSprinble commented 5 years ago

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

JulianKlug commented 5 years ago

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?

jdunc commented 4 years ago

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

codextech commented 4 years ago

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

rebirthtobi commented 4 years ago

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

hhoangg commented 4 years ago
'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: 'admin@admin.com',
      password,
      createdAt: new Date(),
      updatedAt: new Date()
    }], {});
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.bulkDelete('Users', null, {});
  }
};

it worked with me