sequelize / cli

The Sequelize CLI
MIT License
2.54k stars 525 forks source link

sequelize db:seed:all doesn't work with beforeCreate. Isn't it? #689

Closed frederikhors closed 6 years ago

frederikhors commented 6 years ago

I'm trying to seed my DB with users.

I'm using seed like this:

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.bulkInsert('Users', [
      {
        email: 'fake@user.com',
        password: 'fakepass',
        createdAt: new Date(),
        updatedAt: new Date()
      }
    ], {})
  },

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

In my model I have:

const bcrypt = require('bcrypt')

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    email: DataTypes.STRING,
    password: DataTypes.STRING
  }, {})

  User.beforeCreate(async user => {
    user.password = await user.generatePasswordHash()
  })

  User.prototype.generatePasswordHash = () => {
    const saltRounds = 10
    return bcrypt.hash(this.password, saltRounds)
  }

  return User
}

But when I try to use:

sequelize db:seed:all it doesn't hash passwords.

Why?

bupyc9 commented 6 years ago

@frederikhors Seed does not use model. Data is inserted directly into the table

jlevesque commented 6 years ago

How can we seed a table and call beforeCreate then ?

nmihaica commented 6 years ago

@jlevesque By importing and populating through sequelize model.

KefDS commented 5 years ago

@nmihaica can I import and populate using sequelize models in the up function?

So far I've tried to call MyModel.create() and MyModel.beforeBulkCreate() but I got this: ERROR: Error: Migration 20181226195402-demo-user.js (or wrapper) didn't return a promise

If this function is not intended to use another thing that the queryInterface API, where can we seed a table using sequelize models?

nmihaica commented 5 years ago

@KefDS Yes.

const User = require('@models').User 

let users = [];

module.exports = {
  up: function (queryInterface, Sequelize) {
    return User.bulkCreate(users, {
      validate:true,
      individualHooks: true
    })
  },
  down: function (queryInterface, Sequelize) {
  }
};
jlevesque commented 5 years ago

Calling the sequelize model is a good solution, thanks @nmihaica.

But the problem is that queryInterface doesn't support hooks. I wanted to use it so I wouldn't have to import my model in the seed script. If your project is in ES6, like mine, you have to run the seeds with babel. I was trying to avoid that, because I wanted to be able to seed some basic data on the prod server, and this server only contains the builded application and the packages to run it. It doesn't seems logical to me to have babel on a live server and this also means that I had to push the src code on the prod server.

I did a database import instead hehe. My seed scripts are similar to nmihaica's example.

tobeSprinble commented 5 years ago

this is not woking for me `module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.bulkInsert('FirstLevelAdmins', [{ name: 'John', password: 'Doe', createdAt: new Date(), updatedAt: new Date(), email: 'johnDoe@test.com', id: '06c57dbd-b35f-4d53-9088-60d120bcdb81' }], { individualHooks: true }); },

down: function (queryInterface, Sequelize) { queryInterface.bulkDelete('FirstLevelAdmins', [{ }]) } }; `

kiran-ghuge commented 5 years ago

@frederikhors seed does not work use model data inserted directly into table, I have same problem so I did something like this to bcrypt password import bcrypt first and then implement as follows

const salt = bcrypt.genSaltSync();
return queryInterface.bulkInsert('t_users', [{
      username: 'admin',
      password: bcrypt.hashSync('123456', salt),
      created_at: new Date(),
      updated_at: new Date()
    }], {});

Hope this will solve your problem

osmanyz commented 4 years ago

@frederikhors seed does not work use model data inserted directly into table, I have same problem so I did something like this to bcrypt password import bcrypt first and then implement as follows

const salt = bcrypt.genSaltSync();
return queryInterface.bulkInsert('t_users', [{
      username: 'admin',
      password: bcrypt.hashSync('123456', salt),
      created_at: new Date(),
      updated_at: new Date()
    }], {});

Hope this will solve your problem

it worked

blindibrasil commented 4 years ago

I am using (async - await) and it is working perfectly. If you want to see how it looks, I'm posting the code below:

const bcrypt = require('bcryptjs');

module.exports = {
  up: async queryInterface => {
    return queryInterface.bulkInsert('users', [
      {
        name: 'Admin',
        email: 'admin@root.com',
        passwordHash: await bcrypt.hash('123456', 8),
        type: 'admin',
        createdAt: new Date(),
        updatedAt: new Date(),
        avatarId: null,
      },
    ]);
  },

  down: queryInterface => {
    return queryInterface.bulkDelete('users', null);
  },
};
felipe-muner commented 4 years ago

I did the same as @blindibrasil haha tamo junto amigao !

MaykonMorais commented 4 years ago

@blindibrasil Great! Thank you!