Skip to content

Instantly share code, notes, and snippets.

@rjaus
Created August 4, 2021 05:58
Show Gist options
  • Save rjaus/34f257171b916f2428db2b6985880509 to your computer and use it in GitHub Desktop.
Save rjaus/34f257171b916f2428db2b6985880509 to your computer and use it in GitHub Desktop.
Sequelize JSON vs JSONB with Postgres
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Service 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
this.hasMany(models.Proof)
this.hasMany(models.Parser)
}
};
Service.init({
name: DataTypes.STRING,
public: {
type: DataTypes.BOOLEAN,
defaultValue: false
},
proofResources: {
type: DataTypes.JSON,
allowNull: false,
get() {
return JSON.parse(this.getDataValue("proofResources"));
},
set(value) {
return this.setDataValue("proofResources", JSON.stringify(value));
}
},
proofResourcesJSONB: {
type: DataTypes.JSONB
}
}
}, {
sequelize,
modelName: 'Service',
});
return Service;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment