【问题标题】:How to change a specific column data using hooks in Node.js? (Sequelize, Postgresql)如何使用 Node.js 中的钩子更改特定列数据? (续集,Postgresql)
【发布时间】:2020-12-26 18:52:57
【问题描述】:

我使用迁移在 postgresql 数据库中的旧表中添加了一个新列,并为该列分配了默认值。

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    return Promise.all([
      queryInterface.addColumn(
        'notifications', // table name
        'flag', // new field name
        {
          type: Sequelize.INTEGER,
          defaultValue: 0,
          allowNull: false,
        },
      ),
    ]);
  },

  down: async (queryInterface, Sequelize) => {
  }
};

现在我想更改为该列的特定值。我怎么能用钩子或服务做到这一点?我是 node.js 的新手,非常感谢您的帮助。

我的通知.model.js

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;


module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const notifications = sequelizeClient.define('notifications', {
    senderId: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    recipientId: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    notificationtype: {
      type: DataTypes.STRING,
      allowNull: false
    },
    postId:{
      type:DataTypes.INTEGER,
      allowNull:true
    },
    conversationId:{
      type:DataTypes.INTEGER,
      allowNull:true
    }
  },
  
  );
  return notifications;
};

这是我的 notification.hooks.js。 我应该添加什么来将标志更新为 1?

const { authenticate } = require('@feathersjs/authentication').hooks;
const authHooks = require('feathers-authentication-hooks');

const { fastJoin, discard, disallow } = require('feathers-hooks-common');

const acquireNotification = require('../../hooks/notifications/acquire_notification');
const relateWithUser = require('../../hooks/notifications/relate_with_users');


const reverse = require('../../hooks/common/reverse');

module.exports = {
  before: {
    
    all: [
      authenticate('jwt'),
      
      reverse(),
      

    ],
    find: [
      acquireNotification(),
      reverse()
    ],
    get: [
      acquireNotification()
    ],
    create: [
      authHooks.associateCurrentUser({
        idField: 'id',
        as: 'senderId'
      }),
    ],
    update: [
    ],
    patch: [
    ],
    remove: [
    ]
  },

  after: {
    all: [],
    find: [
      fastJoin(relateWithUser),
    ],
    get: [],
    create: [
      fastJoin(relateWithUser),
    ],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};

【问题讨论】:

  • 您希望更新一些记录并将这些记录的flag 字段值设置为指定值吗?
  • 是的,我正在尝试这样做:/
  • 您是否使用 Sequelize 定义模型?如果是这样显示他们
  • 我编辑我的问题以显示

标签: node.js postgresql migration sequelize.js


【解决方案1】:

您需要在notifications 模型中添加flag 列:

flag:{
      type:DataTypes.INTEGER,
      allowNull:false
    }

然后你可以像这样更新一些具有特定条件的记录:

await notifications.update({
  flag: 1 // desired value
}, {
  where: {
    notificationtype: 'type' // indicate a desired condition here
  }
});

【讨论】:

  • 谢谢你,但我不能把它添加到我的钩子里,你能再看看我的问题吗?我添加了我的 hooks.js
猜你喜欢
  • 2021-06-08
  • 1970-01-01
  • 2022-07-01
  • 2010-12-06
  • 2021-11-08
  • 2012-01-25
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
相关资源
最近更新 更多