简体   繁体   中英

How To Seed Data Files Using Sequelize and Custom Function With Promise?

I'm trying to use Sequelize in NodeJS and am using their Seeders CLI to try and add a query to bulkInsert command. I have created a simple JS function and everytime I run the sequelize migration command it returns a null value error. I also tried to do a beforeCreate method, but I'm not sure why the errors says that function does not exist. Here is what I need help with in code.

passGen.js

const bcrypt = require('bcrypt');

// https://github.com/kelektiv/node.bcrypt.js

function BpassGen(password){
    bcrypt.hash(password, 15, function(err, hash) {
        return hash;
      });
}

exports.BpassGen = BpassGen;

Then my Seed File

'use strict';
const PassGenX = require('../helpers/passwordGeneration');
var sequelize = require('sequelize');
const uuidV1 = require('uuid/v1');
const Accounts = require('../models/accounts');

const uuidT = uuidV1();

var password = PassGenX.BpassGen('secretConfig');

module.exports = {
  up: (queryInterface, Sequelize) => {

   // ERROR: Accounts.beforeCreate is not a function
   Accounts.beforeCreate((Accounts, options) => {
      return PassGenX.BpassGen('secretConfig').then(hashedPw => {
        Accounts.password = hashedPw;
      });
    });

   return queryInterface.bulkInsert('Accounts', [
    {
      uid: uuidT,
      userName: 'NaniMae1',
      firstName: 'Nani',
      lastName: 'Mae',
      dateOfBirth: new Date(),
      email: 'yaya@gmail.com',
      backupEmail: 'dsf@gmail.com',
      phoneNumber: null,
      phoneNumberStatus: 1,
      twoFactorEnabled: 1,
      imageURL: null,
      passwordHash: '',
      passwordKey: '',
      securityStamp: '',
      userLimit: 10,
      createdAt: new Date(),
      updatedAt: new Date(),
      Id_dataValidity: 1,
      Id_status: 1,
      Id_accountRole: 1,
      Id_inviteKey: 1,
      Id_privacy: 2
    }
], {});
  },

  down: (queryInterface, Sequelize) => {

  }
};

I also tried adding the password field to call the function, but it still results into null:

password: PassGenX.BpassGen('secretKey'),

What I want to do is maybe I need to add a promise? How would I do that? What I thought would happen is that my helper function would get called and then the password field will have the bcrypt hash, but that did not happen. In the CLI I just get the same error of 'password does not accept a null value' because in my models (sequelize migration definition) field for null is false, so it cannot be null.

How can I use my helper function to generate a password that is used in my seeder file for creating an account in Sequelize?

EDIT: How can I implement hooks like this such as direct method into my Account model js AND Account migration js files? sequelize hook docs

Or what about adding another QueryInterface like Upsert?

Upsert Sequelize

It seems that you are requiring the file itself, without sequelize.

Could you please try to change this line:

const Accounts = require('../models/accounts');

to:

const model = require('../models/index');

And then use it as:

model.Accounts.beforeCreate{(...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM