how to update Database using hooks/sequelize in nodejs

Aakash

this is my model.js


var methods = require("../hooks");

module.exports = (sequelize, DataTypes) => {
  const Users = sequelize.define(
    "dummytables",
    {
      first_name: {
        type: DataTypes.STRING,
        allowNull: false,
        defaultValue: "First Name",
      },
      last_name: {
        type: DataTypes.STRING,
        allowNull: false,
        defaultValue: "Last Name",
      },
      full_name: {
        type: DataTypes.STRING,
        allowNull: true,
      },
    },
    {
      hooks: methods.hooks,
    }
  );
  return Users;

this is my hooks.js file

const dummytable = require("../models/model");

var hooks = {};

exports.hooks = hooks;

var set_full_name = (dummytable) => {
  dummytable.full_name = dummytable.first_name + ` ` + dummytable.last_name;
};

hooks.beforeCreate = [set_full_name];
hooks.beforeUpdate = [set_full_name];

this is my apiRoutes.js file

const express = require("express");
const router = express.Router();
const db = require("../models");


// update data
router.put("/", (req, res) => {
  const { first_name, last_name, id } = req.body;
  db.dummytables
    .update(
      {
        first_name,
        last_name,
      },
      {
        where: {
          id,
        },
      }
    )
    .then(() => res.send("success"));
});

I want to insert and update full_name by using hooks

when i am inserting any data full_name get the value from first_name & last_name but when i am trying to update the data its not updating full_name

i dont know how to update full_name using hooks

Mohamed Mirghani

you need to use individualHooks when calling update

// update data
router.put("/", (req, res) => {
  const { first_name, last_name, id } = req.body;
  db.dummytables
    .update(
      {
        first_name,
        last_name,
      },
      {
        where: {
          id,
        },
        individualHooks: true
      }
    )
    .then(() => res.send("success"));
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to insert and update existing data in database using nodejs?

How to update database using pandastable

Update a column in database in NodeJs

How to update multiple columns in mysql using nodejs

How to update a data in mongodb using nodejs

How to dump postgres database and sync with other postgres database using nodejs

How to update Sql database using PHP

How to update or insert an iframe into database using Codeigniter?

how to update a record in database using LINQ?

How could I update the database using LINQ

How to update database using PHP variables?

how to update database using php,mysql

How to update the data in database using linq

How to update data in the database using artisan Tinker

How to update data in database using import excel

How to update file in database using jsp?

Using DTO Entity Framework, how to update Database?

How to update the database schema using NHibernate

How to UPDATE the data in the database using php

How to update DataBase using parameters via ODBC?

How to update json file in database using fetch

How to update links with common header file using javascript or nodejs

How to find and update the latest record in mongodb using nodejs?

how to update data after using aggregate in nodejs mongodb

How to update machine type of gcp instnace using nodejs client library

How can I know an update event on mysql using nodejs with mysql?

How to update the specific value in JSON and store it back in variable using NodeJS

How to update environment variable key name dynamically using NodeJS

How to disable/inactive the data in mongoDB database instead of delete using nodejs