0

I added a new column to my old table in postgresql database using migrations and assigned default value for that column.

'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) => {
  }
};

Now i want to change to specific values of that column. How can i do that with hooks or service? I am new at node.js, i will really appreciate your help.

My notifications.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;
};

This is my notifications.hooks.js. What should i add to update the flag to 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: []
  }
};

4
  • Do you wish to update some records and set flag field value for those records to a specified value? Commented Dec 26, 2020 at 18:56
  • Yes, i am trying to do that :/ Commented Dec 26, 2020 at 19:03
  • Do you define models using Sequelize? If so show them Commented Dec 26, 2020 at 19:10
  • I edit my question to show Commented Dec 26, 2020 at 19:21

1 Answer 1

1

You need to add a flag column to notifications model:

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

And then you can update some records with specific conditions like this:

await notifications.update({
  flag: 1 // desired value
}, {
  where: {
    notificationtype: 'type' // indicate a desired condition here
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that, but i can not add this to my hooks, Can you please check out my question again? I added my hooks.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.