Make a field readOnly with Sequelize
Sequelize does not allow by itself to define a field as readOnly. But you can do so by using on of Sequelize's addons named sequelize-noupdate-attributes.

Requirements
- An admin backend running on forest-express-sequelize 
- sequelize-noupdate-attributes npm package 
How it works
Directory: /models
This directory contains the files where your models are defined with Sequelize. To make fields readOnly you need to require the sequelize-noupdate-attributes package and add the noUpdate option to the relevant fields.
const sequelizeNoUpdateAttributes = require('sequelize-noupdate-attributes');
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  sequelizeNoUpdateAttributes(sequelize);
  
  const Users = sequelize.define('users', {
    firstName: {
      type: DataTypes.STRING,
      noUpdate: true,
    },
    lastName: {
      type: DataTypes.STRING,
      noUpdate: true,
    },
    email: {
      type: DataTypes.STRING,
    },
  }, {
    tableName: 'users',
    underscored: true,
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });
  return Users;
};
Last updated
Was this helpful?
