Created
August 4, 2021 05:58
-
-
Save rjaus/34f257171b916f2428db2b6985880509 to your computer and use it in GitHub Desktop.
Sequelize JSON vs JSONB with Postgres
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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