Hooks not triggering when inserting raw queries via sequelize.query()











up vote
1
down vote

favorite












I have the following Employee model for a MySQL database:



var bcrypt = require('bcrypt');

module.exports = (sequelize, DataTypes) => {
const Employee = sequelize.define(
"Employee",
{
username: DataTypes.STRING,
password: DataTypes.STRING,
}, {}
);
return Employee;
};


Seeding the database is done by reading a .sql file containing 10,000+ employees via raw queries:



sequelize.query(mySeedingSqlFileHere);



The problem is that the passwords in the SQL file are plain text and I'd like to use bcrypt to hash them before inserting into the database. I've never done bulk inserts before so I was looking into Sequelize docs for adding a hook to the Employee model, like so:



hooks: {
beforeBulkCreate: (employees, options) => {
for (employee in employees) {
if (employee.password) {
employee.password = await bcrypt.hash(employee.password, 10);
}
}
}
}


This isn't working as I'm still getting the plain text values after reseeding - should I be looking into another way? I was looking into sequelize capitalize name before saving in database - instance hook










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have the following Employee model for a MySQL database:



    var bcrypt = require('bcrypt');

    module.exports = (sequelize, DataTypes) => {
    const Employee = sequelize.define(
    "Employee",
    {
    username: DataTypes.STRING,
    password: DataTypes.STRING,
    }, {}
    );
    return Employee;
    };


    Seeding the database is done by reading a .sql file containing 10,000+ employees via raw queries:



    sequelize.query(mySeedingSqlFileHere);



    The problem is that the passwords in the SQL file are plain text and I'd like to use bcrypt to hash them before inserting into the database. I've never done bulk inserts before so I was looking into Sequelize docs for adding a hook to the Employee model, like so:



    hooks: {
    beforeBulkCreate: (employees, options) => {
    for (employee in employees) {
    if (employee.password) {
    employee.password = await bcrypt.hash(employee.password, 10);
    }
    }
    }
    }


    This isn't working as I'm still getting the plain text values after reseeding - should I be looking into another way? I was looking into sequelize capitalize name before saving in database - instance hook










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have the following Employee model for a MySQL database:



      var bcrypt = require('bcrypt');

      module.exports = (sequelize, DataTypes) => {
      const Employee = sequelize.define(
      "Employee",
      {
      username: DataTypes.STRING,
      password: DataTypes.STRING,
      }, {}
      );
      return Employee;
      };


      Seeding the database is done by reading a .sql file containing 10,000+ employees via raw queries:



      sequelize.query(mySeedingSqlFileHere);



      The problem is that the passwords in the SQL file are plain text and I'd like to use bcrypt to hash them before inserting into the database. I've never done bulk inserts before so I was looking into Sequelize docs for adding a hook to the Employee model, like so:



      hooks: {
      beforeBulkCreate: (employees, options) => {
      for (employee in employees) {
      if (employee.password) {
      employee.password = await bcrypt.hash(employee.password, 10);
      }
      }
      }
      }


      This isn't working as I'm still getting the plain text values after reseeding - should I be looking into another way? I was looking into sequelize capitalize name before saving in database - instance hook










      share|improve this question















      I have the following Employee model for a MySQL database:



      var bcrypt = require('bcrypt');

      module.exports = (sequelize, DataTypes) => {
      const Employee = sequelize.define(
      "Employee",
      {
      username: DataTypes.STRING,
      password: DataTypes.STRING,
      }, {}
      );
      return Employee;
      };


      Seeding the database is done by reading a .sql file containing 10,000+ employees via raw queries:



      sequelize.query(mySeedingSqlFileHere);



      The problem is that the passwords in the SQL file are plain text and I'd like to use bcrypt to hash them before inserting into the database. I've never done bulk inserts before so I was looking into Sequelize docs for adding a hook to the Employee model, like so:



      hooks: {
      beforeBulkCreate: (employees, options) => {
      for (employee in employees) {
      if (employee.password) {
      employee.password = await bcrypt.hash(employee.password, 10);
      }
      }
      }
      }


      This isn't working as I'm still getting the plain text values after reseeding - should I be looking into another way? I was looking into sequelize capitalize name before saving in database - instance hook







      mysql node.js sequelize.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 5:31

























      asked Nov 20 at 4:15









      Sean

      327519




      327519
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your hooks won't be called until you use model's function for DB operation , so if you are running raw query , hooks will never be fired,




          Reason : You can write anything inside your raw query , select/insert/update/delete anything , how does sequelize.js know that
          it has to fire the hooks. This is only possible when you use methods
          like



          Model.create();
          Model.bulkCreate();
          Model.update();
          Model.destroy;


          And as per DOC raw query doesn't have hooks option to add.
          And for MODEL queries you can check that it has option to
          enable/disable hook.







          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386132%2fhooks-not-triggering-when-inserting-raw-queries-via-sequelize-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            Your hooks won't be called until you use model's function for DB operation , so if you are running raw query , hooks will never be fired,




            Reason : You can write anything inside your raw query , select/insert/update/delete anything , how does sequelize.js know that
            it has to fire the hooks. This is only possible when you use methods
            like



            Model.create();
            Model.bulkCreate();
            Model.update();
            Model.destroy;


            And as per DOC raw query doesn't have hooks option to add.
            And for MODEL queries you can check that it has option to
            enable/disable hook.







            share|improve this answer

























              up vote
              1
              down vote



              accepted










              Your hooks won't be called until you use model's function for DB operation , so if you are running raw query , hooks will never be fired,




              Reason : You can write anything inside your raw query , select/insert/update/delete anything , how does sequelize.js know that
              it has to fire the hooks. This is only possible when you use methods
              like



              Model.create();
              Model.bulkCreate();
              Model.update();
              Model.destroy;


              And as per DOC raw query doesn't have hooks option to add.
              And for MODEL queries you can check that it has option to
              enable/disable hook.







              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                Your hooks won't be called until you use model's function for DB operation , so if you are running raw query , hooks will never be fired,




                Reason : You can write anything inside your raw query , select/insert/update/delete anything , how does sequelize.js know that
                it has to fire the hooks. This is only possible when you use methods
                like



                Model.create();
                Model.bulkCreate();
                Model.update();
                Model.destroy;


                And as per DOC raw query doesn't have hooks option to add.
                And for MODEL queries you can check that it has option to
                enable/disable hook.







                share|improve this answer












                Your hooks won't be called until you use model's function for DB operation , so if you are running raw query , hooks will never be fired,




                Reason : You can write anything inside your raw query , select/insert/update/delete anything , how does sequelize.js know that
                it has to fire the hooks. This is only possible when you use methods
                like



                Model.create();
                Model.bulkCreate();
                Model.update();
                Model.destroy;


                And as per DOC raw query doesn't have hooks option to add.
                And for MODEL queries you can check that it has option to
                enable/disable hook.








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 5:52









                Vivek Doshi

                19.9k22651




                19.9k22651






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386132%2fhooks-not-triggering-when-inserting-raw-queries-via-sequelize-query%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen