Skip to content

Instantly share code, notes, and snippets.

@umuralpay
Created April 16, 2021 21:27
Show Gist options
  • Save umuralpay/7b639868236d58a8373cd1b0d9e94d0b to your computer and use it in GitHub Desktop.
Save umuralpay/7b639868236d58a8373cd1b0d9e94d0b to your computer and use it in GitHub Desktop.
Sequelize hide properties from json
'use strict';
import {Model} from "sequelize";
module.exports = (sequelize, DataTypes) => {
class Product extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
// declare here or from the options "defaultScope.attributes.exclude" at the bottom
toJSON() {
return { ...this.get(), };
}
}
Product.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
createdAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
sequelize,
modelName: 'product',
underscored: true,
defaultScope: {
attributes: {exclude: ['updatedAt', 'createdAt']}
}
});
return Product;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment